Is there a way to render dynamic components in Vue similar to JSX?

When working with jsx, it is possible to store components in variables using syntax like

const comp=<p>Hello</p>
. This allows flexibility in rendering components by choosing which one to use.

I am curious if there is a similar capability in Vue. For example, if I have a template that looks like this:

<template>
  <variable-comp />
</template>

I would like to dynamically change the content of variable-comp. While I know about v-if and v-for, they do not provide the same functionality.

Answer №1

Vue utilizes the following syntax to manage dynamic components:

<component v-bind:is=”currentComponent”/>

In this case, 'currentComponent' represents the name (string) of the component.

For example:

<template>
  <component v-bind:is=”currentComponent”/>
</template>
import CompA from './CompA.vue'
import CompB from './CompB.vue'
export default {
  components: {
    CompA,
    CompB
  },
  data() {
    isA: true
  },
  computed: {
    currentComponent() {
      return isA ? 'CompA' : 'CompB'
    }
  }
}

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

WebGL annotations failing to display on the webpage

I'm currently working on displaying a 3D model using Three.js in a web browser and looking to incorporate annotations like the ones shown in this Sketchfab model: Interactive 3D Test. I came across a helpful guide at this link, but I'm facing iss ...

Using VueJs and Laravel 5 for On input implementation

After following the steps outlined in the mentioned tutorial, I was able to achieve a perfect validation setup. However, my goal now is to implement real-time form validation without relying on a traditional submit button. I want the validation to occur as ...

The push() function for Angular $scope is not defined

Below is the HTML code snippet provided: <input ng-blur="CheckUser()" name="username" ng-model="RegisterFormData.username" class="form-control"/>{{ a.check_username_result }} I have assigned the controller ...

Exploring Array Iteration with For-Each in JavaScript

function retrieveMovieCredits(movie) { let counter = 0; let castIds = []; let movieIdList = movie; console.log("Movie ID array length-- ", movieIdList.length); movieIdList.forEach((movieId, index) => { let apiEndpoint = domain + 'mov ...

Creating a React component library can be problematic when incorporating styled components, as they may

I've been working on publishing my React component library, but I encountered an error when installing and testing it in create-react-app. I experimented with various templates for the React component library such as Rinsejs and create-react-library, ...

(NodeJS + Socket IO Issue) NodeJS is sending duplicate data when the page is refreshed, causing an improper response

Each time I refresh a page, NodeJS seems to be repetitively writing data on the socket. Interestingly, the number of writes increases with each page refresh but then stabilizes at three after several refreshes. I urge you to inspect the console output whi ...

Slowly reveal a portion of the picture

I have created a slider that changes pictures when the next button is clicked. Everything works perfectly on localhost, but on the server, some images fade in halfway instead of fully displaying. There are 39 pictures in total for the slider, each with a s ...

"Surprising outcomes when using the splice method on an array

Exploring the functionalities of Array.splice(). deleteCount: A whole number indicating the quantity of old array elements to eliminate. Understood. It seems clear. I aim to extract the final 4 items in the array. I trust that refers to elements? arr. ...

Calculate the total of additional user inputs and show the result in a separate element using JavaScript

Query Is there a way for an element to dynamically show the total of different inputs? Situation I have multiple text boxes all with the same class. There are 4 input fields, each containing a number - Input 1 is "1", Input 2 is "2", and so on. The goal ...

Vue Progress Bar service

When working with Angular, I typically utilize a dedicated class to manage the progress bar functionality. By intercepting HTTP requests and routing requests, including GraphQL requests like so: loading-indicator-service import { Injectable } from &apos ...

Issues with External JavaScript Functionality in MVC 4

To better illustrate my issue, I have created a sample code snippet that showcases the problem. Here is the original code used in the view: @model MyProject.Web.UI.ViewModels.MyModel @{ ViewBag.Title = "Test"; } @section scripts{ <script> ...

Failed request using Ajax

I've been trying to call a C# function using ajax, but for some reason it's not working. Here is the ajax call I'm making: $('#button1 button').click(function () { var username = "username_declared"; var firstname = "firs ...

SelectBoxIt jquery plugin can be applied after the completion of populating options in the select element with AngularJS

Utilizing AngularJS, I am dynamically populating a select box with the code below: <select ng-model="department" ng-options="dept as dept.name for dept in departmentList" class="fancy"> <option value="">-- select an optio ...

A step-by-step guide on displaying log files using NanoHTTPD

I have developed a Java desktop application that can receive HTTP requests using the embedded NanoHTTPD web server from https://github.com/NanoHttpd/nanohttpd. Once an HTTP request is received, my application performs certain tasks and logs the activity to ...

Creating flexible routes with localized paths (i18n) in Next.js without relying on getStaticPaths

I am currently exploring ways to create simple dummy dynamic routes, such as /order/{productid}. While Next offers various strategies, I have identified two potential solutions for my specific needs: Static paths generation: With over 1400 products, st ...

Tips for preserving both existing data and new data within React's useState hook in React Native or ReactJS?

As I dive into learning reactjs, one question that has been on my mind is how to store both previous and upcoming data in useState. Is there a special trick for achieving this? For example: Imagine I enter "A" and then follow it with "B". My goal is to ha ...

Troubleshooting Ajax contact form's failure to refresh (PHP and Bootstrap 4)

I have successfully implemented this code on one website, but it is not working as expected on another website. It sends the email but refreshes the page and redirects to a contact.php page with a message. Despite double-checking everything, including cha ...

Issue encountered during Node.js installation

Every time I attempt to install node js, I encounter the following errors: C:\Users\Administrator>cd C:/xampp/htdocs/chat C:\xampp\htdocs\chat>npm install npm WARN package.json <a href="/cdn-cgi/l/email-protection" class ...

I'm trying to create a horizontal list using ng-repeat but something isn't quite right. Can anyone help me figure out

Feeling a bit lost after staring at this code for what seems like an eternity. I'm trying to create a horizontal list of 2 image thumbnails within a modal using Angular's ng-repeat. Here's the HTML snippet: <div class="modal-body"> ...

Conceal the nearest parent element above using JavaScript (or jQuery)

I need to implement a functionality where clicking a button hides a specific HTML class. The structure of the HTML is as follows: <thead> <tr class="employee-list"> <th> <button class="show-emp">S ...