【VueJs入门】
一、上手步骤
vue.js和jquery一样,只要引入vue.js就可以了。
1、创建一个Vue实例: 先引入Vue.js文件,然后new一个Vue的实例即可。如下面的代码,通过<script src="./vue.js"></script>引入,然后在<script>标签中创建实例
2、挂载 在Vue的实例里,通过传递的el属性
3、单向/双向绑定 data属性里的变量 = html标签里的{{}} = input标签里的v-model
<!DOCTYPE html> <html lang="en"> <script src="./vue.js"></script> <body> <div id="app"> <input v-model="test"/> {{test}} </div> <script> new Vue({ //创建一个Vue的实例 el: "#app", //挂载点是id="app"的地方 data: { //数据 test: "abc" //变量test } }) </script> </body> </html>
二、案例(DEMO控制)
案例说明:
1. 单个dom的添加/删除/显示/隐藏——变量控
2. 多个在显示上互斥的dom(如登录时的提示)—— 用1个变量去控制
3. 多个同类型dom(用v-for来动态生成)
代码:
1 <html lang="en"> 2 <script src="./vue.js"></script> 3 <body> 4 <div id="app"> 5 <p> 6 点击按钮切换显示的图标 7 </p> 8 <input type="button" v-on:click="set1" v-if="data1" value="【一个图标】"/> 9 <input type="button" v-on:click="set1" v-if="!data1" value="另外一个图标"/> 10 <p>————————————————————————————</p> 11 <p>点击不同按钮决定显示什么</p> 12 <input type="button" @click="set2('baidu')" value="百度"> 13 <input type="button" @click="set2('qq')" value="腾讯"> 14 <input type="button" @click="set2('taobao')" value="淘宝"> 15 <button @click="set2">什么都不显示</button> 16 <p> 17 <a href="http://www.baidu.com" target="_blank" v-if="data2=='baidu'">百度</a> 18 <a href="http://www.qq.com" target="_blank" v-if="data2=='qq'">腾讯</a> 19 <a href="http://www.taobao.com" target="_blank" v-if="data2=='taobao'">淘宝</a> 20 </p> 21 <p>————————————————————————————</p> 22 <p> 23 同类型dom,典型的是表格,类似的有li。<br> 24 <button @click="set3">点击添加内容</button> 25 </p> 26 <ul> 27 <li v-for="i in data3">{{i}}</li> 28 </ul> 29 </div> 30 <script> 31 new Vue({ //创建一个Vue的实例 32 el: "#app", //挂载点是id="app"的地方 33 data: { 34 data1: true, 35 data2: "", 36 data3: [1] 37 }, 38 methods: { 39 set1: function () { 40 this.data1 = !this.data1; 41 }, 42 set2: function (arg) { 43 this.data2 = arg; 44 }, 45 set3: function (arg) { 46 this.data3.push(this.data3.length + 1); 47 } 48 } 49 }) 50 </script> 51 </body>
三、案例(input相关)
微博的注册页面如下图
代码:
<!DOCTYPE html> <html lang="en"> <script src="./vue.js"></script> <body> <div id="app"> <p> 邮箱:<input v-model="mail"/> </p> <p> 设置密码:<input type="password" v-model="pw"/> </p> <p> 官方注册微博名:<input v-model="name" placeholder="请参考组织/企业/品牌名称"/> </p> <p> 所在地: <select v-model="province"> <option value="zhejiang">浙江</option> <option value="shanghai">上海</option> </select> <select v-model="city"> <option v-for="(val,key) in citys" v-bind:value="key">{{val}}</option> </select> </p> <p> 验证码: <input v-model="verificationCode"> 请输入:1234 </p> <p> <input type="button" v-on:click="check" value="提交"/> <input type="button" v-on:click="inputDefault" value="默认值"/> </p> <p style="color:green" v-if="error=='success'">提交成功</p> <p style="color:red" v-if="error=='less'">缺少内容</p> <p style="color:red" v-if="error=='VerificationCode'">验证码错误</p> </div> <script> new Vue({ //创建一个Vue的实例 el: "#app", //挂载点是id="app"的地方 created: function () { this.changeProvince(); }, data: { //数据 province: "zhejiang", mail: "", pw: "", name: "", city: "", citys: {}, provinceWithCity: { zhejiang: { hangzhou: "杭州", shaoxing: "绍兴" }, shanghai: { pudong: "浦东区", jingan: "静安区" } }, verificationCode: "", error: "" }, methods: { changeProvince: function () { this.citys = this.provinceWithCity['zhejiang']; this.$watch('province', function (newVal, oldVal) { this.citys = this.provinceWithCity[newVal]; }) }, check: function () { //提交内容检查 if (this.mail && this.pw && this.name && this.province && this.city) { if (this.verificationCode === '1234') { this.error = 'success'; console.log([this.mail, this.pw, this.name, this.province, this.city]); } else { this.error = 'VerificationCode' } } else { this.error = 'less'; } }, inputDefault: function () { this.mail = '123@qq.com'; this.pw = '123'; this.name = 'abc'; this.province = 'zhejiang'; this.city = 'hangzhou'; this.verificationCode = '1234'; } } }) </script> </body> </html>
四、案例(表格相关)
1.表格的核心特点是:类型重复的大量内容。
2.Vue非常擅长对表格的处理,只需要已知数据,预先设置好格式,即可自动生成数据。
3.常见表格需求是选择性显示(比如只显示符合条件的项),这点Vue也十分擅长,你只需要设置好条件,Vue在渲染的时候会自动帮你完成
<!DOCTYPE html> <html lang="en"> <script src="./vue.js"></script> <style> table { border-collapse: collapse; } th, td { border: 1px solid #000; } </style> <body> <div id="app"> <input type="button" v-on:click="randomData" value="随机生成数据"/> <input type="button" v-on:click="yearAbove20" v-if="yearLimit!==20" value="只显示年龄大于20的人"/> <input type="button" v-on:click="all" v-if="yearLimit>0" value="显示全部"/> <p></p> <table> <tr> <td>序号</td> <td>姓名</td> <td>年龄</td> </tr> <tr v-for="(val,key) in datas" v-if="val.year > yearLimit"> <td>{{val.index}}</td> <td>{{val.name}}</td> <td v-if="val.year<30">{{val.year}}</td> <td v-else>年龄太大了,不告诉你喔</td> </tr> </table> </div> <script> new Vue({ //创建一个Vue的实例 el: "#app", //挂载点是id="app"的地方 data: { datas: [], yearLimit: 0 }, methods: { getName: function () { var familyNames = new Array( "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许", "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎", "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常", "乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄", "和", "穆", "萧", "尹" ); var givenNames = new Array( "子璇", "淼", "国栋", "夫子", "瑞堂", "甜", "敏", "尚", "国贤", "贺祥", "晨涛", "昊轩", "易轩", "益辰", "益帆", "益冉", "瑾春", "瑾昆", "春齐", "杨", "文昊", "东东", "雄霖", "浩晨", "熙涵", "溶溶", "冰枫", "欣欣", "宜豪", "欣慧", "建政", "美欣", "淑慧", "文轩", "文杰", "欣源", "忠林", "榕润", "欣汝", "慧嘉", "新建", "建林", "亦菲", "林", "冰洁", "佳欣", "涵涵", "禹辰", "淳美", "泽惠", "伟洋", "涵越", "润丽", "翔", "淑华", "晶莹", "凌晶", "苒溪", "雨涵", "嘉怡", "佳毅", "子辰", "佳琪", "紫轩", "瑞辰", "昕蕊", "萌", "明远", "欣宜", "泽远", "欣怡", "佳怡", "佳惠", "晨茜", "晨璐", "运昊", "汝鑫", "淑君", "晶滢", "润莎", "榕汕", "佳钰", "佳玉", "晓庆", "一鸣", "语晨", "添池", "添昊", "雨泽", "雅晗", "雅涵", "清妍", "诗悦", "嘉乐", "晨涵", "天赫", "玥傲", "佳昊", "天昊", "萌萌", "若萌" ); var ran1 = parseInt(100 * Math.random()); var ran2 = parseInt(100 * Math.random()); return familyNames[ran1] + givenNames[ran2]; }, randomData: function () { var data = []; for (var i = 1; i < (5 + 10 * Math.random()); i++) { var obj = { index: i, name: this.getName(), year: parseInt(1 + Math.random() * 50) } data.push(obj) } this.datas = data; console.log(this.datas); }, yearAbove20: function () { this.yearLimit = 20; }, all: function () { this.yearLimit = -1; } } }) </script> </body> </html>
五、其它Vue常见功能
1、过滤器功能:
1. 主要用于文本转换;
2. 例如获得一个日期对象后,通过过滤器命令自动转为我们要求的日期格式。
3. {{ message | capitalize }} message变量被过滤器函数capitalize所处理
2、计算属性:
1. 更加高级的功能,可以视为过滤器功能的进阶版,适用的方向更多(不仅仅是文本)
2. 获取一个变量(输入内容)→通过计算函数转换→显示转换结果(输出内容)
3. 当输入内容变更时,输出内容也会自动随之变更
4. 利用ES5的getter和setter特性来实现,有缓存特点
3、$watch方法:
1. 监控变量,当变量改变时触发回调函数;
2. 例如之前的微博注册demo中,通过检测表示省份的变量的变化,来动态设置表示市的dom
4、class/style绑定:
1. 通过改变变量,来设置dom的样式的类,或者直接设置样式的属性 2. <div v-bind:class="{ active: isActive }"></div> 3. isActive值为true时,dom获得active这个类
5、事件监听:
1. 通过$emit触发事件和$on响应事件,只在当前Vue实例内有效,因此不会带来干扰;
2. 用起来非常舒服,适用于一对多和多对一的场景;
3. 跨组件响应(父子组件通信)时,可以使用global event bus来实现,或者使用插件实现
6、路由功能:
1. 简单来说,按需加载,而不是一次性全部加载;
2. 有官方推荐支持使用的的vue-router库;
通过本篇文章先全局了解下vue.js后期会继续学习。
参考
原文博客地址:https://blog.csdn.net/column/details/12809.html
非常感谢原文作者提供这么宝贵的文章。
想的太多,做的太少,中间的落差就是烦恼,要么去做,要么别想 中尉【6】