在Vue.js中传递属性作为组件参数
问题描述:
我在Vue.js中创建测试组件。我想传递一个参数在我的模板中使用如下:在Vue.js中传递属性作为组件参数
Vue.component('test', {
props: ['href'],
template: '<li><a href="{{href}}"><slot></slot></a></li>'
});
在我的html文件:
<test href="/">Tvest</test>
但物业href
不具约束力的属性。
<li><a href="{{href}}">Tvest</a></li>
我该如何在Vue.js中正确地做到这一点?
答
您需要get rid of the brackets周围href
并指定通过使用绑定数据属性v-bind
directive:
<li><a v-bind:href="href"><slot></slot></a></li>
答
使用v-bind directive设置道具:
<a v-bind:href="href"><slot></slot></a>
或快捷
<a :href="href"><slot></slot></a>
+0
当我这样放入时,我的组件消失 –
+0
我复制了括号...... :(。当然,括号是一种双向绑定,只能作为html元素的innerContent。 – Reiner
不错,工作就像一个魅力! –