LoginSignup
9
4

More than 5 years have passed since last update.

Vue.jsのコンポーネントのtemplateで複数要素を書いていつもハマってしまう

Last updated at Posted at 2018-11-24

タイトルのとおりで、久しぶりにVue.jsを書くといつもハマってしまうのでメモ。

mycomponentというコンポーネントを作ります。

<div id="app">
    <mycomponent></mycomponent>
</div>

コンポーネントのtemplateで、無意識に次のように複数要素を書いてしまいます。

Vue.component('mycomponent', {
  template:`
    <h1>component</h1>
    <b>hello</b>
  `
})

var app = new Vue({
  el: '#app'
})

で、なんでhelloがでないんだーとなる。

2018-11-24_21h59_28.png

ルート要素は単一でないといけないので、<div>などで囲う必要があります。

Vue.component('mycomponent', {
  template:`
    <div>
        <h1>component</h1>
        <b>hello</b>
    </div>
  `
})

var app = new Vue({
  el: '#app'
})

ちゃんと出ました。

2018-11-24_22h00_37.png

「基礎から学ぶ Vue.js」の「SECTION 22 コンポーネントの定義方法」にしっかり書いてある!

9
4
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
9
4