vue2.x 5分鐘上手

安裝

安裝vue-cli 這是vue的腳手架

npm install vue-cli -g

創建項目

vue init webpack 項目名

下載依賴

    npm install

運行

npm run dev

打包

npm run build

常用指令

[v-bind和v-on]

v-bind指令用于設置HTML屬性:v-bind:href 縮寫為 :href

<a :href="{{url}}">aa</a>

v-on 指令用于綁定HTML事件 :v-on:click 縮寫為 @click

<a @click="get()">aa</a>

<a v-on:click="get()">aa</a>
<div id="three">
    <input type="button" value="點我" @click="onClick"/>
</div>

var three = new Vue({
    el: "#three",
  methods: {
       onClick:function () {
           console.log("This is Test")
       }
    }
})

條件,循環

v-if &v-for

 <template v-if="list.length">
  </template>
 <div v-else>空</div>
-----
<tr v-for="(item, index) in list">
<td>{{index+1}}</td>
<td>{{item.name}}</td>
<td>{{item.price}}</td>
<td>
    <el-button @click="handleReduce(index)" :disabled="item.count ===1">-</el-button>
    {{item.count}}
    <el-button @click="handleAdd(index)">+</el-button>
</td>
<td>
    <el-button @click="handleRemove(index)">移除</el-button>
</td>
</tr>

表格Demo

表格CRUD

html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Vue 購物車Demo</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
    <!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> -->
    <!-- 引入樣式 -->
    <link rel="stylesheet" >
    <link rel="stylesheet" type="text/css" href="index.css"/>
    <!-- 引入組件庫 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>

<body>
    <div id="app" v-cloak>
        <template v-if="list.length">
            <table>
                <thead>
                    <tr>
                        <th>\</th>
                        <th>商品名稱</th>
                        <th>商品單價</th>
                        <th>購買數量</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item, index) in list">
                        <td>{{index+1}}</td>
                        <td>{{item.name}}</td>
                        <td>{{item.price}}</td>
                        <td>
                            <el-button @click="handleReduce(index)" :disabled="item.count ===1">-</el-button>
                            {{item.count}}
                            <el-button @click="handleAdd(index)">+</el-button>
                        </td>
                        <td>
                            <el-button @click="handleRemove(index)">移除</el-button>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div>總價:¥{{totalPrice}}</div>
        </template>
        <div v-else>購物車為空</div>
    </div>
    <script src="index.js" type="text/javascript"></script>
</body>

</html>

css

table{
    border: 1px solid #e9e9e9 ;
    border-collapse: collapse ;
    border-spacing: 0 ;
    empty-cells: show ;
}
th, td{
    padding: 8px 16px; 
    border: 1px solid #e9e9e9 ;
    text-align: left ;
}

js

var app = new Vue({
    el: '#app',
    data: {
        list:[{
            id:1,
            name:'iphone7',
            price:6688,
            count:5
        },
        {
            id:2,
            name:'iphonex',
            price:18999,
            count:2
        },
        {
            id:3,
            name:'iphone8',
            price:13888,
            count:3
        }]
        
    },
    methods: {
        handleReduce:function(index){
            if(this.list[index].count===1) return ;
            this.list[index].count-- ;
        },
        handleAdd:function(index){
            this.list[index].count++ ;

        },
        handleRemove:function(index){
            this.list.splice(index,1) ;
        },
        
    },
    computed:{
        totalPrice:function(){
            var total = 0 ;
            for(var i = 0; i<this.list.length ;i++){
                var item = this.list[i] ;
                total +=item.price * item.count ;
            }
            return total.toString().replace(/\B(?=(\d{3})+$)/g,',') ;
        }
    }
})

運行效果:


最后推薦一本書籍《Vue.js實戰》,這本書籍配合官方文檔入手會避免很多坑

參考資料

vue官方文檔

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容