vue2.x 5分钟上手

作者: tanoak | 来源:发表于2018-10-06 16:18 被阅读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" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <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官方文档

相关文章

  • 2019-03-09 Vue基础知识

    Vue2.x 模板语法,条件渲染,列表渲染渲染 Vue2.x Router,Vuex 集成Vue2.x Vue2....

  • VUE相关

    中文官网地址 vue2.x菜鸟教程 vue2.x

  • vue2.x 5分钟上手

    安装 安装vue-cli 这是vue的脚手架 创建项目 下载依赖 运行 打包 常用指令 [v-bind和v-on]...

  • vue学习(59)计算属性和监视

    知识点 computed函数与Vue2.x中computed配置功能一致写法 watch 与Vue2.x中watc...

  • vue2.x plugin vue2-touch

    今天小编想和大家分享一下Vue2.x的手势插件--简单来说就是Hammerjs的vue2.x plugin的封装。...

  • VUE的通信方式

    前言 本章总结了vue2.x与vue3.x的通信方式 VUE2.x的通信方式 props传递数据 通过 $emit...

  • 一款轻量级vue2.x日历组件

    一款轻量级vue2.x日历组件 vue2-date-component 一款基于vue2.x日期时间选择组件项目地...

  • Vue3

    Vue3.0的优势 性能比Vue2.x快1.2~2倍 按需编译,体积比Vue2.x更小 组合API(类似React...

  • (八)观察者 - watch

    本章将介绍的是Vue3.x中的观察者watch,区别于Vue2.x,采用了函数的模式实现 1、概述:Vue2.x中...

  • vue2.x学习(一)

    出于各方面考虑,公司前端技术选型现在从angular1.x迁移到vue2.x。所以现在打算学习一下vue2.x。从...

网友评论

    本文标题:vue2.x 5分钟上手

    本文链接:https://www.haomeiwen.com/subject/rlthsftx.html