LoginSignup
3
3

More than 3 years have passed since last update.

Vue.jsで外部HTMLファイルをinclude(っぽく)する

Last updated at Posted at 2020-02-28

概要

Componentに分けたいけど分けられない。諸事情でHTMLファイルをincludeしたい。
HTMLファイルを直接弄りたいとか言われましても

方法

  • 外部HTMLファイルを非同期で読み込んで展開する

具体例

<template>
  <main>
    <section v-html="external"></section>
  </main>
</template>

<script lang="ts">
import { Vue, Component } from "vue-property-decorator";

@Component
export default class App extends Vue {
  external: string = "";

  public mounted() {
    this.loadExternalHtml();
  }
  
  public loadExternalHtml() {
    fetch("external.html").then(res => {
      res.text().then(html => {
        this.external = html;
      });
    });
  }
}

includeっぽくはなる。
これでガリガリHTMLを弄ってもおk

3
3
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
3
3