Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render html from database in Vue

I am making a blog app using Vue.js for frontend and Node.js for backend.

I am using a rich text editor (vue2-editor) in frontend for users to create the content of the blog. I want to store that content in the database (MySQL) and retrieve it. Currently whatever content I am storing is going as a plain HTML.

<p><strong>This is Blog Heading</strong></p><p><br></p><p><u>This is underlined</u></p><p><br></p><p>This is start of the paragraph</p>

And after retrieving from the database it renders is as a string and not HTML

How can I get it to display the HTML properly?

like image 833
Jack Giratina Avatar asked Jan 08 '20 08:01

Jack Giratina


People also ask

How does Vue render HTML?

Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance's data. All Vue templates are syntactically valid HTML that can be parsed by spec-compliant browsers and HTML parsers.

How do I add HTML to Vue?

The simplest way to get started with Vue is to grab the development version script for it and add it to the head tag of your HTML file. Then you can start Vue code inside the HTML file inside of script tags. And have the Vue code connect up with an existing element on your HTML page.

Does Vue sanitize HTML?

What if you want something in between? This is where vue-sanitize comes in. Not only will it allow you to use a whitelist of "safe" HTML tags, it will remove disallowed tags rather than escaping them.

What is $V in Vue?

If you have $v in your HTML codes, definitely, the code makes use of Vuelidate for form validations. $event is a special object used to store and retrieve events by Vue.js .


2 Answers

Use v-html to display it as html and not a string.

You can read more about it here:

https://v2.vuejs.org/v2/guide/syntax.html

Example:

<div v-html="htmlFromDb" />
like image 132
T. Short Avatar answered Sep 28 '22 02:09

T. Short


Providing you a solution as per my knowledge hope it will help you. Please have a look on code.

<p><strong>This is Blog Heading</strong></p>
<p><br></p>
<p><u>This is underlined</u></p>
<p><br></p>
<p>This is start of the paragraph</p>
like image 44
Ankit Rathore Avatar answered Sep 28 '22 04:09

Ankit Rathore