Vue Html

Posted on  by admin


Modified2 years, 11 months ago. How is it possible to add elements dynamically to the content? Problem is that if I press the link no action will fire.

Vue do not see new elements. 22 gold badges88 silver badges2323 bronze badges. 2323 bronze badges.


Update: Based on this answer, you can do a similar dynamic-template component in Vue 2.

  • You can actually set up the component spec in the computed section and bind it using :is. Vue bindings don't happen on interpolated HTML.

You need something Vue sees as a template, like a partial. However, Vue only applies bindings to a partial once; you can't go back and change the template text and have it re-bind.


So each time the template text changes, you have to create a new partial.

There is a tag/element you can put in your HTML, and it accepts a variable name, so the procedure is:.

the template HTML changes. register new partial name for the new template HTML. update name variable so the new partial is rendered.

It's a little bit horrible to register something new every time there's a change, so it would be preferable to use a component with a more structured template if possible, but if you really need completely dynamic HTML with bindings, it works.

The example below starts out with one message, link-ified as per your filter, and after two seconds, changes message. You can just use message as the name of the partial for registering, but you need a computed that returns that name after doing the registering, otherwise it would try to render before the name was registered.

v-bind Shorthand

88 gold badges6969 silver badges9595 bronze badges. 9595 bronze badges. I just learned about $compile, and it seems to fit your need very nicely.

A very simple directive using $compile avoids all the registrations.

88 gold badges6969 silver badges9595 bronze badges. 9595 bronze badges. 44 gold badges2525 silver badges4545 bronze badges2. 4545 bronze badges. The best solution I found which works fine with custom html is looks like this, it's like you kind of create new component each times the html property changes.

No actually one did this, we just use computed property for creating new component. That is how it looks:.

I did not check it for memory efficiency, but it looks like works just fine. 33 gold badges1919 silver badges2424 bronze badges.

2424 bronze badges. Since partial has been removed from VueJS 2 (


A better way may be to create a component which processes its content and create appropriate DOM elements. The above component will replace hashtags by clickable links.

Modified version of @RoyJ's answer, works in Vue.js v2.6.10.

44 gold badges2525 silver badges4545 bronze badges. 4545 bronze badges. Though it’s not that often, sometimes there is actually a good reason for needing to insert raw HTML stored as strings into the DOM.

Very rarely, of course. In most cases you should never do this, as this opens you up to a variety of XSS attacks. A somewhat valid use-case might be if you’re writing a new front-end component for an ancient legacy system that (shudders) stores HTML mixed with data in an aging database in the long-forgotten server room in some leased facility upstate.

In that case, you might have to resort to rendering raw HTML in your app. So let’s get started. First things first, break out your RegEx skills ….

I’d rather not risk the fate of the known universe just to accommodate a legacy system. Anyway, it turns out that Vue provides us with a nice little directive that can handle all this for us. Predictably, it’s called v-html.

To use it, pass a reference to a HTML string in your data model to v-html in your component template, like so:. And just like that, your HTML will be rendered into the component.

If you update the legacySystemHTML property, the HTML will update accordingly. Also, here’s a little somewhat-related secret. You can use the v-text property the same way, except it only sets the text content of an element.

No idea why you’d use that when you could just use Mustache expressions ({{ componentInnerText }}) instead, but it’s there!

You’re browsing the documentation for v2.x and earlier.

For v3.x, click here. Vue.js uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying Vue instance’s data.


All Vue.js templates are valid HTML that can be parsed by spec-compliant browsers and HTML parsers.

Under the hood, Vue compiles the templates into Virtual DOM render functions. Combined with the reactivity system, Vue is able to intelligently figure out the minimal number of components to re-render and apply the minimal amount of DOM manipulations when the app state changes.

If you are familiar with Virtual DOM concepts and prefer the raw power of JavaScript, you can also directly write render functions instead of templates, with optional JSX support.