How to Render HTML Tags in Vue.js from Data Objects Using v-html

preview_player
Показать описание
---

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Vuejs Html tag / attribute in data()

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---

The Problem

Here’s a simplified version of the structure you might be working with:

[[See Video to Reveal this Text or Code Snippet]]

Data Structure

Your Vue component looks like this in the data section:

[[See Video to Reveal this Text or Code Snippet]]

The Solution

1. Understanding v-html and JSX:

Your icon should be a string representation of HTML, enclosing your icon within quotes.

2. Adjusting the Data Structure:

Instead of defining the icon with actual HTML syntax, simply define it as a string:

[[See Video to Reveal this Text or Code Snippet]]

3. Rendering the Icon in HTML:

Change your HTML slightly to display the icon correctly within Vue's rendering engine:

[[See Video to Reveal this Text or Code Snippet]]

4. Optimizations for Dynamic Classes:

If your icons are similar but only differ by classes, a more efficient method is to store only the class in your data and construct the icon element in your template:

[[See Video to Reveal this Text or Code Snippet]]

Then in your HTML, render it as:

[[See Video to Reveal this Text or Code Snippet]]

Conclusion

Рекомендации по теме
welcome to shbcf.ru