Vue中的冒号变量
在使用Vue中经常看到HTML中有标签属性前面添加了“:”,有些没有。其实是v-bind的缩写。v-bind缩写: ‘:’用法:动态绑定一个或多个特性,或一个组件prop到表达式。在绑定class或style特性时,支持其它类型的值,如数组或对象。在绑定prop时,必须在子组件中声明。可以用修饰符指定不同的绑定类型。没有参数时,可以绑定到一个包含键值对的对象,注意此时class和s...
·
在使用Vue中经常看到HTML中有标签属性前面添加了“:”,有些没有。
其实是v-bind的缩写。
v-bind
- 缩写: ‘:’
- 用法:动态绑定一个或多个特性,或一个组件prop到表达式。在绑定class或style特性时,支持其它类型的值,如数组或对象。在绑定prop时,必须在子组件中声明。可以用修饰符指定不同的绑定类型。没有参数时,可以绑定到一个包含键值对的对象,注意此时class和style绑定不支持数组和对象。
示例:
<!-- 绑定一个属性 -->
<img v-bind:src="imageSrc">
<!-- 动态特性名 (2.6.0+) -->
<button v-bind:[key]="value"></button>
<!-- 缩写 -->
<img :src="imageSrc">
<!-- 动态特性名缩写 (2.6.0+) -->
<button :[key]="value"></button>
<!-- 内联字符串拼接 -->
<img :src="'/path/to/images/' + fileName">
<!-- class 绑定 -->
<div :class="{ red: isRed }"></div>
<div :class="[classA, classB]"></div>
<div :class="[classA, { classB: isB, classC: isC }]">
<!-- style 绑定 -->
<div :style="{ fontSize: size + 'px' }"></div>
<div :style="[styleObjectA, styleObjectB]"></div>
<!-- 绑定一个有属性的对象 -->
<div v-bind="{ id: someProp, 'other-attr': otherProp }"></div>
<!-- 通过 prop 修饰符绑定 DOM 属性 -->
<div v-bind:text-content.prop="text"></div>
<!-- prop 绑定。“prop”必须在 my-component 中声明。-->
<my-component :prop="someThing"></my-component>
<!-- 通过 $props 将父组件的 props 一起传给子组件 -->
<child-component v-bind="$props"></child-component>
<!-- XLink -->
<svg><a :xlink:special="foo"></a></svg>
修饰符:
(1).prop
被用于绑定DOM属性
(2).camel
将kebab-case特性转换为camelCase
(3).sync
语法糖,会扩展成一个更新父组件绑定值的v-on侦听器。
更多推荐
已为社区贡献1条内容
所有评论(0)