Guide: Displaying components within a Vue2 string

Within my Vue2 component, I am working with a string that looks something like this:

<template>
    <div><h1>Hello World</h1>
    <div v-html="testString"></div>
    </div>
</template>
<script>
    export default {
        name: "test-component",
        props: ['incoming'],        
        data: function() {
            return {
                testString: "";
            }
        },
        created() {
            this.testString = this.incoming;
        }
    }
</script>

After setting up the imports properly, I include the following in another template:

<template>
    <text-component :incoming="mycontent"></text-component>
</template>
<script>
import 'test-component' from './path/to/test-component.vue'
export default { // etc, just presume this bit is right, it's only psudo code
components: ['test-component'],
data() { return { mycontent: '' }},
created() {
    this.mycontent="I just want to <router-link to='/home' v-html='Go Home'></router-link>"
</script>

Now, my goal is for the first template with testString to display the <router-link> as if I had manually inserted it.

I have attempted using v-html and {{ }}, within my test-component but it seems like I may be overlooking something. In Vue1, I believe I would utilize {{{ }}}.

If anyone can provide guidance on this issue, I would greatly appreciate it. Thank you.

Answer №1

According to the explanation of v-html:

Keep in mind that the contents are inserted as raw HTML - they won't be processed as Vue templates.


It is recommended to utilize a slot instead.

Add the slot into your testComponent:

<template>
  <div><h1>Hello World</h1>
  <slot></slot>
</template>

Subsequently, input the content directly within the test-component tag:

<template>
  <test-component>
    I simply want to <router-link to='/home' v-html='Go Home'></router-link>
  </test-component>
</template>

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

What is the best way to partially change a CSS class name in Vue?

Imagine you have this HTML snippet <div class="progress-bar w-90"></div> and now you want to replace the value 90 with an object from Vue. Below is the attempt, but the syntax seems off: <div :class="progress-bar w-{{ progres ...

Guide to implementing Google Adsense on a page post-load using Angular 6

Hi there, I recently completed my website www.revlproject.org and now I'm trying to get approved for Google Adsense. However, I keep receiving the message "valuable inventory: no content." After some investigation, I realized that because my site is b ...

Exchange of Fusion Chart

Attempting to perform a fusion chart swap with asp has proven to be challenging. The alternative approach involves rendering the fusion chart first and then using onmousedown to replace the chart with an image. However, this method is also encountering i ...

Managing form submissions in React with inputs spread across multiple components

I've been working on a ReactJS project with a form that is divided into multiple components. The main component imports all the child components, each containing input boxes, along with a submit button: My issue lies in trying to get all the componen ...

Troubleshooting difficulties integrating NodeJS with R using an R script

I have been attempting to execute an R-script from a js-file but I am consistently receiving a null value as the return. Here is my folder structure: -R_test --example.js --helloWorld.R The contents of example.js are as follows: var R = require(" ...

Creating a two-dimensional canvas within a div element using the console

I have some code that currently generates an image in the console when I hit F12. However, I would like to display this image inside a more user-friendly area such as a div on the webpage. I attempted to create a <div class = "mylog"> to place the i ...

Modify the values of an object by utilizing the setter function

How can I utilize the setter method to update existing values of an object and perform mathematical operations? var obj = { set model(object) { //method's logic } }; obj = {x:10, y: 20, p: 15}; obj = { x:10, y: 20, p: 15 set mod ...

Can you upload a file using the MaterialUI Upload Button in React and then send it to Firestorage?

Hi everyone! I recently implemented MaterialUI's Upload Button in my project, which you can find here: https://material-ui.com/components/buttons/ Below you will see the code snippet where I have integrated this button and now I am trying to upload a ...

Horizontal rule located within a table but spanning the entire width

I wrote the following code: <table> {item.awards.map((obj,i) => <tbody> <tr> <td>Name</td> <td>:</td> <td>{obj.title}</td> </tr> ...

Get the Vue.js package from Node.js by downloading the zip file

I am having trouble downloading a zip file from nodejs using vuejs. The issue I am facing is that an odd underscore appears around the fileName when the dialog box pops up. If I manually set the fileName like this: const fileName = "xmlFile.zip"; Then t ...

JavaScript: The hyperlink provided in the data-href attribute is not functioning properly within the carousel

I have a carousel with images that I want to link to specific pages: JS Fiddle. My goal is to have each image in the carousel direct users to a different webpage when clicked. For example: Clicking on the wagon image should go to wagon.com Clicking on th ...

Encountering a 404 error when submitting a netlify form in my application built using VueJs 3, Vite, VueRouter,

I am facing an issue with implementing a form on my ContactView.vue page src/views/ContactView.vue. I want the form to redirect to a custom success page src/views/SuccessView.vue. <form name="contact-form" action="/success&qu ...

Utilize a jQuery class selector to retrieve the value of a textbox through HTMLHelper

How can I retrieve the value of "StudentID" in a JavaScript function using jQuery? HTML: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Student.Models.vwStudent>>" %> <div class="divClass"> ...

Determine the precise x and y coordinates of a centered element using JQuery

How can I determine the exact left and top positions of an element? The parent container has text-align: center, causing potential confusion when there are multiple elements on the bottom row. For instance, the first one may have a position of 0px instea ...

Dynamic rule addition with JQuery Validation

I am searching for a method to dynamically incorporate the jQuery validation rules, as they are needed in various locations. For instance, a user can edit news in two different ways. The first method involves using a standard form with jQuery validation, ...

Exploring the world of Vue.js and Quarkus: creating, developing,

As a beginner, I have many questions, but one that is currently on my mind relates to a project I am working on. The company I am with is developing a program with the backend built in Quarkus (Java) and the frontend in Vue.js. My query is whether these tw ...

How to access variables with dynamic names in Vue.js

I'm curious if it's possible to dynamically access variables from Vue’s data collection by specifying the variable name through another variable. For instance, consider the following example: Here are some of the variables/properties: var sit ...

Execute jQuery after Angular has completed its loading process

I'm currently working on making some small adjustments to an existing website. This website was originally created using Angular. While my code is written with jQuery, I do have the flexibility to use any type of JavaScript necessary. Despite this, ...

Adjust the Bootstrap date-time-picker to accommodate various languages, while ensuring that the selected date and time are displayed in English

As I work on my app, I've implemented localization support for multiple languages. When initializing the datetimepicker in the app, I provide the current language like this: datetimepicker({ locale: languageObj.language, format: 'DD MMM ...

Only authenticated users are permitted to write to Firebase databases

Currently, I am in the process of setting up a new Vue JS project for a blog with Firebase integration. The main objective is to allow any logged-in user to create blog posts that are saved in the Firebase real-time database at https://xxx.firebaseio.com/b ...