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

Error in Browserify Express App: Unexpected token while parsing the file

I have been attempting to browserify a javascript file. When I run the command: browserify global.js -o bundle.js An error message is returned: Error: Parsing file C:\ocquiz\public\javascripts\global.js: Unexpected token (756 ...

Storing data from an API response into the localStorage using Vue.js upon clicking

My objective is to save specific data in the browser's localStorage upon clicking a link. However, the output I receive is either undefined or completely empty. <li v-for="(category, index) in categories" :key="index"> ...

Implementing slideDown() functionality to bootstrap 4 card-body with jQuery: A step-by-step guide

Here is the unique HTML code I created for the card section: <div class="row"> <% products.forEach(function(product){ %> <div class="col-lg-3 col-md-4"> <div class="card mb-4 shadow "> &l ...

Testing of onClick event in a React component using styled-components with Sinon spies

Utilizing sinon to test a react component and verify that an onClick function is triggered. Struggling to target the element to click on due to the use of styled-components. Encountering this error message: The "simulate" method should only be run on ...

What are the steps for implementing responsive design animation in my particular scenario?

Can someone please help me with a strange bug in Chrome? I am trying to create a responsive design for my app. The width of the 'item' element should change based on the browser's width. It works fine when the page first loads in Chrome, b ...

When using jQuery to add an item to a List, it disappears immediately after

I am facing an issue with the code I have. Whenever I click on the "Add" button to add an item, it briefly appears in the list but then disappears. I'm having trouble figuring out why this is happening. $(document).ready(function() { $("#submitB ...

ways to disrupt a timeout

I am attempting to incorporate a functionality similar to this example: https://www.w3schools.com/jsref/met_win_cleartimeout.asp On my personal website: If you enter certain characters in the input field, select one of them, and then pause for 5 seconds, ...

Attempting to use jQuery AJAX to submit data without navigating away from the current webpage

Trying to implement the solution provided in this post where I am trying to send data to a PHP page and trigger an action, but unfortunately, it seems to just refresh the page without any visible outcome. Even after checking the network tab in the element ...

What is the solution to resolving the error message "Uncaught ReferenceError: Pusher is not defined" in Vue.js 2?

Whenever I try to launch my application, the console shows the following error: Uncaught ReferenceError: Pusher is not defined and Uncaught ReferenceError: App is not defined Despite running the following commands in my terminal: npm install and ...

Guide on incorporating a dropdown menu within a Jssor slider

I am facing an issue with trying to include a dropdown menu inside the Jssor slider. The menu does not drop down when placed inside it. Here is the code snippet: <head> <script src="jquery.js"></script> <script src="jssor.slider.min. ...

Grab the current URL using useRouter in a Next.js app

I am using userouter to obtain the URL of the current page and then utilizing the clipboard to copy it. However, I am encountering an issue where the copied content shows as object object instead of the expected URL. Can someone please help me identify w ...

Is Apollo & GraphQL caching a better alternative to using Vuex?

I've been diving into Apollo & GraphQL recently. I've come across the idea of leveraging the cache instead of relying on Vuex - but I'm struggling to understand how to do this without creating some sort of anti-pattern by mixing the two toge ...

Explain the process of data binding with DOM elements

I'm intrigued by how jQuery has the ability to link arbitrary data with any DOM element without the use of HTML data attributes. $('#div_id').data('suffix', (count++)); In the Firebug HTML snapshot, I don't see any data attr ...

Is there a way for me to instruct jQuery to revert an element back to its initial value, prior to any dynamic alterations?

My question is regarding changing the content of a div using the `html()` method in JavaScript. I am currently working on implementing a "cancel" button and would like to revert the content back to its original value without having to completely reset it w ...

How can I identify duplicate values within two distinct javascript objects?

Consider two sets of JavaScript arrays containing objects: var allUsers=[{name:"John",age:25},{name:"Emily", age:30},{name:"Michael",age:22}] and var activeUsers=[{name:"John",status:"active"},{name:"Sarah", status:"active"}] I am attempting to iterat ...

Using Handlebars.js to conditionally display data within an object is not functioning as expected

I am attempting to retrieve JSON data value and only display the element if the data is present. However, I am experiencing issues with Handlebar JS. var data = { listBank: [ { "enableSavedCards":"false", "enableAxisAccount":"t ...

Tips on moving a square along the z axis in three.js

Can anyone provide assistance with translating a square on the z axis in three.js? I would appreciate any examples or guidance on the best approach to achieve this. ...

What is the best way to change a date-containing string into a Json object?

I need to convert a string into a JSON Object using JavaScript. However, when I do so, the date in the original string gets completely changed. Here is the string I am working with: var JsonData=[[2013-02-27,787],[2013-02-26,131],[2013-02-02,0],[2013-01- ...

Ways to extract the ASP.net variable value and place it inside a JavaScript textbox

Currently, I'm in the process of developing Javascript code for an ASP.net page. Within my coding framework, the string "foo" is linked to a string variable called myString. In order to transfer the value of myString to a JavaScript variable, I incl ...

What is the best way to display three unique maps simultaneously on separate views?

In this scenario, I have incorporated three separate divs and my goal is to integrate three maps into them. The javascript function that controls this process is as follows: function initialize() { var map_canvas1 = document.getElementById('map_canva ...