noobcode0000
noobcode0000

Reputation: 243

How to add components to vue on click

So I have a page with a button. When this button is clicked I would like text to appear, and everytime the button is clicked I would like the text to appear like this:

enter image description here

This is my code:

<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>

Yet when I click the button nothing appears? I would really appreciate some help as I do not understand this.

UPDATE

I checked console and I am seeing this:

[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 error then points to this.components.push(Comp) . In vue.config.js I added runtimeCompiler: true yet this error still persists and the text does not appear.

This is all done on vuejs + electron.

Upvotes: 0

Views: 579

Answers (1)

DengSihan
DengSihan

Reputation: 2997

It seems like you are using vue2.

Because the compiler is not included in runtime-only builds, you have to set an alias for 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'
            }
        },
    }

for more details, please view the docs: Runtime + Compiler vs. Runtime-only

Upvotes: 1

Related Questions