?-6ren"> ?-我正在努力遵循以下 vuejs 应用场景动态组件 + 异步组件模式。一切正常,但仍然只有一个问题:我怎样才能访问通过传入的 Prop 数据 请看现场 fiddle : https://jsfiddl-6ren">
gpt4 book ai didi

javascript - vuejs : can we give props to async component via dynamic component pattern ?

转载 作者:行者123 更新时间:2023-11-30 15:57:07 29 4
gpt4 key购买 nike

我正在努力遵循以下 vuejs 应用场景动态组件 + 异步组件模式。一切正常,但仍然只有一个问题:我怎样才能访问通过

传入的 Prop 数据
<component :is="asyncComp" opts="someDataForAsyncComp">

请看现场 fiddle : 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/

不过我认为这不是最好的方法,最好问问 Evan。

关于javascript - vuejs : can we give props to async component via dynamic component pattern <component :is ="type" opts ="asyncCompOptions">?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38344091/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com