3
1

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のコンポーネントが自動で読み込まれない件

Last updated at Posted at 2021-05-08

はじめに

componentsの自動読み込みで躓いたので備忘録を残します。

[対象]
Nuxt.js v2.15〜

目次

  1. componentsの自動読み込
  2. 自動読み込みの設定
  3. ネストになっている場合
  4. [参考]

componentsの自動読み込み

Nuxt.jsの自動インポートはv2.13から新しく追加された機能です。
componentsを読み込ませたい対象ファイルで、<ファイル名 />とするだけでimportせずとも読み込むことができ、import地獄から抜け出せる優れものです。

スクリーンショット 2021-05-08 15.06.48.png
このようにするだけでwelAppBar.vueファイルを読み込むことができます。素晴らしいです。

components自動読み込みの設定

スクリーンショット 2021-05-08 15.14.02.png
export default内で上記のようにして自動読み込みを有効化します。最後にカンマを忘れずにつけましょう。たったこれだけで先ほどのように<ファイル名 />で呼び出すことだできます

ネストになっている場合

Nuxt.js v2.15以上ではcomponentsファイルがネスト構造になっている場合、呼び出し時にcomponents以下のディレクトリ名も記載しなくてはいけません。(ここで躓きました)
自動読み込み機能が追加された当初はディレクトリ名は記載しなくても良かったのですが、v2.15以上からの仕様となっているので気をつけて実装していきたいです。

components/base/foo/Button.vue
スクリーンショット 2021-05-08 15.22.15.png

参考文献

-Nuxt.js公式

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?