vue(2)-----指令(1)

    xiaoxiao2022-07-14  128

    指令

    vue分为指令和组件。指令的作用:是用来操作DOM的,指令就是绑定在DOM身上的一个属性,这个属性具备一定的功能。这个功能用来操作DNM。可以直接使用指令来操作DOM。这个指令需要模板语法的支持,采用jsx语法糖。

    模板语法 mustache语法 双大括号语法

    模板语法的支持程度还是很高的,数据类型都支持,但是不支持(console.log alert)。

    <!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>Document</title> </head> <body> <div id="app"> <p>{{msg}}</p> <p>{{str}}</p> <p>{{num}}</p> <p>{{nul&&'false'||'true'}}</p> <p>{{undefined&&'你的'||'WODE'}}</p> <P>{{arr[1]}}</P> <p>{{obg.name}}</p> <p>{{(function(){return '1+3333'})()}}</p> <p>{{msg.split('').reverse().join('')}}</p> </div> </body> <script src="../lib/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { msg: '你好', str: 'str', num: 4, nul: null, undefined: undefined, arr: [1, 2, 3, 4], obg: { name: 'st', } } }) </script> </html>

    指令

    格式:

    v-xxx = “mustache语法”v-xxx = “msg”v-xxx = “{{msg}}” ×

    1. v-html 和 v-text

    v-html 将一个数据展示在一个DOM内容中。防止脚本攻击 xss CSRF。v-text 非转义输出。 <!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>Document</title> </head> <body> <div id="app"> <p v-html='msg'>123123</p> <input type="text" :value='val'> <input type="text" v-bind:value='val'> <p v-html='a'></p> <p v-text='a'></p> </div> </body> <script src="../lib/vue.js"></script> <script> new Vue({ el: '#app', data: { msg: 'hello vue', val: '王某抽烟被拍摄了', a: '<strong>加粗了</string>' } }) </script> </html>

    2. v-bind 单项数据绑定

    使用技巧:凡是DOM的属性要和数据进行绑定,那么我们就可以使用v-bind格式:v-bind:DOMattr = “data”简写:DOMarrt = “data” <!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>Document</title> </head> <body> <div id="app"> <h3> v-bind 单项数据绑定 </h3> <input type="text" v-bind:value = "val"/> <input type="text" :value = "val"/> <img :src="img" alt=""> </div> </body> <script src="../lib/vue.js"></script> <script> new Vue({ el: '#app', data: { msg: 'hello Vue.js', val: '你是真的皮哦', img: 'https://www.baidu.com/img/dong_96c3c31cae66e61ed02644d732fcd5f8.gif', } }) </script> </html>

    3. class vs style

    class 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> 推荐使用arr形式 style - object ```html <div :style = "{width:'100px',height: '100px',background: 'blue'}"></div> <div :style = "style"></div> ``` - arr ```html <div :style = "[style,border]"></div> ```

    4. 条件渲染 v-if vs v-show

    v-if 有三种使用形式

    单路分支双路分支多路分支 v-if如果值为false,那么绑定的DOM就会被销毁。v-if 操作的是一个DOM的生成和销毁。v-if如果是false,那么这个DOM元素是不会渲染的。template标签如果放在模板的范围内使用,那么将来不会被解析渲染。 <!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>Document</title> <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script> </head> <body> <div id="app"> <h3> v-if 单路</h3> <p v-if = "flag"> 单路分支 </p> <h3> v-if 双路 </h3> <p v-if = "flag"> 双路1 </p> <p v-else> 双路2 </p> <h3> v-if 多路 </h3> <p v-if = "type === 'A' "> A </p> <p v-else-if = " type === 'B'"> B </p> <p v-else> C </p> </div> </body> <script> new Vue({ el: '#app', data: { msg: 'hello 下午到了', flag: false, type: 'A' } }) </script> </html> v-show v-show 操作的是一个DOM的dispay样式属性如果v-show的初始值是false,那么这个绑定的DOM元素是否会渲染呢? v-show不管值是什么,它都会渲染出来。 <!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>Document</title> </head> <body> <div id="app"> <ul> <li v-show="flag">1</li> <li v-show='flag'>2上面的兄弟为何不显示</li> <template v-show='flag'> <li>3</li> <li>4</li> </template> <li>5</li> <li>6</li> </ul> </div> </body> <script src="../lib/vue.js"></script> <script> new Vue({ el: '#app', data: { flag: false, } }) </script> </html> v-if vs v-show区别一般来说,v-if 有更高的切换开销,而 v-show 有更高的初始渲染开销。因此,如果需要非常频繁地切换,则使用 v-show 较好;如果在运行时条件很少改变,则使用 v-if 较好。v-show 不支持 元素,也不支持 v-else。

    5. 列表渲染

    v-for 是用来做列表渲染的

    格式 v-for = " xxx in(of) data " 举例: v-for = " item in(of) todos "

    带参数的格式 v-for = " (item,index) in todos "

    key( 留一部分 )

    每次列表循环的后面都要绑定一个key,是为了进行DOM的唯一标识,这样就不会让vue因为惰性而影响列表的正常渲染。

    理想的key是使用数据中的id。

    <!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>Document</title> </head> <body> <div id="app"> <ul> <li v-for='s in str'>{{s}}</li> </ul> <ul> <li v-for='n of num'>{{n}}</li> </ul> <ul> <li v-for='(item,index) in arr'>{{index}}{{item}}</li> </ul> <ul> <li v-for='(item,key,index) in obj'>{{index}}{{key}}:{{item}}</li> </ul> <button v-on:click='add'>添加</button> <ul> <li v-for='item in json'> <ul> <li v-for='vsa in item'> {{vsa}} </li> </ul> </li> </ul> </div> </body> <script src="../lib/vue.js"></script> <script> new Vue({ el: '#app', data: { str: 'hello 你是真的皮哦', num: 5, arr: [1, 2, 4, 6, 8], obj: { id: 1, name: 'st', age: 22, }, json: [{ id: 1, text: '冲冲冲' }, { id: 2, text: '滚滚滚' }, { id: 3, text: '哎哎哎' }] }, methods: { add() { this.json.push({ id: 3, text: '打篮球', }) }, notChange() { this.$set(this.json, 0, '做作业'); }, clear() { this.json.splice(0, this.json.length); } }, computed: { newJson() { return this.json.filter(item => { return item.id > 1; }) } } }) </script> </html>

    数据的更新检测

    使用以下方法操作数组,可以检测变动。 push()pop()shift()unshift()splice()sort()reverse() 新数组替换旧数组 filter()concat()slice()map() <h3> 新数组 computed - filter </h3> <ul> <li v-for = " item in newJson" :key = "item.id"> {{ item.text }} </li> </ul> computed: { newJson () { return this.json.filter( item => { return item.id>1 }) } } 不能检测以下变动的数组 vm.items[indexOfItem]=newValue解决 (1)Vue.set(example1.items,indexOfItem,newValue)vm.items.length = 0解决(1)splice notChange () { this.$set(this.json,0,'做作业') }, clear () { // this.json.length = 0 this.json.splice(0,this.json.length) }

    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选项中定义的数据一样使用
    最新回复(0)