How to render HTML tags in Vue template?

When you need to render HTML tags in a Vue template, you can use Vue's built-in directive v-html. This directive allows the parent component to parse and render the data as HTML into the child component. It sounds advanced, but I'll explain the concept using humor and simple examples.

First, let's look at a simple example. Say you have a data attribute that contains HTML code and you want to render it into a page with links and images. You can do this:

<template>  
  <div>  
    <child-component v-html="htmlCode"></child-component>  
  </div>  
</template>  
  
<script>  
import ChildComponent from './ChildComponent.vue';  
  
export default {
      
        
  components: {
      
        
    ChildComponent  
  },  
  data() {
      
        
    return {
      
        
      htmlCode: '<p><a href="https://www.example.com">This is a link</a></p><p><img src="https://www.example.com/image.jpg" alt="Example image"></p>'  
    };  
  }  
};  
</script>

In the above example, we assigned the string containing the HTML code to the htmlCode data attribute. Then, in the template, we use the v-html directive to bind this data attribute to the child component. A child component is a simple

element, which will render the received HTML code onto the page as parsed HTML.

Now, let's look at a more practical example. In the template, we use the v-html directive to render the content attribute value into each table cell. In this way, users can click on the link and jump to the corresponding page.

It should be noted that you need to be careful when rendering HTML code using the v-html directive, as it can allow arbitrary HTML and JavaScript code to be executed on your page. If you are not sure about the reliability of a certain data source, it is a good idea to filter and clean the data to avoid security issues.

item.link contains a link, and item.image contains the HTML code for an image.

Render a template containing dynamic data:

<template>  
  <div>  
    <my-component v-html="dynamicContent"></my-component>  
  </div>  
</template>  
  
<script>  
import MyComponent from './MyComponent.vue';  
  
export default {
      
        
  components: {
      
       MyComponent },  
  data() {
      
        
    return {
      
        
      dynamicContent: `  
        <h1>Dynamic Content</h1>  
        <p>{
       
       { dynamicMessage }}</p>  
        <ul>  
          <li v-for="item in dynamicItems">{
       
       { item }}</li>  
        </ul>  
      `  
    };  
  },  
  computed: {
      
        
    dynamicMessage() {
      
        
      return 'Hello, this is dynamic content!';  
    },  
    dynamicItems() {
      
        
      return ['Item 1', 'Item 2', 'Item 3'];  
    }  
  }  
};  
</script>

In the above example, we used the v-html directive to render dynamicContent into the MyComponent component. dynamicContent contains a dynamic template that uses computed properties and v-for directives to generate list items. This way, we can dynamically generate template content at runtime and render it on the page.

Besides using the v-html directive in tables, you can use it in other situations as well. For example, if you have a dynamically generated list where some items contain links or images, you can use the v-html directive to render the entire list.

Another thing to note is that since the v-html directive parses the data of the parent component as HTML, make sure the data in the parent component is trusted to avoid security issues. If you use an untrusted data source, it may lead to security issues such as cross-site scripting (XSS). To avoid these problems, you can filter and sanitize the data to ensure it only contains safe HTML code.

To sum up, the v-html directive is a very useful directive in Vue, which allows the parent component to parse and render data as HTML to the child component. Extra care needs to be taken when using this directive to ensure that the data is trusted to avoid security issues.

Guess you like

Origin blog.csdn.net/2301_77795034/article/details/131111623