一、插值语法
Mustache 语法,既双大括号。Mustache语法不仅可以直接写变量,也可以写简单的表达式
{{message}}
{{firstName + lastName}}
{{firstName + ' ' + lastName}} = {{firstName}} {{lastName}}
v-once :不会跟着数据的改变而改变,只会渲染一次!
< h2 v-once > {{message}}</ h2>
v-html :请求的数据本身就是一个HTML代码。
< h2 v-html = " url" > </ h2>
...
data:{
url: "< a href = ' http://www.baidu.com' > 百度一下</ a> "
}
v-test : 与Mustache比较相似:都是用于将数据显示在界面中。但通常情况下只接手一个String类型。
< h2> {{message}},123</ h2>
< h2 v-text = " message" > ,123</ h2>
v-pre :用于跳过这个元素和子元素的编译过程,既显示原本的Mustache语法。
< h2> {{message}}</ h2>
< h2 v-pre > {{message}}</ h2>
v-cloak :斗篷,某些情况下,浏览器可能会直接显示出未编译的Mustache标签,既js代码有延迟。
< h2 v-cloak > {{message}}</ h2>
< style>
[v-cloak] {
display : none;
}
</ style>
二、绑定属性
v-bind :用于绑定一个或多个属性值,或者向另一个组件传递props值。作用:动态绑定属性 缩写(语法糖): “: ”
动态绑定class(对象语法)
< h2 :class = " {active: isActive,line: isLine}" > {{message}}</ h2>
< h2 :class = " getActive()" > {{message}}</ h2>
...
methods:{
getActive(){
return {active: this.isActive,line: this.isLine}
}
}
动态绑定class(数组语法)
< h2 :class = " [' active' ,' line' ]" > {{message}}</ h2>
动态绑定style(对象语法)
< h2 : style =" { font-size : '50px' } " > {{message}}</ h2>
< h2 : style =" { fontSize : fontSize + 'px' } " >
...
data:{
fontSize: 50
}
动态绑定style(数组语法)
< div : style =" [baseStyles, overridingStyles] " > </ div>
...
data:{
baseStyle: {backgroundColor: 'red', fontSize: '100px'}
}
三、计算属性
computed :{…}当需要多次调用的时候,只会调用一次!性能更高~,且有缓存。
基本使用
< h2> {{fullName}}</ h2>
< script>
const app = new Vue ( {
el: "#app" ,
data: {
...
} ,
computed: {
fullName: function ( ) {
return this . firstName + ' ' + this . lastName
}
}
} )
</ script>
复杂操作
< h2> {{totalPrice}}</ h2>
< script>
const app = new Vue ( {
el: "#app" ,
data: {
...
} ,
computed: {
totalPrice: function ( ) {
let result = 0
for ( let i = 0 ; i < this . books. length; i++ ) {
result += this . books[ i] . price
}
return result
}
}
} )
</ script>
计算属性的setter和getter(完整写法) 3.1 一般实现get方法! 只读属性! 无set方法! 3.2 下面的简写版就是上面“基本使用”所展示的样子。 3.3 set方法如果要实现是有参数的,但不常用。可自行测试~
< h2> {{fullName}}</ h2>
< script>
const app = new Vue ( {
el: "#app" ,
data: {
...
} ,
computed: {
fullName: {
set : function ( ) { }
get : function ( ) {
return this . firstName + ' ' + this . lastName
}
}
}
} )
</ script>
计算属性和methods的对比
< h2> {{getFullName()}}</ h2>
< h2> {{getFullName()}}</ h2>
< h2> {{FullName}}</ h2>
< h2> {{FullName}}</ h2>
< script>
const app = new Vue ( {
el: "#app" ,
data: {
...
} ,
computed: {
fullName: function ( ) {
console. log ( "fullname" )
return this . firstName + ' ' + this . lastName
}
} ,
methods: {
getFullName: function ( ) {
console. log ( "getfullname" )
return this . firstName + ' ' + this . lastName
}
}
} )
</ script>
四、事件监听
基本使用
< button @click = " increment" > +</ button>
< script>
methods: {
increment ( ) {
this . counter ++
}
}
</ script>
参数问题
< button @click = " increment" > +</ button>
< button @click = " increment()" > +</ button>
< script>
methods: {
increment ( ) {
this . counter ++
}
}
</ script>
< button @click = " increment(123)" > +</ button>
< button @click = " increment()" > +</ button>
< button @click = " increment" > +</ button>
< script>
methods: {
increment ( event) {
console. log ( event)
}
}
</ script>
< button @click = " increment(123,$event)" > +</ button>
< button @click = " increment(123)" > +</ button>
< button @click = " increment()" > +</ button>
< button @click = " increment" > +</ button>
< script>
methods: {
increment ( abc, event) {
console. log ( abc, event)
}
}
</ script>
修饰符的使用
< div @click = " divClick" >
aaaa
< button @click = " buttonClick" > 按钮</ button>
</ div>
< div @click = " divClick" >
aaaa
< button @click.stop = " buttonClick" > 按钮</ button>
</ div>
< input type = " submit" value = " 提交" @clcik.prevent = " SubmitClick" >
< input type = " text" @keyup = " keyup" >
< input type = " text" @keyup.enter = " keyup" >
< button @click.once = " buttonClick" > 按钮</ button>
< button @click.native = " buttonClick" > 按钮</ button>