【问题标题】:How to add components to vue on click如何在单击时将组件添加到 vue
【发布时间】:2021-10-25 03:23:33
【问题描述】:

所以我有一个带有按钮的页面。单击此按钮时,我希望显示文本,并且每次单击该按钮时,我希望文本显示如下:

这是我的代码:

<v-row>
    <component
    v-for="(component, index) in components"
    :key="index"
    :is="component"/>
</v-row>

<v-row justify="left" class="ml-3">
    <v-btn class="elevation-7 grey darken-1 btn" @click="add">Click me</v-btn>
</v-row>

<script>
import Vue from 'vue'
Vue.component('component', {
        template: '<p>component</p>'
    })

export default {
        data: () => {
            return {
                components: [Comp]
            };
        },

        methods: {
            add: function () {
                this.components.push(Comp)
            }
        }
}
</script>

然而,当我点击按钮时,什么都没有出现?我真的很感激一些帮助,因为我不明白这一点。

更新

我检查了控制台,我看到了这个:

[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

然后此错误指向 this.components.push(Comp) 。在 vue.config.js 中我添加了 runtimeCompiler: true 但此错误仍然存​​在并且文本没有出现。

这都是在 vuejs + electron 上完成的。

【问题讨论】:

标签: javascript vue.js electron


【解决方案1】:

你好像在用vue2。

因为编译器不包含在运行时构建中,所以你必须为 vue 设置一个别名。

    // if you are using webpack
    build: {
        extend(config, ctx){
            config.resolve.alias['vue'] = 'vue/dist/vue.common';
        }
    },
    
    // if you are using vite
    vite: {
        resolve: {
            alias: {
                'vue': 'vue/dist/vue.common'
            }
        },
    }

更多详情请查看docs: Runtime + Compiler vs. Runtime-only

【讨论】:

  • 非常感谢
猜你喜欢
  • 2018-11-12
  • 2021-09-04
  • 2022-01-06
  • 2022-09-30
  • 2011-05-15
  • 2023-03-11
  • 2018-09-03
  • 2013-06-21
相关资源
最近更新 更多