javascript - 以编程方式实例化 vuetify-components

标签 javascript vue.js components vuetify.js

我正在尝试以编程方式实例化 vuetify-components 并将它们添加到 DOM 中。使用 v-cardv-dialoge 等简单组件可以正常工作,但不能与 v-data-tables 一起使用。

我创建了一个codesandbox来展示这个问题:https://codesandbox.io/s/throbbing-butterfly-4ljhx?file=/src/components/MainWindow.vue

单击添加表按钮时查看控制台中的错误。

任何人都可以帮我了解如何添加表以及为什么它会抛出这些类型错误吗?谢谢!

顺便说一句。我正在遵循本指南:https://css-tricks.com/creating-vue-js-component-instances-programmatically/

最佳答案

您需要将 vuetify 实例传递到扩展 Vue 构造函数中,就像实例化主 Vue 实例时一样...

MainWindow.vue

<template>
  <v-app>
    <v-btn @click="addTable" color="red">Generate Data-Table</v-btn>
    <hr>
    <v-btn @click="addCard">Generate simple Card</v-btn>
    <v-container ref="mainContainer"></v-container>
  </v-app>
</template>

<script>
import Table from "./Table.vue";
import Card from "./Card.vue";
import Vue from "vue";
import vuetify from "../plugins/vuetify";

export default {
  name: "mainWindow",
  components: { Table, Card },
  methods: {
    addTable() {
      var ComponentClass = Vue.extend(Table);
      var instance = new ComponentClass({ vuetify });
      instance.$mount();
      this.$refs.mainContainer.appendChild(instance.$el);
    },
    addCard() {
      var ComponentClass = Vue.extend(Card);
      var instance = new ComponentClass({});
      instance.$mount();
      this.$refs.mainContainer.appendChild(instance.$el);
    }
  }
};
</script>

<style>
</style>

注意:这不是在 Vue 中使用动态组件的推荐方式!

来自链接的文章:

In my case, I don’t know which component to insert in the template and also where to insert it. This information is only available at runtime

仅当您不知道“哪里”时这才有用。如果您知道“哪里”,“哪个”部分可以通过 Dynamic Components 轻松解决

关于javascript - 以编程方式实例化 vuetify-components,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63844286/

相关文章:

C++实体组件系统和使用模板访问组件

architecture - 复杂信息系统制图工具

javascript - 在重定向之前,如何在鼠标单击时为 'a' 元素设置动画?

javascript - Vue.js 组件中的 DRY 初始化

c# - 用于创建具有合并功能的电子邮件模板的好库

webpack - 使用 for 循环创建异步 Vue 组件

node.js - 配置服务器来为 vue 应用程序提供服务

javascript - 当分数上升时,如何减少我的 setInterval?

javascript - 多个 Controller 操纵 Angular 指令中的内容?

javascript - 如何使用 jQuery 使用 data-filters 属性检索 "Result One"?