Vue中的省略语法(常用必掌握)
v-bind
-
缩写:
: -
预期:
any (with argument) | Object (without argument) -
修饰符:
.prop- 作为一个 DOM property 绑定而不是作为 attribute 绑定。.camel- (2.1.0+) 将 kebab-case attribute 名转换为 camelCase。(2.1.0+).sync(2.3.0+) 语法糖,会扩展成一个更新父组件绑定值的v-on侦听器。
-
用法: 动态地绑定一个或多个 attribute,或一个组件 prop 到表达式。
在绑定class或styleattribute 时,支持其它类型的值,如数组或对象。
在绑定 prop 时,prop 必须在子组件中声明。可以用修饰符指定不同的绑定类型。
没有参数时,可以绑定到一个包含键值对的对象。注意此时class和style绑定不支持数组和对象。 -
示例: prop 绑定。“prop”必须在子组件中声明。\
<one-component :prop="someThing" :fn=add></one-component>
可以传递函数,函数名后不要加(),函数在父组件的 methods 里定义,同时也要在子组件中有声明。
v-on
-
缩写:
@ -
预期:
Function | Inline Statement | Object -
参数:
event -
修饰符:
.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 }模式添加侦听器
-
示例: 方法处理器
<button v-on:click="fn"></button>