Components with no specific branding in Vue

Creating a quiz in Vue.js with various question types:

  • Select one
  • Select multiple
  • Select image
  • Match

The challenge lies in the mixing of question types within the same quiz, leading to the use of different components (<x-select-one-question>,

<x-select-multiple-question>
, <x-select-image-question>, <x-match>, etc.) which prevents straightforward insertion into the quiz.

Is there a way to create a generic component for this purpose? Perhaps something along these lines:

<template>
  <div>
    <x-question 
      v-for="(question, index) in questions" 
      :key="index" 
      type="question.type"></x-question>
  </div>
</template>

Answer №1

To dynamically render different components at run time, you can utilize the 'component' type and set the relevant component using the 'is' attribute like so:

<component :is="componentToRender(type)

Within the function, determine which component to return based on a specified criteria, for example:

componentToRender(type){
 case 'quiz1': return 'QuizOneComponent';
// add more cases here
}

Answer №2

You could create a component similar to this:

<template>
<select v-if="type == 'select'">
  <option v-for="item in items" :value="item.value">{{ item.text }}</option>
</select>
<select v-else-if="type == 'multiple'" multiple>
  <option v-for="item in items" :value="item.value">{{ item.text }}</option>
</select>
<input v-else-if="type == 'image'" type="file" id="image" name="image" accept="image/png, image/jpeg">
</template>

Vue.component('question', {
  template: '#question',
  props: {
    type: {
        default: ''
    },
    items: {
      type: Array,
      default: [
        { value: 1, text: 'One' },
        { value: 2, text: 'Two' },
        { value: 3, text: 'Three' }
      ]
    }
  }
});

By passing the correct prop like 'select', 'multiple', or 'image', you can display the desired input field.

<div id="app">
  <question type='image'></question>
</div>

If you'd like to experiment with it, check out this JSFiddle link.

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

Turn off the checkboxes

Is there a way to use JQuery and HTML to disable two checkboxes when one checkbox is enabled? I need help implementing this in MVC 4. Morning @Html.CheckBoxFor(x => x.FromDateMorning, new { style = "width:auto;", id = "chkFromDateAM", ...

Vue.js Element UI dialog box

Is there a method to customize the close button in el-dialog and replace it with my own design? For instance, can I change the default close button located at the top left corner of the dialog? <el-dialog title="Tips" :visible.sync=" ...

Ways to display the result in a new tab

I am using colorbox with a form that will be submitted to another website. I would like for the response from that web page to load in a new tab with a new URL. ...

Discovering the frequency of a specific key in a JSON object or array using JavaScript

Suppose I have a JSON object with the given data, how can I determine the frequency of the key: "StateID"? [{"StateID":"42","State_name":"Badakhshan","CountryID":"1"}, {"StateID":"43","State_name":"Badgis","CountryID":"1"}, {"StateID":"44","State_name": ...

Steps to Perform ajax Request when the Scroller Reaches the Bottom

I have been working on implementing lazy loading for a set of 200 objects in an array. I have access to APIs that allow me to retrieve JSON data from the server by passing index and row count as parameters for the AJAX call. The response includes the reque ...

Is the malfunction due to jQuery 1.3.2, or is there another underlying issue causing it to not work properly

Previously tested and functioning in version 1.7.2, I am aware that this code should function properly. //close the dropdown when clicking anywhere on the page $("html").live("click", function () { closeDropdown(); }); //open the dropdown when clicki ...

The present user who is currently logged into the stackmob platform

I am currently working on retrieving a list of contacts for the logged-in user, but I am struggling to identify the current user. In Parse.com, you would use Parse.User.current(), but I am unsure if Stackmob offers a similar functionality. Below is the co ...

Challenges arise when attempting to integrate the jquery plugin (jlembed) due to conflicts with namespaces

I've been struggling to make jlembed work, but I keep encountering errors in the Chrome debugger. I've tried all of the solutions mentioned in THIS document with no success. Can someone guide me on the correct way to include jQuery and a jQuery p ...

Guide on how to compile template strings during the build process with Babel, without using Webpack

I'm currently utilizing Babel for transpiling some ES6 code, excluding Webpack. Within the code, there is a template literal that I wish to evaluate during the build process. The import in the code where I want to inject the version looks like this: ...

Pairing arrays to their corresponding strings

I am looking to create a simple script for displaying birthday and nameday congratulations. The objective is to: Retrieve the current day. Store employee data in an array. If an employee's name matches the nameday variable, display a congratul ...

How can I access attribute type information in a Node.js Sequelize model?

I have successfully implemented a model in NodeJS using Postgres and sequelize. Let's say the model is Person and it includes fields for name and age. Now, I am looking to dynamically examine the model class to retrieve details about its attributes, s ...

FancyBox: Can You Achieve the Same Scrolling Effect with HTML Fragments Instead of Images?

Is it possible to display HTML fragments instead of images with the same scrolling effect (when using the next() method)? I am struggling to figure out how to make this work. Currently, I am able to display a single HTML fragment using fancybox: $.fancyb ...

Warning happens two times upon performing a right-click

I've got some code that checks the mouse's position, and triggers an alert followed by a redirect if a certain condition is met. It functions correctly, however, I noticed that if you right-click in one area and then left-click in another area t ...

NEXT JS - Application renders twice due to rewrites in place

I'm currently working on a NEXT JS project and facing the issue of the app rendering twice! Challenge After implementing rewrites in next.config.js, the app is being rendered twice. Removing the rewrites solves the issue and only renders once. next ...

Is there a jQuery function similar to toggle that accepts a boolean parameter?

I find myself frequently writing code similar to the following snippet. It essentially toggles an element depending on a certain condition. For instance, in this hypothetical scenario, the condition is "if the checkbox with id agree is checked and the inp ...

Can a file be imported into Node.js without the need for packaging?

Is there a way to have an entire file evaluated in node? For example, let's say I want to evaluate the angular.js source file. An example of what the code could look like is as follows: jsdom = require("jsdom").jsdom; document = jsdom("<html>& ...

The child module invokes a function within the parent module and retrieves a result

Guardian XML <tr [onSumWeights]="sumWeights" > Algorithm sumWeights(): number { return // Calculate total value of all weights } Offspring @Input() onTotalWeights: () => number; canMakeChanges() { return this.onTota ...

What is the most efficient method to execute JavaScript programmatically upon loading an ASP.net page?

Within my global.asax file for an ASP.net project, I have set up a system to check for specific conditions. When these conditions are met, my goal is to automatically run JavaScript code when the page loads. This is the snippet of code I am currently usin ...

When the back button on the browser is clicked, ensure that the customer is returned to the same position

What is my goal? I am looking to store the user's current position on the page so that when they navigate to another page and use the browser's "Back" button, they are taken back to that position. This behavior should only be triggered when the u ...

The functionality of changing the checkbox to "checked" by clicking on the span is not

How can I create a toggle button with a checkbox using css and jquery? Clicking on the span representing the toggle button should change the checked property of the checkbox. Currently, the span does not change the property, even though it triggers the c ...