模板语法 mustache语法 双大括号语法指令 Vue指令 组件 作用: 是用来操作DOM的,指令就是绑定在DOM身上的一个属性,这个属性具备一定的功能,这个功能用来操作DOM,以后我们不在像以前一样,先获取DOM,在操作了,我们现在可以直接使用指令来操作DOM
这个指令需要模板语法的支持,所以我们采用jsx语法糖
模板语法 模板语法支持性还是很高的,数据类型都是支持的,但是不支持 输出语法 ( console.log alert )指令
格式: v-xxx = “mustache语法” v-xxx = “msg” v-xxx = “{{msg}}” ×
v-html 将一个数据展示在一个DOM内容中, innerHTML( html属性 )
防止脚本攻击 xss CSRF <h3> v-html </h3> <p v-html = "msg" style="" > </p> new Vue({ el: '#app', data: { msg: 'hello Vue.js', val: '你好vue', img: 'https://www.baidu.com/img/dong_96c3c31cae66e61ed02644d732fcd5f8.gif', a: " <strong> 你好vue </strong> " } })v-bind 单项数据绑定
使用技巧: 凡是 DOM 的属性要和数据进行绑定,那么我们就可以使用 v-bind格式: v-bind:DOMattr = “data”简写: :DOMattr = “data” <h3> v-bind 单项数据绑定 </h3> <input type="text" v-bind:value = "val"/> <input type="text" :value = "val"/> <img :src="img" alt=""> new Vue({ el: '#app', data: { msg: 'hello Vue.js', val: '你好vue', img: 'https://www.baidu.com/img/dong_96c3c31cae66e61ed02644d732fcd5f8.gif', a: " <strong> 你好vue </strong> " } }) v-text 非转义输出 <h3> v-text </h3> <p v-text = "msg"></p> new Vue({ el: '#app', data: { msg: 'hello Vue.js', val: '你好vue', img: 'https://www.baidu.com/img/dong_96c3c31cae66e61ed02644d732fcd5f8.gif', a: " <strong> 你好vue </strong> " } }) class vs styleclass object <div :class = "{[size]:true,[color]: true,[box]: true}"></div> <div :class = "{[size]: 5>2?true:false,[color]: true,[box]: true}"></div> arr <div :class = "[size,box,color]"></div> <div :class = "[class_flag?size:'',box,color]"></div> style object <div :style = "{width:'100px',height: '100px',background: 'blue'}"></div> <div :style = "style"></div> arr <div :style = "[style,border]"></div> <div id="app"> <h3> 类名 </h3> <div class="box box_size box_color"> 你好vue </div> <h3> vue中类名的使用有两种形式 </h3> <hr> <h3> vue - class - 对象形式 </h3> <div :class="{[size]:true,[color]: true,[box]: true}"></div> <div :class="{[size]: 5>2?true:false,[color]: true,[box]: true}"></div> <hr> <h3> vue -class - arr </h3> <div :class="[size,box,color]"></div> <div :class="[class_flag?size:'',box,color]"></div> </div> new Vue({ el: '#app', data: { msg: 'hello hello', size: 'box_size', color: 'box_color', box: 'box', class_flag: true } }) 条件渲染 v-if && v-show条件渲染有两个指令, 一个是 v-if , 另外一个是 v-show * v-if 有三种使用形式
1. v-if 如果值为false,那么绑定的DOM就会被销毁 2. v-if 操作的是一个DOM的生成和销毁 3. 如果v-if的初始值时false,那么绑定元素是否会渲染呢? v-if如果是false,那么这个DOM元素是不会渲染的 单路分支 <h3> v-if 单路</h3> <p v-if = "flag"> 单路分支 </p> new Vue({ el: '#app', data: { msg: 'hello hello', flag: false, type: 'A' } }) 双路分支 <h3> v-if 双路 </h3> <p v-if = "flag"> 双路1 </p> <p v-else> 双路2 </p> new Vue({ el: '#app', data: { msg: 'hello hello', flag: false, type: 'A' } }) 多路分支 <h3> v-if 多路 </h3> <p v-if = "type === 'A' "> A </p> <p v-else-if = " type === 'B'"> B </p> <p v-else> C </p> new Vue({ el: '#app', data: { msg: 'hello hello', flag: false, type: 'A' } })v-show
v-show 操作的是一个DOM的dispay样式属性如果v-show的初始值是false,那么这个绑定的DOM元素是否会渲染呢? v-show不管值是什么,它都会渲染出来 <h3> v-show </h3> <p v-show = "flag"> 你好vue </p>v-if vs v-show 一般来说,v-if 有更高的切换开销,而 v-show 有更高的初始渲染开销。因此,如果需要非常频繁地切换,则使用 v-show 较好;如果在运行时条件很少改变,则使用 v-if 较好。 * template template标签如果放在模板的范围内使用,那么将来不会被解析渲染
<template v-if = 'flag'> <div class="box" > <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> </ul> </div> </template></div><template> 123 </template> new Vue({ el: '#app', data: { msg: 'hello vue.js', flag: false } })列表渲染 v-for 是用来做列表渲染的
格式 v-for = " xxx in(of) data " 举例: v-for = " item in(of) todos "带参数的格式 v-for = " (item,index) in todos " <h3> num </h3> <ul> <li v-for = " n in num "> {{ n }} </li> </ul> <hr> <h3> string </h3> <ul> <li v-for = " s of str"> {{ s }} </li> </ul> <hr> <h3> arr </h3> <ul> <li v-for = " item in arr "> {{ item }} </li> </ul> <h3> arr - v-for 带参数的 </h3> <ul> <li v-for = "( item , index) in arr "> <p> item -- {{ item }} </p> <p> index ---{{ index }} </p> </li> </ul> <hr> <h3> object </h3> <ul> <li v-for = " item in obj "> {{ item }} </li> </ul> key 每次列表循环的后面都要绑定一个key,是为了进行DOM的唯一标识,这样就不会让vue因为惰性而影响列表的正常渲染理想的key是使用数据中的id <h3> object-v-for 带参数 </h3> <ul> <li v-for = " (item,key) in obj "> <p> item -- {{ item }} </p> <p> key -- {{ key }} </p> </li> </ul> <h3> object-v-for 带三个参数 </h3> <ul> <li v-for = " (item,key,index) in obj "> <p> item -- {{ item }} </p> <p> key -- {{ key }} </p> <p> index -- {{ index }} </p> </li> </ul>数据的更新检测 a. 使用以下方法操作数组,可以检测变动 push() pop() shift() unshift() splice() sort() reverse() b. filter(), concat() 和 slice() ,map(),新数组替换旧数组 c. 不能检测以下变动的数组 vm.items[indexOfItem] = newValue 解决 (1)Vue.set(example1.items, indexOfItem, newValue) vm.items.length = 0 解决 (1)splice
<h3> json </h3> <button v-on:click = "add"> 添加 </button> <button v-on:click = "notChange"> 不能检测的 </button> <button v-on:click = "clear"> 清空一个数组 </button> <ul> <li v-for = "item in json"> <ul> <li v-for = "(ele,index) in item" :key = "index"> {{ ele }} </li> </ul> </li> </ul> <hr> <h3> 新数组 computed - filter </h3> <ul> <li v-for = " item in newJson" :key = "item.id"> {{ item.text }} </li> </ul> </div> methods 方法事件的添加使用的是 v-on:eventType = ‘事件处理程序’
事件处理程序往options里面的methods配置项中书写 <button v-on:click = "add"> + </button> new Vue({ el: '#app', data: { arr: [1,2,3,4] }, methods: { add () { this.arr.push(5) } } })computed 计算属性
计算属性中存放的也是方法
计算属性的方法中必须要有返回值
计算属性的方法名可以像data选项中定义的数据一样使用
