简体   繁体   中英

Pass Attribute as a Component Parameter in Vue.js

I'm creating a test Component in Vue.js. I want to pass a parameter to use in my template as follows:

Vue.component('test', {
   props: ['href'],
   template: '<li><a href="{{href}}"><slot></slot></a></li>'
});

In my html file:

<test href="/">Tvest</test>

But the property href is not binding to the attribute.

<li><a href="{{href}}">Tvest</a></li>

How can I do it properly in Vue.js?

Use the v-bind directive to set the prop:

<a v-bind:href="href"><slot></slot></a>

or shortcut

<a :href="href"><slot></slot></a>

您需要去掉href周围的括号,并使用v-bind指令指定您正在绑定数据属性:

<li><a v-bind:href="href"><slot></slot></a></li>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM