Assess the HTML containing v-html injection

Is there a way to inject raw HTML using Vue, especially when the HTML contains Vue markup that needs to be evaluated?

Consider the following example where HTML is rendered from a variable:

<p v-html="markup"></p>
{
    computed: {
        markup() {
            return 'Hello <em>World</em>!';
        },
    },
}

This approach works well, but what if the markup computed property includes a custom Vue component as well?

return 'Hello <em>World</em>! My name is <my-name></my-name>!';

In this situation, <my-name></my-name> will not be evaluated and will be rendered as-is.

How can I make it "Vue-fied" and how does scope work in such cases?

Answer №1

After thoroughly reviewing the documentation, I discovered that achieving this functionality "out-of-the-box" is considered somewhat of an anti-pattern.

However, the issue I am trying to address requires it, as I need to have the ability to insert custom components in various locations within translated text.

To solve this, I decided to utilize Custom Directives.

The only limitation I encountered is that when passing a property like name from the context where the directive is included, it must be referenced using $parent.

return 'Hello <em>World</em>! My name is <my-name :name="$parent.name"></my-name>!';

The usage will look like this:

<p v-markup="markup"></p>

When rendered, the output will be equivalent to:

<p>
    Hello <em>World</em>! My name is <my-name :name="name"></my-name>!
</p>

For more information, check out this Gist: Link to Gist

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

Retrieve the chosen option from a dropdown menu and transfer it to the submit button in PHP

I have a unique situation where I need to extract and store selected values from two separate drop-down menus generated from arrays. The goal is to pass these selected values in the submit button link. For example, if a user selects "123" for email and "t ...

Is it possible for the r.js optimizer to generate a fresh index.html file that links to the compiled files

After using r.js to optimize my project, I'm wondering how to generate a single index.html file that includes just one optimized script and one CSS file. Would I need to manually write this post-build or is there another way to achieve this? ...

Generating components dynamically at runtime following an HTTP request in VueJS

Is there a way to conditionally render VueJS components based on an Http request immediately upon application launch? I want to display component 1 if the response is successful, otherwise display component 2. Additionally, I would like the rendering of th ...

Transferring session data through AJAX in PHP

I'm currently developing an app using PhoneGap. However, PhoneGap only supports HTML, CSS, and JS, not PHP. This led me to the workaround of placing the PHP file on a remote server and using AJAX to call it via the server's URL. My issue now is ...

Implementing pagination with complete page information using node.js and mongodb

Hello, I am currently working on implementing pagination in NodeJS and MongoDB. Check out the code I have written below: router.get("/PO_pagination", verify, async (req, res) => { console.log("req.query", req.query) try { let ...

Chrome Extension: Despite adding it to web_accessible_resources, the resource remains unavailable

I'm currently developing a Chrome Extension with React/Redux and utilizing Webpack. As part of the optimization process, I have started migrating resources to a separate file using the WebPack DLLReference plugin. After generating the dll/dll.vendor. ...

The functionality of Vue.js routes is disrupted when employing the router-link component

I recently worked on a project using Vue.js. In the navbar, I added two menu options - Home and About, which are supposed to direct users to specific pages when clicked. Everything runs without any errors, but for some reason, the router doesn't navig ...

"""Exploring the use of query strings with jQuery for Ajax pagination

Here's the issue I'm facing (apologies for any mistakes in my English): I've been using a library called "Jquery_pagination" and the pagination functions perfectly without any issues. However, there is one problem. When a user clicks on a s ...

What are the steps to showcase a randomly generated number in a live Flot chart using Json?

In my C# page, I have created a random number stored in a json object: if (method == "rnd") { //Random number this.Page.Response.ContentType = "application/json2"; Random rnd = new Random(); int nr = rnd.Next(1, 100); // generates a number ...

What is the best way to send the name of a list item to a different component in React?

Looking for some help with my current project: https://i.sstatic.net/soj4q.jpg I'm working on implementing the 'Comment' feature, but I'm stuck on how to pass the name of a list item to the 'Comment' component header. You c ...

Express Js EJS Layouts encountered an issue: No default engine was specified and no file extension was included

Hey there! I'm currently experimenting with implementing Express EJS Layouts in my application. However, as soon as I try to include app.use(expressEjsLayouts), an error is being thrown. The application functions perfectly fine without it, but I reall ...

Transforming the Date into Local Time: An Instantaneous Process?

I am currently working with a Kendo UI MVC grid that contains three date columns. These dates, which do not include any time values, are stored in the database as local time rather than UTC. The columns within the grid are defined like so: col ...

"Utilize the power of the ajaxform jQuery plugin to automatically reset form fields

Initially, I'd like to emphasize that this is an original inquiry. My predicament is as follows: I have a chatroom php script where I utilize the ajaxForm jQuery plugin to seamlessly send new messages without having to reload the entire page. Howev ...

Updating the project version in package.json using Vue CLI

I'm facing a seemingly simple task, but it's proven to be hard to find the right information online. Most resources I come across talk about updating NPM versions or dependencies, rather than simply changing the project version in package.json. ...

Triggering the body onunload event

I am currently developing a HTA that needs to make final modifications on the onunload event. However, I am facing an issue as the event does not appear to be triggered. Can someone confirm if this event is still supported? Is there an equivalent event in ...

Adding an operation to a function that is linked to an event

On one of my pages, I have a button that triggers some ajax calls with data binding. The binding code is generic and used in various parts of the application, so altering its behavior could impact other users. However, I now have a specific requirement fo ...

Tips for resolving the issue: SyntaxError - Unexpected token import

I've encountered this error in the past and have looked at other solutions, but none of them seem to work for my specific issue. My package.json file includes the following dependencies: "dependencies": { "axios": "^0.15.2", "babel": "^6.5. ...

necessity for a condition in Material UI input field

I need assistance with a function that I use to incorporate Material UI text fields into my code. The issue I'm currently facing is figuring out how to dynamically add the "required" attribute based on a boolean parameter that determines whether the f ...

Updating a global variable in Angular after making an HTTP call

I'm facing a challenge where I have a global variable that needs to be updated after an HTTP GET call. Once updated, I then need to pass this updated variable to another function. I'm struggling to figure out the best approach for achieving this. ...

What are some techniques for streamlining and simplifying complex and repetitive JS/jQuery code?

I've come across this code snippet in a JavaScript file that I need to modify, but it's quite confusing to me. I'm not sure why it has been written this way and I would appreciate some help in understanding its purpose. Can someone break it ...