, where htmlCode is a variable or expression containing HTML code. Example:

Hello, World!

Note: v-html will overwrite the element"/>

, where htmlCode is a variable or expression containing HTML code. Example:

Hello, World!

Note: v-html will overwrite the element">

Home  >  Article  >  Web Front-end  >  How to use v-html in vue

How to use v-html in vue

下次还敢
下次还敢Original
2024-05-02 22:18:39682browse

The v-html directive in Vue.js is used to: Insert raw HTML code, independent of the Vue compiler. Use the syntax:

, where htmlCode is a variable or expression that contains HTML code. Example:

Hello, World!

Note: v-html will overwrite the element

How to use v-html in vue

Usage of v-html in Vue

v-html in Vue.js Directives allow you to dynamically insert HTML code into your templates. It is used to insert raw HTML content that is not affected by the Vue compiler.

How to use

v-html The directive requires a variable or expression as a parameter that contains the HTML code to be inserted. The syntax is as follows:

<code class="html"><p v-html="htmlCode"></p></code>

where htmlCode is a variable or expression containing HTML code.

Example

<code class="html"><script>
import { ref } from 'vue';

export default {
  setup() {
    const html = ref('<p><h1>Hello, World!</h1></p>');

    return { html };
  },
};
</script>

<template>
  <div v-html="html"></div>
</template></code>

Note:

  • v-html will overwrite the element Existing content, so please use with caution.
  • Since v-html is inserted directly into HTML, make sure you trust the content you are inserting to avoid security issues. The
  • v-html directive does not compile Vue compiler directives or expressions, so please do not use them in inserted HTML.
  • If you need to insert dynamic content that is affected by the Vue compiler, consider using v-bind:innerHTML instead.

The above is the detailed content of How to use v-html in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:The role of style in vueNext article:The role of style in vue