v-bind
指令描述:用于动态地将数据绑定到 HTML 元素的属性上,当数据发生变化时,其对应的属性值也会自动更新变化
语法:v-bind:attrName="expression"
or :attrName="expression"
attrName
为要绑定的属性名,expression
为要绑定的数据,它可以是任意的 Vue 表达式
1. 动态绑定元素属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <template > <a :href ="url" :title ="title" > {{title}}</a > <input type ="text" :disabled ="disabled" > </template > <script > export default { data ( ) { return { url : 'https://www.baidu.com' , title : '百度' , disabled : true } } } </script >
2. 动态绑定元素class, 对象语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <template > <div :class ="{content: isStat}" > Lorem ipsum dolor sit, amet consectetur adipisicing elit. Adipisci, fugit. Voluptatem pariatur incidunt nihil saepe perspiciatis est dolor dolorum, praesentium dignissimos tempore facere facilis sequi fugit asperiores vel aperiam maxime!</div > </template > <script > export default { data ( ) { return { isStat : true } } } </script > <style scoped > .content { font-size : 22px ; font-weight : bold; color : blueviolet } </style >
3. 动态绑定元素class, 数组语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <template > <div :class ="[className, 'style2']" > Lorem ipsum dolor sit, amet consectetur adipisicing elit. Adipisci, fugit. Voluptatem pariatur incidunt nihil saepe perspiciatis est dolor dolorum, praesentium dignissimos tempore facere facilis sequi fugit asperiores vel aperiam maxime!</div > </template > <script > export default { data ( ) { return { className : 'content' } } } </script > <style scoped > .content { font-size : 22px ; font-weight : bold; color : blueviolet } .style2 { border : 2px solid black; padding : 10px 15px ; background-color : chartreuse } </style >
4. 动态绑定元素style, 对象语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <template > <div :style ="content" > Lorem ipsum dolor sit, amet consectetur adipisicing elit. Adipisci, fugit. Voluptatem pariatur incidunt nihil saepe perspiciatis est dolor dolorum, praesentium dignissimos tempore facere facilis sequi fugit asperiores vel aperiam maxime!</div > </template > <script > export default { data ( ) { return { content : { fontSize : '22px' , fontWeight : 'bold' , color : 'blueviolet' } } } } </script >
5. 动态绑定元素style, 数组语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <template > <div :style ="[content, style2]" > Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptates enim architecto rem nostrum, aliquam corporis. Maiores vel magni exercitationem. Odit vitae dicta ipsam earum delectus aliquid velit ipsum vel deleniti?</div > </template > <script > export default { data ( ) { return { content : { fontSize : '22px' , fontWeight : 'bold' , color : 'blueviolet' }, style2 : { border : '2px solid black' , padding : '10px 15px' , backgroundColor : 'chartreuse' } } } } </script >
v-model
指令描述:用于实现表单元素( 如 input、textarea、select 等 )和 Vue 实例数据的双向绑定,它会根据表单的类型来动态的将表单输入的内容与 Vue 组件实例的数据进行双向绑定
语法:v-model="data"
data 为要绑定的数据,必须是响应式数据
例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <template > <h2 > 例1. text类型表单</h2 > <input type ="text" v-model.trim ="message" > <p > 您输入的内容是:{{ message }}</p > <h2 > 例2. radio类型表单</h2 > <p > 性别:{{ sex }}</p > <input type ="radio" name ="sex" value ="男" v-model ="sex" > 男 <input type ="radio" name ="sex" value ="女" v-model ="sex" > 女 <h2 > 例3. checkbox类型表单</h2 > <p > 喜好:{{ likes }}</p > <input type ="checkbox" name ="like" value ="篮球" v-model ="likes" > 篮球 <input type ="checkbox" name ="like" value ="足球" v-model ="likes" > 足球 <input type ="checkbox" name ="like" value ="乒乓球" v-model ="likes" > 乒乓球 </template > <script > export default { data ( ) { return { message : '' , sex : '男' , likes : ['乒乓球' ] } } } </script >
v-model
修饰符:
.lazy
失去焦点或按下回车键时才更新绑定数据
.number
输入字符串转换为有效的数字
.trim
过滤首尾的空格
v-on
指令描述:用于监听 DOM 事件,并在事件触发时执行指定的方法或表达式
语法:v-on:eventName="handler"
or @:eventName="handler"
eventName 为事件名,handler 为事件触发时要执行的函数或 Vue表达式
例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <template > <button @click ="handler" > Button-1</button > <button @click ="handler('Button-2 was clicked')" > Button-2</button > <button @click ="handler($event)" > Button-2</button > </template > <script > export default { methods : { handler (arg ) { console .log (arg); } } } </script >
另外,v-on
还支持修饰符,用来对事件进行额外的处理,如 阻止默认行为、阻止事件冒泡等
常用事件修饰符列表:
修饰符 描述 .prevent
阻止默认行为 .stop
阻止事件冒泡 .capture
事件在捕获阶段触发 .self
只当事件在该元素本身触发时才处理 .once
事件只执行一次 .passive
提升页面滚动性能 .left
按下键盘左键 .right
按下键盘右键 .up
按下键盘上键 .down
按下键盘下键 .enter
按下键盘 回车 键 .delete
按下键盘 Delete 键 .esc
按下键盘 ESC 键 .space
按下键盘 空格 键 .tab
按下键盘 Tab 键 .capsLock
按下键盘 CapsLock 键 .ctrl
组合键,按下键盘 Ctrl 键 + Other .alt
组合键,按下键盘 Alt 键 + Other .shift
组合键,按下键盘 Shift 键 + Other .meta
组合键,按下键盘 Win 键 + Other
例2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <template > <div class ="bg" @click ="alert1" > <a href ="https://www.timeic.top" @click.prevent.stop ="alert2" > click me!</a > </div > </template > <script > export default { methods : { alert1 ( ) { alert ('alert 1' ); }, alert2 ( ) { alert ('alert 2' ); } } } </script > <style scoped > .bg { background-color : tomato; } </style >
例3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <template > <input v-model ="text" type ="text" @keyup.ctrl.enter ="handler" > </template > <script > export default { data ( ) { return { text : '' } }, methods : { handler ( ) { alert (`您输入的是 '${this .text} '` ); } } } </script >
computed
计算属性Vue 的 computed 用于定义一些基于现有数据计算得出的属性。它可以将逻辑与模板分离,使代码更加简洁、易读、易于维护。同时计算属性会通过缓存和依赖跟踪机制提供响应式和高效的性能
特点:
缓存: 计算属性会根据其依赖的响应式数据进行缓存( 缓存计算结果 ),只有当它所依赖的响应式数据 发生变化时才会重新计算,这意味着在模板中多次访问同一个计算属性时,只会计算一次例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <template > <p > {{ fullName }}</p > 姓:<input type ="text" v-model ="firstName" > <br > 名:<input type ="text" v-model ="lastName" > </template > <script > export default { data ( ) { return { firstName : '王' , lastName : '五' } }, computed : { fullName : { get ( ) { return this .firstName + ' ' + this .lastName }, set (value ) { let [firstName, lastName] = value.split (' ' ) this .firstName = firstName this .lastName = lastName } } } } </script >
1 2 3 4 5 6 7 8 9 10 11 import { createApp } from 'vue' import App from './App.vue' const app = createApp (App )const vm = app.mount ('#app' )
watch
侦听器Vue 的 watch
选项可以用来监听某个属性的变化,并在属性变化时执行相应的操作
当你需要在数据发生变化时执行异步 或复杂的操作,或者需要在数据变化后执行一些特定的逻辑时,可以使用 watch
例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 <template > <h2 > 今天天气很{{ info }}</h2 > <button @click ="changeWeather" > 切换天气</button > </template > <script > export default { data ( ) { return { isHot : true } }, computed : { info ( ) { return this .isHot ? '炎热' : '凉爽' } }, methods : { changeWeather ( ) { this .isHot = !this .isHot } }, watch : { isHot : { handler (newValue, oldValue ) { console .log ('isHot 被修改了' , newValue, '->' , oldValue); } } } } </script >
watch
侦听器还支持一些配置选项,常用的有:
v-show
指令描述:用于根据条件动态的显示与隐藏元素,当表达式的值为 false
时元素的 style 就会加上 display: none
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <template > <button @click ="toggle" > Toggle</button > <div v-show ="isVisible" > I am visible</div > </template > <script > export default { data ( ) { return { isVisible : true } }, methods : { toggle ( ) { this .isVisible = !this .isVisible } } } </script >
v-if
指令描述:用于根据条件动态地渲染/销毁元素或组件。它根据表达式的值来决定是否渲染元素,它的语法为:v-if="条件"
v-if
还可以配合 v-else-if
和 v-else
指令来实现多条件处理
例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <template > <div v-if ="type === 'A'" > 这是类型 A</div > <div v-else-if ="type === 'B'" > 这是类型 B</div > <div v-else > 这是其他类型</div > <button @click ="changeType('A')" > A </button > <button @click ="changeType('B')" > B </button > <button @click ="changeType('O')" > O </button > </template > <script > export default { data ( ) { return { type : 'A' } }, methods : { changeType (type ) { this .type = type } } } </script >
v-for
指令描述:用于渲染列表数据,它可以将一个数组或一个对象在页面中响应式的渲染为多个相同结构的元素
语法:v-for="value in items"
or v-for="(value, key) in items"
例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 <template > <ul > <h2 > 人员列表(数组)</h2 > <li v-for ="(element, index) in persons" :key ="element.id" > {{ element.name }}-{{ element.age }} </li > </ul > <ul > <h2 > 汽车信息(对象)</h2 > <li v-for ="(value, key) in car" :key ="key" > {{ key }}-{{ value }} </li > </ul > <ul > <h2 > 遍历一个字符串</h2 > <li v-for ="(char, index) in string" > {{ char }}-{{ index }} </li > </ul > <ul > <h2 > 遍历指定次数(5次)</h2 > <li v-for ="(number, index) in 5" > {{ number }}-{{ index }} </li > </ul > </template > <script > export default { data ( ) { return { persons : [ { id : 1 , name : '张三' , age : 18 }, { id : 2 , name : '李四' , age : 19 }, { id : 3 , name : '王五' , age : 20 } ], car : { name : '宝马xxx' , price : 'xx万' , color : '白色' }, string : 'HelloVue' } } } </script >
key
属性的作用:
Vue 默认按照“就地更新”的策略来更新通过 v-for
渲染的元素列表。当数据项的顺序改变时,Vue 不会随之移动 DOM 元素的顺序,而是就地更新所有的元素,确保它们在原本指定的索引位置上渲染。默认模式是高效的,但只适用于列表渲染输出的结果不依赖子组件状态或者临时 DOM 状态 (例如表单输入值) 的情况
key
属性可以帮助 Vue 在列表项更新时准确、高效地识别每个元素,避免不必要的重新渲染。具体来说,key
提供了一个唯一标识,使 Vue 在数据变化时能更智能地对比新旧元素,从而进行最小化的 DOM 渲染
例2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <template > <h2 > 人员列表</h2 > <input type ="text" placeholder ="请输入名字" v-model ="keyWord" > <button @click ="sortType = 2" > 年龄升序</button > <button @click ="sortType = 1" > 年龄降序</button > <button @click ="sortType = 0" > 原顺序</button > <ul > <li v-for ="e in filPersons" :key ="e.id" > {{ e.name }} - {{ e.age }} - {{ e.sex }} </li > </ul > </template > <script > export default { data ( ) { return { keyWord : '' , sortType : 0 , persons : [ { id : 1 , name : 'Patricia' , age : 28 , sex : '女' }, { id : 2 , name : 'Jennifer' , age : 22 , sex : '女' }, { id : 3 , name : 'James' , age : 30 , sex : '男' }, { id : 4 , name : 'Robert' , age : 19 , sex : '男' } ] } }, computed : { filPersons ( ) { let arr = this .persons .filter (p => { return p.name .indexOf (this .keyWord ) !== -1 }) if (this .sortType ) { arr.sort ((p1, p2 ) => { return this .sortType === 1 ? (p2.age - p1.age ) : (p1.age - p2.age ) }) } return arr } } } </script >
v-text
& v-html
指令描述:用于更新 HTML 元素的内容
注意:由于 v-html
指令将字符串作为 HTML 直接解析,因此要确保绑定的数据是可信的。在使用 v-html
指令时,应该避免将用户输入的内容直接插入到 HTML 中,以防止 XSS( 跨站脚本攻击 ) 等安全问题
v-cloak
指令描述:用于解决 Vue 编译过程出现的闪烁问题。通常与 CSS 配合使用,可以确保在 Vue 实例完全编译之前,元素不会出现 Vue 插值表达式( {{xxx}}
)
例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <template > <h1 v-cloak > {{message}}</h1 > </template > <script > export default { data ( ) { return { message : 'v-cloak' } } } </script > <style > [v-cloak] { display : none; } </style >
v-pre
指令描述:用于跳过当前元素和其子元素的模板编译过程。这意味着你不能在 v-pre
标记的元素上使用 Vue指令和插值语法
例:
1 2 3 4 5 6 <template > <h1 v-pre > public element</h1 > <p > {{message}}</p > </template >