最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js v-if for attributes - Stack Overflow

programmeradmin5浏览0评论

Let's say I have this code

table(my-attr="value")
    ..plex ponent Jade...

and I would like render that my-attr base on property delivered into ponent. Since v-if works on whole element I cannot do something like

table(my-attr="value", v-if="myProp")
table(v-else)

because I would have to duplicate all the code inside table.

How can I achieve that?

Let's say I have this code

table(my-attr="value")
    ...plex ponent Jade...

and I would like render that my-attr base on property delivered into ponent. Since v-if works on whole element I cannot do something like

table(my-attr="value", v-if="myProp")
table(v-else)

because I would have to duplicate all the code inside table.

How can I achieve that?

Share Improve this question asked Mar 30, 2016 at 7:20 n1_n1_ 4,3974 gold badges36 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15

You can use v-bind or interpolate the value directly with {{}}

// (sorry, no jade)
<table v-bind:attribute1="someMethod" attribute2="{{anotherMethod}}">

Now someMethod and anotherMethod should be data, puted properties, or methods of your ponent, and should return either the attribute's desired value or false. In the latter case, the attribute will not be added to the element at all.

Update: Note that interpolations in attributes have been removed in Vue 2

发布评论

评论列表(0)

  1. 暂无评论