1

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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.