Generating and assembling text with vue.js

One of the requirements states that all HTML elements must be defined in a JSON file and utilized within the template.

In order to meet this requirement, there is a function called "markComplete" that should be triggered whenever a checkbox is changed.

Sample Code:

<template>
<span v-html="htmlContent"></span>
</template>

<script>
 data(){
        return{
        htmlContent: "<input type='checkbox' v-on:change='markComplete'>"
        }
    }
</script>

The code above does not work as expected because the onChange event is not properly mounted, resulting in a

Uncaught ReferenceError: markComplete is not defined
error message.

Are there any alternative methods to ensure that this functionality works correctly?

Answer №1

When attempting to compile the string as Vue Templates using v-html, keep in mind:

The contents are inserted as plain HTML and will not be compiled as Vue templates.

To learn more about v-html, refer to the Vue Docs.

For an alternative solution, check out this informative article

If you prefer not to use a library, consider the code snippet below:

Begin by creating a JavaScript file (ideally named RenderString.js):

import Vue from "vue"

Vue.component("RenderString", {
  props: {
    string: {
      required: true,
      type: String
    }
  },
  render(h) {
    const render = {
      template: "<div>" + this.string + "</div>",
      methods: {
        markComplete() {
          console.log('the method called')
        }
      }
    }
    return h(render)
  }
})

Next, integrate the following code into your parent component:

<template>
  <div><RenderString :string="htmlContent" /></div>
</template>

<script>
import RenderString from "./components/RenderString"

export default {
  name: "App",
  data: () => ({
    htmlContent: "<input type='checkbox' v-on:change='markComplete'>"
  })
}
</script>

Note: While I have not executed the code above, I have prepared a similar functional CodeSandbox Example

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

Guide to building a personalized autocomplete feature in vue js

Presently, I am using the buefy autocomplete feature, but it is causing a few problems. In the DepartmentDetail.vue file <template slot-scope="props"> <div class="container is-fluid"> <b-loading :is-full-page=" ...

What is the best way to trigger a particular function only when a Vue component is called by a specific component, and not by any other components?

I've created a component called select-diagnosis that is utilized by various components. When select-diagnosis is invoked by a specific component known as PtdTreatment, it should execute a particular function within the fetchDiagnosis function. Howev ...

Vue-Router with dynamically created route redirects for Server Side Rendering (SSR)

Our system is designed to handle various types of profile pages, such as user profiles and company profiles, each with its own template. However, the URLs for these profiles do not differentiate between the types. All URLs follow this format: mysite.com/{{ ...

Using Vue.js to Duplicate a Component Across a Page Multiple Times

My objective: I am working on implementing filters that will be displayed on a page to filter the products shown. For mobile devices, I want to hide these filters behind a button which, upon being clicked, will reveal the filters in a side-slide menu. Alt ...

Experience the power of SSR Nuxt.js with a seamlessly integrated REST API backend

Currently, I am working on developing a SSR Nuxt.js application that integrates with a REST API server. In order to achieve this, I have included my endpoint /api into the Nuxt server.js code as shown below: const express = require('express') c ...

Why is TinyMCE removing my custom MPDF HTML elements?

I have a Vue application with a Symfony backend. I am using TinyMCE to edit documents that are generated by mpdf. In the mpdf twig content, I need to add a page footer on certain pages and exclude it on others. To achieve this, I have created custom HTML t ...

Develop a Vue mixin to enable theme switching in a Vue.js application

I have successfully developed three distinct themes: light, default, and dark. Currently, I am working on implementing a toggle function in the footer section that allows users to switch between these themes effortlessly. Following the guidance provided b ...

Component fails to update when attribute is modified

My issue is that the crud-table component does not refresh when I change currentTable. It works when I assign currentTable = 'doctor' in the created() hook, but not here. Why is that? <template> <div id="adminpanel"> <div id ...

Error loading resource in Vue npm run serve: net::ERR_CONTENT_LENGTH_MISMATCH

I am encountering the following error message in Google Chrome console: Failed to load resource: net::ERR_CONTENT_LENGTH_MISMATCH chunk-vendors.js:1 This results in a blank page when attempting to load a Vue development page initiated with: user@ubuntu:~# ...

When executing `npm run start`, a blank page appears exclusively on the server

I recently set up a Vue landing page on my Mac. In the terminal, I navigated to the folder and executed "npm install" and "npm run dev", which ran without any issues. However, when trying to do the same on a managed server, I encountered challenges with t ...

Optimal method for managing user session data in Vue with the power of Laravel and Inertia

I am creating an application that features a dynamic menu using Laravel, Vue, and Inertia. To transmit the session details about the menu items to be displayed (such as icons) and user information (like avatar and name), I currently rely on the HandleIner ...

Why do Computed Setters fail to refresh the View?

In the current view, there are two buttons available for selection: one for the list layout and the other for the group layout. The chosen state needs to be stored either in local storage or a cookie. // Pug Syntax #app span.button( :class="{'a ...

Error: unable to detect click event on table cell

I need help passing a cell value to a function when clicking on the cell. While it's easy to do this with a regular table, I am trying to figure out how to achieve the same result with bootstrap-vue b-table. Here is the code snippet for the table: &l ...

Using VueJS to switch classes on multiple cards

There is a page with multiple cards, each containing its own set of status radio buttons: ok, missing, error. The goal is to be able to change the status of individual cards without affecting others. A method was created to update the class on the @change ...

Troubleshooting: Issue with v-link to classname in Vue.js

This is the code I am working with: <div v-link="'/work/' + {{data.useClass}}" class="itemImg {{data.useClass}}"> When rendered in the browser, it looks like this: <div class="itemImg x50"> The expectation is that class="itemImg { ...

Issues with Vuetify not recognizing style adjustments

Trying to modify the design so that buttons display text with capital letters instead of all uppercase. My version of Vue is 3.5.5 I have included src/stylus/main.styl $button-text-transform = 'capitalize' @require '~vuetify/src/stylus/ap ...

"Linking a mouse click event to a specific function within an array using

I'm faced with an issue while trying to bind a click function from an array in my code. It's not responding as expected. Here is a simple example that illustrates the problem: What can I do to solve this issue? var app = new Vue({ el: &apo ...

Using Vue.js to create numerous modal popups

Currently, I am using Vue.JS for a research project at my workplace. My focus right now is mainly on the front-end. I have a table with several entries, and when a row is clicked, I want a modal popup window to display further details about that specific ...

Attaching multiple elements to events in Vue.js

Can events be attached to multiple HTML elements without the use of components? Example: <ul> <li> <li> <li> <li> </ul> Is it possible to bind a click event to all <li> elements in this scenario? If so, ...

Unable to overwrite class in VueJS component

Recently, I've been encountering an issue with over-riding a specific instance of my VueJS Component. Despite my efforts, the component continues to use the default value. The particular value causing trouble is the buttonClass. Interestingly, the ot ...