Vue可以使用Component创建动态控件,语法如下:
<compontent :is="item.component" :options="item.options" ></compontent>
也可以使用v-for创建多个动态控件。如果迭代创建,需要统一控件的接口。方便起见,将所有的属性放置在options属性中,控件只有两个属性,一个是类型,另一个是optioins,这样可以使用一个控件进行迭代创建:
Vue.component('mc', {
props: {
options: Object,
field: String
},
data: function () {
return {
item: {}
}
},
template: `
<compontent :is="item.component" :options="item.options" ></compontent>
`,
created: function () {
var me = this;
if (this.options.controls)
for (var i in this.options.controls) {
var ctrl = this.options.controls[i];
if (ctrl.options.field === this.field) {
this.item = ctrl;
}
}
}
})
网友评论