如何使用动态道具数据动态渲染 vue 组件的新实例
How can I render a new instance of vue component dynamically with dynamic props data
我有一个vue组件,实现接口如下。
<my-data-grid :colunms='columns' :rows='rows'></my-data-grid>
我需要在具有不同数据的多个标签页上动态呈现上述组件。每个 my-data-grid 在不同的时间呈现。就像输入一组新数据然后创建一个呈现组件的新页面一样。目前它可以正确更新列和行属性,但一旦有新数据进入就会覆盖其他标签页上的数据。
当我尝试动态执行此操作以便组件可以在 运行 时呈现时,组件显示但列和行道具未更新。它显示 data() 未定义。
我的部分代码如下所示。你也可以参考这个 link
Render other components in Tab using template
Import MyDataGrid from "./MyDataGridComponent'
Vue.component('my-data-grid', MyDataGrid)
export default {
name: 'app',
data: function() {
return {
headerText0: {
text: 'Tab1'
},
columns: ['some arrays'],
rows: ['some arrays'],
headerText1: {
text: 'Tab2'
},
headerText2: {
text: 'Tab3'
},
Content1: function() {
return {
template: Vue.component('MyDataGrid', {
template: ' <my-data-grid :colunms='columns' :rows = 'rows' > < /my-data-grid>',
data() {
return {
colunms: this.columns,
rows: this.rows
}
}
)
}
}
}
}
}
过去 3 天我都在互联网上搜索过,不知何故我感到筋疲力尽。这是我第一次在Whosebug上提问,如果我跑题了请见谅。
感谢期待您的帮助。
属性是使用 'props' 在组件上定义的。您将行和列定义为组件的本地数据。
将行和列移出数据字段,并添加一个 props 字段:
props: ['rows', 'columns'],
@Jim 感谢您的回复,经过大量搜索我能够从这个 article: https://css-tricks.com/creating-vue-js-component-instances-programmatically/ 中得到解决方案。我通过使用 Vue.extend 创建 MyDataGrid 组件的副本来解决它,如下所示。
下面一行导入了我的vue组件
import MyDataGrid from "./MyDataGridComponent.vue";
我将 MyDataGrid 组件传递给 Vue.extend 以创建 Vue 构造函数的子类
var MyDataGridClass = Vue.extend(MyDataGrid);
然后使用 new 关键字从中创建一个实例:propsData 为我的 vue 组件中的列和行道具提供所需的数据。
var instance = new MyDataGridClass({
propsData: { columns: this.columns, rows: this.rows }
});
I mount空元素上的实例
instance.$mount()
我使用原生 javascript api 将其插入我页面上的 DOM 元素中。
var tab = document.getElementById('#mytab')
tab.appendChild(instance.$el)
完整代码如下图
import Vue from "vue";
import MyDataGrid from "./MyDataGridComponent.vue";
export default {
data: function () {
return {
columns: ['some array'],
rows: ['some array']
}
},
methods: {
addTab() {
var ComponentClass = Vue.extend(MyDataGrid);
var instance = new ComponentClass({
propsData: { columns: this.columns, rows: this.rows }
});
instance.$mount()
var tab = document.getElementById('#mytab')
tab.appendChild(instance.$el)
}
}
};
有时候挑战是创造力的肥料。寻找,你就会找到。
我有一个vue组件,实现接口如下。
<my-data-grid :colunms='columns' :rows='rows'></my-data-grid>
我需要在具有不同数据的多个标签页上动态呈现上述组件。每个 my-data-grid 在不同的时间呈现。就像输入一组新数据然后创建一个呈现组件的新页面一样。目前它可以正确更新列和行属性,但一旦有新数据进入就会覆盖其他标签页上的数据。
当我尝试动态执行此操作以便组件可以在 运行 时呈现时,组件显示但列和行道具未更新。它显示 data() 未定义。
我的部分代码如下所示。你也可以参考这个 link Render other components in Tab using template
Import MyDataGrid from "./MyDataGridComponent'
Vue.component('my-data-grid', MyDataGrid)
export default {
name: 'app',
data: function() {
return {
headerText0: {
text: 'Tab1'
},
columns: ['some arrays'],
rows: ['some arrays'],
headerText1: {
text: 'Tab2'
},
headerText2: {
text: 'Tab3'
},
Content1: function() {
return {
template: Vue.component('MyDataGrid', {
template: ' <my-data-grid :colunms='columns' :rows = 'rows' > < /my-data-grid>',
data() {
return {
colunms: this.columns,
rows: this.rows
}
}
)
}
}
}
}
}
过去 3 天我都在互联网上搜索过,不知何故我感到筋疲力尽。这是我第一次在Whosebug上提问,如果我跑题了请见谅。 感谢期待您的帮助。
属性是使用 'props' 在组件上定义的。您将行和列定义为组件的本地数据。
将行和列移出数据字段,并添加一个 props 字段:
props: ['rows', 'columns'],
@Jim 感谢您的回复,经过大量搜索我能够从这个 article: https://css-tricks.com/creating-vue-js-component-instances-programmatically/ 中得到解决方案。我通过使用 Vue.extend 创建 MyDataGrid 组件的副本来解决它,如下所示。
下面一行导入了我的vue组件
import MyDataGrid from "./MyDataGridComponent.vue";
我将 MyDataGrid 组件传递给 Vue.extend 以创建 Vue 构造函数的子类
var MyDataGridClass = Vue.extend(MyDataGrid);
然后使用 new 关键字从中创建一个实例:propsData 为我的 vue 组件中的列和行道具提供所需的数据。
var instance = new MyDataGridClass({ propsData: { columns: this.columns, rows: this.rows } });
I mount空元素上的实例
instance.$mount()
我使用原生 javascript api 将其插入我页面上的 DOM 元素中。
var tab = document.getElementById('#mytab') tab.appendChild(instance.$el)
完整代码如下图
import Vue from "vue";
import MyDataGrid from "./MyDataGridComponent.vue";
export default {
data: function () {
return {
columns: ['some array'],
rows: ['some array']
}
},
methods: {
addTab() {
var ComponentClass = Vue.extend(MyDataGrid);
var instance = new ComponentClass({
propsData: { columns: this.columns, rows: this.rows }
});
instance.$mount()
var tab = document.getElementById('#mytab')
tab.appendChild(instance.$el)
}
}
};
有时候挑战是创造力的肥料。寻找,你就会找到。