vue提供了很多的指令,使用这些指令能够更加方便我们进行编程
v-text
- v-text 能够整体更新节点的内容,如果需要部分更新则需要用{{textContent}}插入
<template>
<div>
<span v-text="msg" ></span>
|| 上下两种写法作用相同
<span>{{msg}}</span>
</div>
</template>
<script>
export default{
data(){
return {
msg:"hello word"
}
}
}
</script>
v-html
- v-html更新元素的
innerHTML
。注意:内容按普通 HTML 插入 - 不会作为 Vue 模板进行编译 。如果试图使用v-html
组合模板,可以重新考虑是否通过使用组件来替代。
v-bind
- v-bind通常缩写为:
- 动态地绑定一个或多个特性,或一个组件 prop 到表达式
- 修饰符:
.prop
- 作为一个 DOM property 绑定而不是作为 attribute 绑定。(差别在哪里?).camel
- (2.1.0+) 将 kebab-case 特性名转换为 camelCase. (从 2.1.0 开始支持).sync
(2.3.0+) 语法糖,会扩展成一个更新父组件绑定值的v-on
侦听器。
<template>
<div>
<!-- 动态绑定属性 -->
<input :placeholder="msg" :type="type" />
<button @click="onClick" >
点击更改
</button>
<!-- 动态绑定css -->
<p :style="{fontSize:size+'px'}" ></p>
<!-- 通过 prop 修饰符绑定 DOM 属性 -->
<div v-bind:text-content.prop="text"></div>
</div>
</template>
<script>
export default{
data(){
return{
msg:"我是一个动态的值",
type:"text",
size:"16",
text:"hello word"
}
},
methods:{
//点击更改属性
onClick(){
this.msg = "我是更改过的值"
this.type = "password",
this.size = "30"
this.text = "hello vue"
}
}
}
</script>
v-show
- 根据表达式的true和false,切换元素的display的block和none
<template>
<div>
<div v-show="ok" >{{msg}}</div>
<button @click="onClick" >
{{txt}}
</button>
</div>
</template>
<script>
export default{
data(){
return{
ok:true,
msg:"hello vue",
txt:"点击隐藏"
}
},
methods:{
onClick(){
this.txt = "点击显示",
this.ok = !this.ok
}
}
}
</script>
v-if
根据表达式的值的 truthiness 来有条件地渲染元素。在切换时元素及它的数据绑定 / 组件被销毁并重建。如果元素是 <template>
,将提出它的内容作为条件块。
当条件变化时该指令触发过渡效果
- v-if和v-show的用法大致相同
- v-if是判断是否存在dom元素 v-show是判断显示隐藏dom
v-else
<template>
<div>
<!-- 这个key值必须绑定 -->
<div v-for="(item,index) in arr" :key="index" >
{{item}}
</div>
</div>
</template>
v-on
-
缩写@
-
修饰符:
.stop
- 调用event.stopPropagation()
。.prevent
- 调用event.preventDefault()
。.capture
- 添加事件侦听器时使用 capture 模式。.self
- 只当事件是从侦听器绑定的元素本身触发时才触发回调。.{keyCode | keyAlias}
- 只当事件是从特定键触发时才触发回调。.native
- 监听组件根元素的原生事件。.once
- 只触发一次回调。.left
- (2.2.0) 只当点击鼠标左键时触发。.right
- (2.2.0) 只当点击鼠标右键时触发。.middle
- (2.2.0) 只当点击鼠标中键时触发。.passive
- (2.3.0) 以{ passive: true }
模式添加侦听器
-
用法:
绑定事件监听器。事件类型由参数指定。表达式可以是一个方法的名字或一个内联语句,如果没有修饰符也可以省略。
用在普通元素上时,只能监听原生 DOM 事件。用在自定义元素组件上时,也可以监听子组件触发的自定义事件。
在监听原生 DOM 事件时,方法以事件为唯一的参数。如果使用内联语句,语句可以访问一个
$event
属性:v-on:click="handle('ok', $event)"
。从
2.4.0
开始,v-on
同样支持不带参数绑定一个事件/监听器键值对的对象。注意当使用对象语法时,是不支持任何修饰器的。
<template>
<div>
<!-- 方法处理器 -->
<button v-on:click="doThis"></button>
<!-- 动态事件 (2.6.0+) -->
<button v-on:[event]="doThis"></button>
<!-- 内联语句 -->
<button v-on:click="doThat('hello', $event)"></button>
<!-- 缩写 -->
<button @click="doThis"></button>
<!-- 动态事件缩写 (2.6.0+) -->
<button @[event]="doThis"></button>
<!-- 停止冒泡 -->
<button @click.stop="doThis"></button>
<!-- 阻止默认行为 -->
<button @click.prevent="doThis"></button>
<!-- 阻止默认行为,没有表达式 -->
<form @submit.prevent></form>
<!-- 串联修饰符 -->
<button @click.stop.prevent="doThis"></button>
<!-- 键修饰符,键别名 -->
<input @keyup.enter="onEnter">
<!-- 键修饰符,键代码 -->
<input @keyup.13="onEnter">
<!-- 点击回调只会触发一次 -->
<button v-on:click.once="doThis"></button>
<!-- 对象语法 (2.4.0+) -->
<button v-on="{ mousedown: doThis, mouseup: doThat }"></button>
</div>
</template>
<script>
</script>
v-model
<template>
<div>
<!-- 双向数据绑定表单元素内容 -->
<input type="text" v-model="text">
<!-- 当使用.lazy修饰符时,相当于双向数据绑定失效了 span中的内容不会改变 -->
<input type="text" v-model.lazy="text">
<span>{{text}}</span>
<!-- 输入字符串转为数字 -->
<input type="text" v-model.number="text">
<!-- 输入首尾空格过滤 -->
<input type="text" v-model.trim="text">
</div>
</template>
v-slot
- 缩写#
- 示例
父组件
<template>
<div>
<Child>
<span>你好</span>
</Child>
</div>
</template>
<script>
import Child from "./Child"
</script>
子组件
<template>
<div>
<slot></slot>
</div>
</template>
上面例子中, 在父组件使用的子组件标签中加入了一个span标签,正常情况下这个标签是不会显示的。
但是在子组件中使用了插槽,这个内容就会正常显示了。
有时候我们会用到多个插槽,这时候就用到了具名插槽
<template>
<div>
<!-- 父组件 -->
<Child>
<template v-slot:header>
<h3>这是header部分</h3>
</template>
<template v-slot:footer>
<h3>这是footer部分</h3>
</template>
</Child>
</div>
</template>
<script>
import Child from "./Child"
</script>
<template>
<div>
<!-- 子组件 -->
<slot name="header"></slot>
<slot name="footer"></slot>
</div>
</template>
<script>
</script>
作用域插槽可以让插槽内容访问子组件的数据
<template>
<div>
<Child>
<template v-slot:default="user" >
</template>
</Child>
</div>
</template>
<script>
import Child from "./Child"
</script>
<template>
<div>
<slot v-bind:user="user" ></slot>
</div>
</template>