[英]Vue.js dynamic component with dynamic data
I'm trying to use the <component>
element to dynamically display a chosen component.我正在尝试使用<component>
元素来动态显示选定的组件。 Each of these displayed components can take one of any number of data objects.这些显示的组件中的每一个都可以采用任意数量的数据对象之一。 Something like:就像是:
<div id="containers">
<component v-bind:is="currentView"></component>
</div>
var myVue = new Vue({
el:"#containers",
data:{
currentView: "my-component-one",
currentData: {...}
},
method: {
changeView: function(){
//change this.currentView
//change this.currentData
}
}
});
However, the Vue documentation says the v-bind:is
attribute can be used to pass either a component name or the options object.但是, Vue 文档说v-bind:is
属性可用于传递组件名称或选项对象。
It is unclear how I would conditionally get an object of values for that component to use and also conditionally change which component is shown.目前尚不清楚我将如何有条件地获取该组件的值对象以使用以及有条件地更改显示的组件。
I am very green with using Vue (coming fresh off a knockout kick) so perhaps I am simply misunderstanding the intention of the component tag.我非常喜欢使用 Vue(刚从淘汰赛中踢出),所以也许我只是误解了组件标签的意图。
you can simply use v-bind
你可以简单地使用v-bind
html html
<component v-bind:is="currentView" v-bind="data"></component>
script脚本
data()
{
return {
data: {
currentData: "example"
}
}
}
and it will pass currentData
down to child.它会将currentData
向下传递给孩子。 You can also add other properties along with it, including is
.您还可以添加其他属性,包括is
。
If you need to change the component along with props, then you just change the data
property, or whatever you want to call it.如果您需要随 props 一起更改组件,那么您只需更改data
属性,或者您想调用的任何内容。
https://codesandbox.io/s/7w81o10mlx https://codesandbox.io/s/7w81o10mlx
This example might help you understand it.这个例子可能会帮助你理解它。 https://jsfiddle.net/jacobgoh101/mLbrj5gd/ https://jsfiddle.net/jacobgoh101/mLbrj5gd/
For passing Component Name用于传递组件名称
If the component is global, you can pass the component name to v-bind:is
如果组件是全局的,您可以将组件名称传递给v-bind:is
for eg,例如,
Vue.component('test1', {
template: `<div>test1</div>`
})
HTML HTML
<component is="test1"></component>
For passing option对于传递选项
A Vue component is literally just a Javascript object with specific properties Vue 组件实际上只是一个具有特定属性的 Javascript 对象
for eg,例如,
<component v-bind:is="{
template: `<div>test2</div>`
}"></component>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.