Home > Mobile >  Eval variable as HTML in vue
Eval variable as HTML in vue

Time:03-13

It might be basic question,however I can't find the answer and I have no idea it is possible or not.

I am not familliar with vue

What I want to do is like this

 <html>
 [[parse()]]
 </html>

new Vue({
 methods: {
  parse(){
    return "<div>test</div>"
  }
 }
});

Normally it shows the <div>test<div> as strings not html tags,

However I want to eval this return variable as html.

Is it possible?

CodePudding user response:

Use the v-html directive:

new Vue({
  el: '#app',
  methods: {
    parse() {
      return '<div>test</div>'
    }
  }
})
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<div id="app">
  <div v-html="parse()"></div>
</div>

Make sure you read the warnings.

CodePudding user response:

Assuming that you're not using unconstrained user input in your HTML generation which could leave your page vulnerable to injection attacks, you can use the v-html directive for this:

<div v-html="parse()"></div>
  • Related