extract personalized label from data endpoint

I am encountering an issue with the MyComponent.vue component:

<template>
  <div v-html="content"></div>
</template>

<script>
  import Vue from 'vue'
  import CustomComponent from 'CustomComponent.vue'
  Vue.use(CustomComponent)

  export default {
    name: `my-component`,
    computed: {
      content() {
        return this.$store.state.content
      }
    },

    asyncData({store, route: {params: {id}}}) {
      // fetching 'content' from REST API here
      return store.dispatch('FETCH_CONTENT', {id})
    },


    // ...
  }
</script>

The html-string this.$store.state.content is obtained from a REST API. For example:

<custom-component data-count="1"></custom-component><p>some text</p>

In my case, the custom-component tag is not rendering.

My question is whether it is possible to render a vue component from the html-string retrieved from a REST API when using it in v-html?

Answer №1

I was looking for a Vue version with compiler to implement the code snippet below:

<div id="app">
  <h3>
    Vue component creator
  </h3>
  Template
  <textarea v-model="template"></textarea>
  Options
  <textarea v-model="options"></textarea>
  <button @click="create">
    Create component
  </button>
  <component :is="resultComponent"></component>
</div>


Vue.component('test',{
    template:'<div>It works!</div>'
})
new Vue({
    el:'#app',
  data(){
    return{
        stuff:'stuff',
      template:
`<div>
  <test></test>
  <b>{{stuff}}</b>
  <div v-for="n in 10">{{n}}</div>
</div>`,
            options:
`{
  data(){
    return {stuff:'awesome'}
  }
}`,
      resultComponent:undefined
    }
  },
  methods:{
    create(){
        let component = new Function('return' + this.options)()
      component.template = this.template
      this.resultComponent = component
    }   
  }
})

Link to the fiddle: https://jsfiddle.net/Herteby/9syt27ja/

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

Vue.js component communication issue causing rendering problems

When it comes to the Parent component, I have this snippet of code: <todo-item v-for="(todo, index) in todos" :key="todo.id" :todo="todo" :index="index"> </todo-item> This piece simply loops through the todos array, retrieves each todo obj ...

hashSync function needs both data and salt to generate the hash

I can't figure out why I am encountering this issue, I have checked the documentation and couldn't find my mistake. Any suggestions? Error: data and salt arguments required const {create} = require('./user.service'); const {genSaltS ...

Updating data in a VueJS Single File Component

I'm finding myself in a bit of a maze when it comes to understanding how data functions within single file components for VueJS. For instance, in a file named test.vue, my understanding is that you would script something like this: export default { ...

Finding the value within a specified range of two numbers in Vue.js

When my code generates a result of 0, I need to determine the corresponding value based on the basic_salary. For example, if the basic_salary is 40,000, it should return the value of "ee": 400. <table class="table table-hover table-borde ...

Creating a seamless scrolling experience with a designated stopping point - here's how to achieve it!

I understand how to implement a scroll effect on an element with a specific class or ID. However, I am unsure of how to make the scrolling stop 20px above that element. I have seen examples using document.getElementById() to achieve this: function scr ...

Striving to implement a dynamic dropdown menu using React's <select> element and rendering users from an array as selectable options

I am currently working on a project to develop an application that allows users to be added to a MongoDb database and have their names displayed in a dropdown menu. While creating the form, I encountered two issues - one related to the closing tag error a ...

Utilizing model associations in Sails.js to populate an array

I am currently using "sails": "~0.10.0-rc5", "sails-mongo": "^0.10.0-rc3". I have two models: Invoice and Item. Invoice model Invoice: { name: 'sample 1', items: [1,2] // 1,2 are the ids of the Item model } Item model Item { id: 1 ...

In the event of a 404 error, simply direct the user to the pageNotFound before ultimately guiding them back

I'm developing a website with Node JS and I want to implement a feature where if the user attempts to navigate to a non-existent page, they are redirected to a "Page Not Found" message before being automatically taken back to the home page after a few ...

Error message displayed: "An error occurred while processing the VueJS InertiaJS Uncaught (in promise) TypeError. It seems that the property 'search'

Currently, I am working on a Vue JS project with Inertia. One of the features I am implementing is a list that allows users to filter by name. data() { return { selectedUser: this.value, selected: null, search: & ...

karma - Plugin not located

Attempting to run JS test cases using karma, but consistently receiving a plugin not found error. Oddly enough, the same configuration file works perfectly for one of my co-workers. Below are the logs: $ karma start karma.conf.js 04 10 2016 17:51:24.755 ...

Login should only be tried when the error code is 403

I have encountered an issue with checking if the API token is expired. The process involves making a GET call, and if a 403 error is received from the API, then re-login is required. This is what I tried: app.get = async (body) => { return new Pro ...

"An issue arises where the bokeh plot fails to render when generating a

I have been working on embedding a bokeh plot within a webpage served by a simple Flask app. I am using the embed.autoload_server function that I found in the bokeh embed examples on github. While everything seems to be functioning correctly on the Python ...

What is the reason for object destructuring not functioning properly in styles?

Why is it that the HTMLelement is considered an object, while the style is also an object, yet this code fails to work as expected? When I remove destructuring, everything functions properly, so I am curious to understand the underlying reason behind thi ...

Revamp the style of the date field in a Material UI dialog

I'm currently working on a React project where I am using Material-UI's date picker component to display date items. However, I find that the default style does not meet my requirements. I would like to use a selector for displaying dates in the ...

Exploring the VueJS method to retrieve the input field value upon focus

Hey there! I’m currently working with an input field on my website. <input @focus="showTimeAvailabilityModal(this)" class="form-control" v-model="date1" placeholder="First Date & Time Preference"/> Whenever the input field is focused, I trigg ...

Having trouble with window.setInterval in AngularJS?

My coding snippet involves the use of the setInterval method: function MyController($scope) { $scope.clock = new Date(); var updateClock = function() { $scope.clock = new Date(); }; setInterval(updateClock, 1000); }; The HTML asso ...

Dragging an image in KineticJS gets stuck after the drag operation is completed

My experience with KineticJS has been positive so far, but I have encountered an issue with dragging images. In the example below, I have set the image to be draggable, but after the initial drag, it seems like the image can't be moved again. var sta ...

Incorporate different courses tailored to the specific job role

https://i.stack.imgur.com/iGwxB.jpg I am interested in dynamically applying different classes to elements based on their position within a list. To elaborate: I have a list containing six elements, and the third element in this list is assigned the class ...

How can I implement the dynamic loading of components using the Vue 3 composition API?

My goal is to dynamically load a component with the name retrieved from the database, however, I keep encountering the error [Vue warn]: Component is missing template or render function. import SimpleQuestions from '@/components/SimpleQuestions.vue&ap ...

Tips for editing bootstrap-vue table columns using non-Latin characters?

I need to create a table using the Cyrillic alphabet, but the keys of the object must be in the Latin alphabet within the program. Example : export default { data() { return { wmsFields: ['№', 'Наименование', ...