LoginSignup
24
20

More than 5 years have passed since last update.

Vue.js で、HTMLタグ文字列を展開して表示する(テンプレート構文)

Posted at

概要

"<h1>hoge</h1>" のような文字列をを HTML タグと認識させて表示させたくなった。
その方法をまとめた。

実現イメージ

スクリーンショット 2018-01-31 9.05.05.png
"<h1>hoge</h1>" が展開されている。
(vue.js っぽく、textbox に入力された文字列をそのままバインディングして表示している。)

ソースコード

<template>
  <div>
    <input type="text" v-model="msg" >
    <p v-if="msg.length > 0">
      {{msg}}
      <br/>
      <span v-html="msg"></span>
    </p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      msg: '<h1> hoge </h1>',
    };
  },
};
</script>

解説

v-html="" というもので表示させたいHTMLタグ文字列を指定すると、展開して表示される。

参考

https://jp.vuejs.org/v2/guide/syntax.html
公式。

24
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
20