最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue實(shí)現(xiàn)購(gòu)物車實(shí)例代碼兩則

 更新時(shí)間:2020年05月30日 10:52:25   作者:若星  
這篇文章主要介紹了Vue實(shí)現(xiàn)購(gòu)物車實(shí)例代碼,需要的朋友可以參考下

一、第一種比較簡(jiǎn)單

效果圖

實(shí)現(xiàn)代碼:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>購(gòu)物車案例</title>
 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<style>
 *{
 padding: 0;
 margin:0
 }
 ul li{
 width: 1200px;
 display: flex;
 align-items: center;
 justify-content: center;
 }
 li div,.total{
 display: inline-block;
 width:200px;
 height: 50px;
 line-height: 50px;
 text-align: center;
 }
 button{
 width: 60px;
 height: 40px;
 text-align: center;
 line-height: 40px;
 }

</style>
<body>

 <div id="app">
 <ul>
  <goodsitem 
  v-for="item in goodslist" 
  :item="item" 
  :key="item.id"
  @onchange="(type)=>{handleCount(type,item)}"
  @ondelete="()=>{handleDelete(item.id)}">
  </goodsitem>
  <div class="total" style="padding-left: 113px">總價(jià):{{total}}</div>
 </ul>
 </div>

</body>
<script>
 var computed={
 props:{
  count:{
   type:Number,
   require:true
  }
 },
 methods:{
  handleCount(type){
  this.$emit('onchange',type)
  }
 },
 template:`<div style="width:200px">
   <button @click="handleCount('sub')">-</button>
   <span>{{count}}</span>
   <button @click="handleCount('add')" >+</button>
   </div>
 
 `
 
 }
 var goodsitem={
 props:{
  item:{
  type:Object,
  require:true
  }
 },
 methods:{
  handleCount(type){
  this.$emit('onchange',type)
  },
  handleDelete(){
  this.$emit('ondelete')
  }
 },
 components:{
  computed
 },
 template:`<li>
   <div>{{item.goodsName}}</div>
   <div>{{item.price}}</div>
   <computed :count="item.count" @onchange="handleCount"></computed>
   <div>{{item.sum}}</div>
   <div><button @click="handleDelete">刪除</button></div>
   </li>
   `
 }

 var app=new Vue({
 el:"#app",
 data:{
  goodslist:[{
  id:1,
  goodsName:"小可愛(ài)",
  price:100,
  count:1,
  sum:100
  },{
  id:2,
  goodsName:"小可愛(ài)",
  price:200,
  count:2,
  sum:400
  },{
  id:3,
  goodsName:"小可愛(ài)",
  price:300,
  count:3,
  sum:900
  },{
  id:4,
  goodsName:"小可愛(ài)",
  price:400,
  count:1,
  sum:400
  },
  ]
 },
 methods:{
  handleCount(type,item){
  if(type=='add'){
   item.count+=1
  }else{
   if(item.count==1){
   this.handleDelete(item.id) 
   return
   }
   item.count-=1
  }
  item.sum=item.count*item.price
  },
  handleDelete(id){
  return this.goodslist=this.goodslist.filter((item)=>{
   return id!=item.id
  })
  }
 },
 computed:{
  total(){
  return this.goodslist.reduce((total,item)=>{
   return total+=item.sum
  },0)
  }
 },
 components:{
  goodsitem
 }
 })
</script>
</html>

二、一個(gè)用vue實(shí)現(xiàn)的簡(jiǎn)單響應(yīng)式購(gòu)物車案例

實(shí)現(xiàn)結(jié)果

如上,所有書(shū)類數(shù)據(jù)存在數(shù)組里,遍歷顯示在表格中,點(diǎn)擊+和-可以實(shí)現(xiàn)數(shù)量和總價(jià)格的響應(yīng)式變化,其中,減號(hào)到1時(shí)便添加了disabled類型,無(wú)法點(diǎn)擊。 價(jià)格顯示時(shí)通過(guò)過(guò)濾器顯示的,加上Z¥符號(hào)和兩位小數(shù)。項(xiàng)目結(jié)構(gòu)為三個(gè)文件。

index.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <link rel="stylesheet" href="style.css" rel="external nofollow" >
</head>
<body>
<div id="app">
 <div v-if="books.length">
 <table>
 <thead>
 <tr>
 <th></th>
 <th>書(shū)籍日期</th>
 <th>出版日期</th>
 <th>價(jià)格</th>
 <th>購(gòu)買數(shù)量</th>
 <th>操作 </th>
 </tr>
 </thead>

 <tbody>
 <tr v-for="(item,index) in books">
 <!--<td v-for="value in item">{{value}}</td>-->
 <td>{{item.id}}</td>
 <td>{{item.name}}</td>
 <td>{{item.date}}</td>
 <!--<td>{{getFinalPrice(item.price)}}</td> 下面是過(guò)濾器語(yǔ)法-->
 <td>{{item.price | showPrice}}</td>
 <td>
 <button @click="increment(index)" v-bind:disabled="item.count<=1">-</button>
 {{item.count}}
 <button @click="decrement(index)">+</button>
 </td>
 <td>
 <button @click="removeHandler(index)">移除</button>
 </td>
 </tr>
 </tbody>
 </table>
 <h2>總價(jià)格: {{totalprice | showPrice}}</h2>
 </div>
 <h2 v-else> 購(gòu)物車為空</h2>
</div>
<script src="../js/vue.js"></script>
<script src="main.js"></script>
<script></script>
</body>
</html>

main.js

const app = new Vue({
 el:"#app",
 data: {
 books: [
 {
 id: 1,
 name: '算法導(dǎo)論',
 date: '2019-01-10',
 price: 85.00,
 count: 1
 },
 {
 id: 2,
 name: '計(jì)算機(jī)導(dǎo)論',
 date: '2019-02-14',
 price: 90.00,
 count: 2
 },
 {
 id: 3,
 name: '科學(xué)導(dǎo)論',
 date: '2019-09-10',
 price: 85.21,
 count: 1
 },
 {
 id: 4,
 name: '網(wǎng)絡(luò)導(dǎo)論',
 date: '2019-08-21',
 price: 19.02,
 count: 1
 },
 ]
 },
 methods:{
 getFinalPrice(price) {
 return '$' + price.toFixed(2)
 },
 increment(index){
 /*if(this.books[index].count <= 1) return*/
 this.books[index].count--
 },
 decrement(index){
 this.books[index].count++
 },
 removeHandler(index){
 this.books.splice(index,1)
 }
 },
 filters:{
 showPrice(price){
 return '$' + price.toFixed(2)
 }
 },
 computed:{
 totalprice(){
 let tprice = 0
 for(let i = 0; i< this.books.length; i++)
 {
 tprice += this.books[i].price * this.books[i].count
 }
 return tprice
 }
 }
})

style.css

table{
 border: 1px solid #e9e9e9;
 border-collapse: collapse;
 bordre-spacing: 0;
}

th, td {
 padding: 8px 16px;
 border: 1px solid #e9e9e9;
 text-align: left;
}

th{
 backgroud-color: #f7f7f7;
 color: #5c6b77;
 font-weight: 600;
}

到此這篇關(guān)于Vue實(shí)現(xiàn)購(gòu)物車實(shí)例代碼的文章就介紹到這了,更多相關(guān)Vue 購(gòu)物車內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決vue打包后vendor.js文件過(guò)大問(wèn)題

    解決vue打包后vendor.js文件過(guò)大問(wèn)題

    這篇文章主要介紹了解決vue打包后vendor.js文件過(guò)大問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • vuex如何在非組件中調(diào)用mutations方法

    vuex如何在非組件中調(diào)用mutations方法

    這篇文章主要介紹了vuex如何在非組件中調(diào)用mutations方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Vue獲取頁(yè)面元素的相對(duì)位置的方法示例

    Vue獲取頁(yè)面元素的相對(duì)位置的方法示例

    這篇文章主要介紹了Vue獲取頁(yè)面元素的相對(duì)位置的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Vue3中實(shí)現(xiàn)歌詞滾動(dòng)顯示效果

    Vue3中實(shí)現(xiàn)歌詞滾動(dòng)顯示效果

    本文分享如何在Vue 3中實(shí)現(xiàn)一個(gè)簡(jiǎn)單的歌詞滾動(dòng)效果,我將從歌詞數(shù)據(jù)的處理開(kāi)始,一步步介紹布局的搭建和事件的實(shí)現(xiàn),感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • Vue3中集成高德地圖并實(shí)現(xiàn)平移縮放功能

    Vue3中集成高德地圖并實(shí)現(xiàn)平移縮放功能

    隨著前端技術(shù)的不斷發(fā)展,地圖應(yīng)用在我們的項(xiàng)目中越來(lái)越常見(jiàn),本文將介紹如何在Vue3項(xiàng)目中集成高德地圖,并通過(guò)簡(jiǎn)單的配置實(shí)現(xiàn)地圖的平移和縮放功能,需要的朋友可以參考下
    2024-09-09
  • Vue實(shí)現(xiàn)購(gòu)物車基本功能

    Vue實(shí)現(xiàn)購(gòu)物車基本功能

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)購(gòu)物車的基本功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 一文搞懂Vue中watch偵聽(tīng)器的用法

    一文搞懂Vue中watch偵聽(tīng)器的用法

    在Vue.js中,您可以使用watch選項(xiàng)來(lái)創(chuàng)建偵聽(tīng)器,以偵聽(tīng)特定屬性的變化,偵聽(tīng)器可以在屬性發(fā)生變化時(shí)執(zhí)行相關(guān)的邏輯,本文給大家詳細(xì)講講Vue中watch偵聽(tīng)器的用法,需要的朋友可以參考下
    2023-11-11
  • Ant Design封裝年份選擇組件的方法

    Ant Design封裝年份選擇組件的方法

    這篇文章主要為大家詳細(xì)介紹了Ant Design封裝年份選擇組件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Pinia進(jìn)階setup函數(shù)式寫法封裝到企業(yè)項(xiàng)目

    Pinia進(jìn)階setup函數(shù)式寫法封裝到企業(yè)項(xiàng)目

    這篇文章主要為大家介紹了Pinia進(jìn)階setup函數(shù)式寫法封裝到企業(yè)項(xiàng)目實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Vue 處理表單input單行文本框的實(shí)例代碼

    Vue 處理表單input單行文本框的實(shí)例代碼

    這篇文章主要Vue 處理表單input單行文本框的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05

最新評(píng)論

巩留县| 吉木萨尔县| 枣强县| 渝北区| 辰溪县| 平阳县| 太仆寺旗| 昭苏县| 营口市| 左贡县| 平邑县| 绥化市| 色达县| 杨浦区| 巫山县| 宁强县| 台北市| 金乡县| 定兴县| 罗山县| 叶城县| 上犹县| 文山县| 宣汉县| 柞水县| 芒康县| 鹿泉市| 青阳县| 珲春市| 县级市| 咸阳市| 阿坝县| 广饶县| 南岸区| 开化县| 上犹县| 原平市| 伊金霍洛旗| 台东县| 麟游县| 乐平市|