Home > Enterprise >  How to parse & to & in vue with data from the database
How to parse & to & in vue with data from the database

Time:11-22

I ran into an issue where the data in my database is stored as What is your favorite Play & Earn Mechanic?

When I display it on my Frontend Vue application, it would display &amp as it is... How can I make it to display as &

CodePudding user response:

How did you insert the content?

If you get content from your API/DB which is html encoded you need to inject the html first via v-html directive.

For example having a string:

"Im a Html Encoded <b>string</b> &amp; I want to be displayed"

You you pass it in your html like this:

{{ "Im a Html Encoded <b>string</b> &amp; I want to be displayed" }}

It will be display as you read it here.

But if you do it like this:

<span v-html="'Im a Html Encoded <b>string</b> &amp; I want to be displayed'"></span>

Its displayed correctly. Have a look here: https://vuejs.org/guide/essentials/template-syntax.html#raw-html

Also read the security warning there - if you allow HTML youre vulnerable to XSS

  • Related