vuejs:我们能否通过动态组件模式<组件:is=“type”opts=“asyncCompOptions”>为异步组件
vuejs: can we give props to async component via dynamic component pattern <component :is="type" opts="asyncCompOptions">?
我正在努力遵循 vuejs 应用场景动态组件 + 异步组件模式。一切正常,但仍然只有一个问题:如何访问通过传入的道具数据
<component :is="asyncComp" opts="someDataForAsyncComp">
请看现场小提琴:https://jsfiddle.net/matiascx/wjab87om/8/
这是代码:
<div id="app">
by dynamic Component:
<component
v-for="item in items"
:is="item.component"
:opts="item.options"> <!-- can we give props data to async component here? -->
</component><div>
以下是 js 部分:
Vue.component('node3', function (resolve, reject) {
setTimeout(function () {
resolve({
template: '<div>I am async node3!</div>',
created: function(){
console.log(this.opts); // can we access props transferred into async component via component
}
})
}, 1000)
})
Vue.component('node', {
template: '<div>must be static tpl!</div>',
props: ['opts'],
computed: {
log: function() {
return JSON.stringify(this.opts);
}
},
data() {
return {}
},
created: function(){
console.log(this.opts);
}
});
Vue.component('node2', {
template: '<div>node2</div>',
props: ['opts'],
computed: {
log: function() {
return JSON.stringify(this.opts);
}
},
data() {
return {}
},
created: function(){
console.log("dfdsfsdfa");
}
});
new Vue({
el: '#app',
data() {
return {
newItem: {
component: "",
options: ""
},
items: [{
component: "node",
options: {
type: "node",
tpl: "<div>node: {{ opts }} {{ log }}</div>"
}
},
{
component: "node2",
options: {
type: "node2",
tpl: "<div>node2: {{ opts }} {{ log }}</div>"
}
},
{
component: "node3",
options: {
type: "node3",
tpl: "<div>node3: {{ opts }} {{ log }}</div>"
}
}
]
};
},
computed: {
isButtonDisplayed() {
return this.newItem.component && this.newItem.options
}
},
methods: {
addItem() {
this.items.push(this.newItem);
this.newItem = {
component: "",
options: ""
}
}
}
});
我在你的jsfiddle中添加了三行:
// dynamic component
var _this = this
this.$nextTick(()=>{console.log(JSON.stringify(_this.$options._parentVnode.data.attrs.opts))})
// static component
console.log(JSON.stringify(this.$options.propsData.opts))
https://jsfiddle.net/gurghet/wjab87om/11/
我不认为这是最好的方法,最好问问埃文。
相关文章:
- React重新渲染但未显示正确的组件
- 不能从angular2中的子组件指定父组件中的数组
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- 如何将输入(type=text)从html表单传递到javascript函数
- React组件等待所需道具进行渲染
- 如何创建带有插槽的vue js组件预加载程序
- 正在将base64 jpeg从input-type=file上传到服务器
- KnockoutJS-组件-多个实例
- 如何更改reactjs中外部/独立组件的状态或属性
- 如何在react js中将值从一个组件发送到另一个组件
- React Native通过Navigator将用户输入数据传递到选项卡栏IOS中的组件
- React组件-设置页面标题
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- 如何在salesforce aura闪电组件中使用jQuery.val()
- 为react组件传递道具的最佳方式
- [Vue warn]:未能解析组件
- 如何控制组件'的createContent函数被激发
- react组件中的绑定方法
- 如何在react js中移动第二个组件
- vuejs:我们能否通过动态组件模式<组件:is=“type”opts=“asyncCompOptions”>为异步组件