admin管理员组文章数量:1319010
I have created a custom modal ponent using VueJS 2.0 with a prop of 'close'. I basically have it working like I want, but would like to improve it a bit.
One feature I have is the option to add a close button as follows:
Add a custom prop of close="true" in the HTML
<modal close="true">
Etc...
</modal>
Use a V-IF conditional statement in the JS file
<button v-if="close == 'true'></button>
The result is that if the property of close is set to true then the close button appears, if not it does not. This is what I want and it works.
My question is can I simplify this solution so that I can simply set the html as follows: <modal close>...</modal>
.
Then I would simply check if the property of close exists. I tried to do so using <button v-if="close"></button>
, but that did not work.
So, what I'd like to know is if this is possible and (if so) how to do it.
Thanks in advance for any help you can offer.
I have created a custom modal ponent using VueJS 2.0 with a prop of 'close'. I basically have it working like I want, but would like to improve it a bit.
One feature I have is the option to add a close button as follows:
Add a custom prop of close="true" in the HTML
<modal close="true">
Etc...
</modal>
Use a V-IF conditional statement in the JS file
<button v-if="close == 'true'></button>
The result is that if the property of close is set to true then the close button appears, if not it does not. This is what I want and it works.
My question is can I simplify this solution so that I can simply set the html as follows: <modal close>...</modal>
.
Then I would simply check if the property of close exists. I tried to do so using <button v-if="close"></button>
, but that did not work.
So, what I'd like to know is if this is possible and (if so) how to do it.
Thanks in advance for any help you can offer.
Share Improve this question asked Mar 23, 2017 at 16:18 MosheMoshe 7,02121 gold badges76 silver badges137 bronze badges1 Answer
Reset to default 5If you set the default of the property to be false
, then you can check whether the property is exactly false
to determine whether or not to show the button:
props: {
close: {
default: false,
},
}
Then you can check like this:
<button v-if="close !== false"></button>
When you add the close
property to the modal
ponent without specifying a value, the value will equal an empty string: ''
. So, it won't be equal to false, and will thus display the close button.
本文标签: javascriptVueJS 20 Using Vif to Check the Existence of a Custom PropStack Overflow
版权声明:本文标题:javascript - VueJS 2.0: Using V-if to Check the Existence of a Custom Prop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742052918a2418146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论