首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在单击时将组件添加到vue

如何在单击时将组件添加到vue
EN

Stack Overflow用户
提问于 2021-08-24 23:52:18
回答 1查看 132关注 0票数 0

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

这是我的密码:

代码语言:javascript
复制
<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>

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

更新

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

代码语言:javascript
复制
[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 +电子上完成的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-25 01:22:18

似乎您正在使用vue2。

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

代码语言:javascript
复制
    // 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:运行时+编译器与运行时专用

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68915256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档