18
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Nuxt.js(Vue.js)で良くある【Component template should contain exactly one root element. 】の対処法

Posted at

Nuxt.js(Vue.js)で良くやってしまう【Component template should contain exactly one root element. 】の対処法です。

Component template should contain exactly one root element.
If you are using v-if on multiple elements, use v-else-if to chain them instead.
スクリーンショット 2020-06-09 10.58.23.png

ビルド時に上記のエラーが出る時は<template>タグが原因です。

<template>
  <p>name</p>
  <input type="text">
  <p>email</p>
  <input type="email">
</template>

templateタグの中は一度全体を親要素で囲う必要があります。
上記のコードでは、親要素で囲わずに並列で書いてしまっているのでエラーになります。

<template>
  <div class="user">
    <p>name</p>
    <input type="text" />
    <p>email</p>
    <input type="email" />
  </div>
</template>

divタグで全体を囲むとエラーが解消されます。

nuxtやvueに慣れるまで良く出るエラーなのでメモとして残しておきます。

18
6
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
18
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?