Tips for dynamically adding components to a Vue application when a user clicks on a

I have a page with a button that, when clicked, should display text. Each time the button is clicked, the text should appear as follows:

https://i.sstatic.net/dGDsv.gif

Below is the code I am using:

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

However, when I click the button, nothing appears. I would appreciate some help as I'm having trouble understanding what's not working.

UPDATE

Upon checking the console, I noticed the following error message:

[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 seems to be pointing towards this.components.push(Comp). Despite adding runtimeCompiler: true to vue.config.js, the error persists and the text remains invisible. This issue is occurring within a vuejs + electron environment.

Answer №1

It appears that your project is utilizing vue2.

Since the compiler is not included in runtime-only builds, you will need to specify 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 further information, please refer to the documentation on Runtime + Compiler vs. Runtime-only

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible that scrollIntoView() function is not available when the page is first loaded?

Within my FAQ page, I have concealed the answers to each question by default, displaying only the questions with a link that allows others to reference each specific question through an embedded id anchor. Here is the structure of the question format: &l ...

Tips for creating a Material UI (next) dialog with generous top and bottom margins that extend off the screen

I am encountering a challenge with the layout. What am I looking for? I need a modal dialog that expands vertically, extending beyond the screen, centered both horizontally and vertically, with minimal margin on the top and bottom. Is this feature not d ...

Swapping Out Models in Three.js: A Guide to Replacing Object3D Models

I'm attempting to swap out an object3D for a character model, while retaining all of its attributes like the position. var object = new THREE.Object3D( ); object = models.character[0].clone( ); object.position.set( 100, 230, 15 ); scene.add( object.sc ...

Prevent altering client values via developer tools

Our application is built using HTML5, the Foundation framework by ZURB, and AngularJS. We are seeking a way to prevent users from accessing and changing the values of our Angular objects (specifically scope variables) through the developer tool console. ...

Creating a text design that spans two lines using Scalable Vector Graphics (SVG

I am working with an SVG that displays strings pulled from an Array... {"label":"Here is an example of the string...", "value":4}, The text above is shown in an SVG element as... <text>Here is an example of the string...<text> I would like ...

Develop an ngDialog template that can be easily reused across different projects

I am interested in developing a reusable template for my application. As far as I know, it seems like you can't directly pass the title and body to ngDialog. What I'm looking for is something similar to the following : <div> <h2>{{ ...

How can I stop the accordion from toggling in Bootstrap V5 when clicking on an element in the header?

In the latest version of Bootstrap, v5, it seems that using e.stopPropagation(); no longer prevents the accordion from toggling when an element in the header is clicked. Do you have a solution for this problem? How to prevent accordion from toggling when ...

Omit any items from an array that do not have any child elements

Upon receiving data from the server in the format of a flat tree, I proceed to transfer this data to the JsTree library for tree building. Before sending the data to JsTree, I filter out any empty elements of type "folder" that do not have children. Below ...

Leveraging SystemJS/jspm for loading asynchronous, es5 modules in a production environment

I am looking for a way to dynamically load dependencies using System.import(), without the need to transpile ES6 to ES5 at production runtime. My goal is to have these modules transpiled into separate ES5 modules that are only fetched when necessary, rathe ...

The JSON.parse function encounters issues when trying to parse due to a SyntaxError: Unexpected character found after JSON at position 2, causing it to be unable

I've encountered an issue with my JavaScript code when trying to retrieve the value of the details field from JSON data. While all other values are successfully passed to their respective fields, the details field generates the following error: "Unabl ...

The Jquery ajax page is redirecting automatically when a post request is made

Encountering an issue while attempting to upload multiple files through AJAX, as the process redirects to a blank page displaying only the names of the uploaded files. Here is the HTML tag: Below is the JavaScript function: function upload(){ var proje ...

Error: 'discord not defined' encountered when using vscode and powershell

I'm still learning the ropes of javascript, and while attempting to link discord with VSCode (using windows powershell), I'm using the following code: const Discord = require (discord.js) ; const client = new Discord.Client(); clien ...

Res.render() is failing to display content

I'm facing an issue with my express route where it's not rendering a jade template properly. If I provide the wrong jade template string, I get the usual error indicating that the file couldn't be found to render in the browser. However, wh ...

Guide on using webpack to import jQuery

When using webpack, is it necessary to install the jquery file from npm, or can I simply require the downloaded file from the jquery website? directory app/ ./assets/javascripts/article/create/base.js ./assets/javascripts/lib/jquery-1.11.1.min.js webpack ...

Utilize Cordova's social share plugin to send a text message containing a variable

I have implemented the social share plugin from Cordova in order to share SMS messages with a specific phone number. <a class = "lft" onclick="window.plugins.socialsharing.shareViaSMS('My cool message', 7767051447, function(msg) {cons ...

Displaying two div elements horizontally using Material-UI

Can the styled utility from mui be used to align 2 divs side by side? The documentation examples don't seem to cover this specific scenario. While answers on this and this address it, they don't relate to mui / material ui. I attempted the fol ...

What is the best approach to resolving the MongoServerError: E11000 duplicate key error?

Index.Js File: const cookieSession = require("cookie-session"); const express = require("express"); const app = express(); const helmet = require("helmet"); const morgan = require("morgan"); const dotenv = require(&q ...

When you hit the refresh button, the vue router automatically redirects you back to

I've encountered an issue with my vuejs project involving the use of vue router in a single page application. While I am able to navigate to different pages using vue router, whenever I reload the page at any route, it ends up redirecting me back to t ...

Vue Component, switching state

Feeling like I'm losing it, this should be really simple but it's not working... The idea is that when the link is clicked, the display property toggles between true and false. However, it's not working as expected. Vue.component('d ...

Tips for resizing a two-dimensional circle using a small graphical user interface in THREE.js

My goal is to adjust the size of a circle by dragging a small GUI slider. I've developed this code which creates a circular shape with a blue outline on a black background using an orthographic camera: setUpGUI(); let radius = 0.6; let ver ...