[英]How to add props to dynamic component in vue.js?
I create component dynamically: 我动态创建组件:
<component is="tab"/>
How to add props for this component? 如何为该组件添加道具? I try: 我尝试:
<component is="tab" my-props="test" />
And I got error in console: 而且我在控制台中出错:
[Vue warn]: Attribute "type" is ignored on component <component> because the component is a fragment instance: http://vuejs.org/guide/components.html#Fragment-Instance
PS I'm using 1.0.26 version vue.js. PS我正在使用1.0.26版本的vue.js。
If test not just "test" text, but some value in your data. 如果进行测试,不仅要“测试”文本,还需要对数据进行一些评估。 Then you need to pass it like so: 然后,您需要像这样传递它:
<component is="tab" :props="test" />
And don't forget to add your properties in props
array inside component. 并且不要忘记在组件的props
数组中添加属性。 props: ['props']
. props: ['props']
。
http://jsbin.com/zekiyeroka/edit?html,css,js,console,output http://jsbin.com/zekiyeroka/edit?html,css,js,控制台,输出
Here is working example. 这是工作示例。 Adding to the data is optional, but i prefer this way. 添加到数据是可选的,但我更喜欢这种方式。 Because you can operate your data in the much easier way in the future. 因为将来您可以以更轻松的方式来操作数据。
PS you need to declare your data
in the component like a function
, not just object
PS,您需要像function
一样在组件中声明data
,而不仅仅是object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.