Vue模板语法 mustache语法列表渲染 事件

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

指令

  1. Vue 指令
    • 作用: 是用来操作DOM的,指令就是绑定在DOM身上的一个属性,这个属性具备一定的功能,这个功能用来操作DOM
    • 以后我们不在像以前一样,先获取DOM,在操作了,我们现在可以直接使用指令来操作DOM
    • 这个指令需要模板语法的支持,所以我们采用jsx语法糖
  2. Vue 组件

模板语法

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

指令的具体展示

  1. 格式:
    • v-xxx = "mustache语法"
    • v-xxx = "msg"
    • v-xxx = "{{msg}}" 错误的
  2. v-html 将一个数据展示在一个DOM内容中, innerHTML( html属性 )
    • 防止脚本攻击 xss CSRF
  3. v-bind
    • 使用技巧: 凡是 DOM 的属性要和数据进行绑定,那么我们就可以使用 v-bind
    • 格式: v-bind:DOMattr = "data"
    • 简写: :DOMattr = "data"
  4. v-text 非转义输出,v-html 转义输出
<p v-html="a"></p><p v-text="a"></p>
new Vue({
el:'#app',
data:{
a:'<em>hello,world</em>'
}
})
输出的结果:
hello,world
<em>hello,world</em>
  1. class vs style
    1. 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>
  1. style
  • object写法
<div :style = "{width:'100px',height: '100px',background: 'blue'}"></div><div :style = "style"></div>
  • arr写法
<div :style = "[style,border]"></div>
  1. 条件渲染 v-if && v-show
    1. 条件渲染有两个指令, 一个是 v-if , 另外一个是 v-show
    2. v-if 有三种使用形式
      • 单路分支
      • 双路分支
      • 多路分支
<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>
new Vue({
el: '#app',
data: {
msg: 'hello 下午到了',
flag: false,
type: 'A'
}
})
  1. v-show
<div id="app"><h3> v-show </h3><p v-show = "flag"> 千锋教育 </p></div>
new Vue({
el: '#app',
data: {
msg: 'hello vue.js',
flag: false
}
})
  1. v-if vs v-show
    • v-if 如果值为false,那么绑定的DOM就会被销毁
    • v-if 操作的是一个DOM的生成和销毁
    • 如果v-if的初始值时false,那么绑定元素是否会渲染呢?
      答:v-if如果是false,那么这个DOM元素是不会渲染的
    • v-show 操作的是一个DOM的dispay样式属性
    • 如果v-show的初始值是false,那么这个绑定的DOM元素是否会渲染呢?
      答:v-show不管值是什么,它都会渲染出来
    • 总结:一般来说,v-if 有更高的切换开销,而 v-show 有更高的初始渲染开销。因此,如果需要非常频繁地切换,则使用 v-show 较好;如果在运行时条件很少改变,则使用 v-if 较好。
    • template:相当于虚拟DOM
      • template标签如果放在模板的范围内使用,那么将来不会被解析渲染
<div id="app"><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>
new Vue({
el: '#app',
data: {
msg: 'hello vue.js',
flag: false
}
})
  1. 列表渲染:
    v-for 是用来做列表渲染的
    1. 格式
      v-for = " xxx in(of) data "
      举例:
      v-for = " item in(of) todos "
<div id="app"><h3> num </h3><ul><li v-for = " n in num "> {{ n }} </li></ul><h3> string </h3><ul><li v-for = " s of str"> {{ s }} </li></ul><h3> arr </h3><ul><li v-for = " item in arr "> {{ item }} </li></ul></div>
new Vue({
el: '#app',
data: {
msg: 'hello vue.js',
num: 10,
str: 'I hate you ~~',
arr: [ 1,2,3,4 ],
})
  1. 带参数的格式
    v-for = " (item,index) in todos "
<div id="app"><h3> arr - v-for 带参数的 </h3><ul><li v-for = "( item , index) in arr "><p> item -- {{ item }} </p><p> index ---{{ index }} </p></li></ul><h3> object </h3><ul><li v-for = " item in obj "> {{ item }} </li></ul><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></div>
new Vue({
el: '#app',
data: {
msg: 'hello vue.js',
num: 10,
str: 'I hate you ~~',
arr: [ 1,2,3,4 ],
obj: {
id: 1,
name: 'zyz',
age: 18
}
})
  1. key( 留一部分 )
    • 每次列表循环的后面都要绑定一个key,是为了进行DOM的唯一标识,这样就不会让vue因为惰性而影响列表的正常渲染
    • 理想的key是使用数据中的id
  2. 数据的更新检测
    • 使用以下方法操作数组,可以检测变动:
      push() pop() shift() unshift() splice() sort() reverse()
    • 新数组替换旧数组:
      filter(), concat() 和 slice() ,map()
    • 不能检测以下变动的数组:
        vm.items[indexOfItem] = newValue
                解决  (1)Vue.set(example1.items, indexOfItem, newValue)
      vm.items.length = 0
                解决   (1)splice
<div id="app"><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><h3> 新数组 computed - filter </h3><ul><li v-for = " item in newJson" :key = "item.id">
{{ item.text }}
</li></ul></div>
new Vue({
el: '#app',
data: {
json: [
'aa',
{
id: 1,
text: '睡觉'
},
{
id: 2,
text: '敲代码'
}
]
},
methods: {
//这里面存放的都是事件的处理程序
add () {
this.json.push({
id: 3,
text: '打篮球'
})
},
notChange () {
// this.json[0] = '做作业'// Vue.set(this.json,0,'做作业')this.$set(this.json,0,'做作业')
},
clear () {
// this.json.length = 0this.json.splice(0,this.json.length)
}
}
})
  1. 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)
}
}
})
  1. computed 计算属性
    • 计算属性中存放的也是方法
    • 计算属性的方法中必须要有返回值
    • 计算属性的方法名可以像data选项中定义的数据一样使用
computed: { // 计算属性// 这里存放的也是方法,但是这个方法是有返回值的,并且方法名还可以当做一个变量(相当于直接在data里面定义的数据)来使用
newJson () {
return this.json.filter( item => {
return item.id>1
})
}
}

事件

  1. 指令v-on
  2. 格式
    v-on:eventType = "事件处理程序名称"
  3. 简写
    @eventType = '事件处理程序名称'
<div id="app"><h3> 普通事件 </h3><button v-on:click = 'normalHandler'> 普通事件 </button><button @click = 'normalHandler'> 普通事件-简写 </button><h3> 事件对象 </h3><button @click = "eventHandler"> 事件对象 </button><h3> 事件传参 </h3><button @click = "arguHandler( 10,20 )"> 事件传参 </button><button @click = "arguHandler( a,b )"> 事件传参 </button><button @click = "argu_event_handler( a,b,$event )"> 事件传参 - 事件对象 </button></div>
//如果我的参数中需要事件对象//解决: 在方法调用的时候,使用一个叫做$event的作为实际参数new Vue({
el: '#app',
data: {
a: 10,
b: 20
},
methods: {
normalHandler () {
alert('普通事件')
},
eventHandler ( e ) {
console.log( e )
},
arguHandler ( a, b ) {
alert( a + b )
},
argu_event_handler ( a,b,e) {
console.log( e )
console.log( a + b )
}
}
})
  1. 事件的修饰符
    1. 格式: v-on:click.xxx = 'handler'
    • xxx指的是修饰符的名称
    1. 修饰符的种类:
      • .stop:阻止冒泡
      • .prevent:阻止浏览器的默认行为
      • .capture:事件捕获
      • .self:阻止冒泡
      • .once:事件只触发一次
      • .passive:滚轮事件不会立即触发,会等待onScoll完成再触发
    2. 问题: 修饰符使用有什么好处?
      • 举例: 事件冒泡案例
      • 分析: 发现 e.stopPropagation() 在每一个方法中都要使用,这个时候发现代码很冗余
      • 解决: 事件修饰符
<div id="app"><div class="big" @click.stop = "bigHandler"><div class="middle" @click.stop = "middleHandler"><div class="small" @click.stop = "smallHandler"></div></div></div><hr><div class="big" @click.self = "bigHandler"><div class="middle" @click.self = "middleHandler"><div class="small" @click.self = "smallHandler"></div></div></div></div>
new Vue({
el: '#app',
methods: {
bigHandler (e) {
// e.stopPropagation()
alert('big')
},
middleHandler (e) {
// e.stopPropagation()
alert('middle')
},
smallHandler (e) {
// e.stopPropagation()
alert( 'small' )
}
}
})

猜你喜欢

转载自www.cnblogs.com/zhaoyingzi/p/10913392.html
今日推荐