0
0

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-property-decorator コンポーネントを読み込まなくても@Componentは書きましょう

Posted at

前提

  • @nuxtjs/axios: ^5.10.3
  • nuxt: ^2.0.0
  • nuxt-property-decorator: ^2.7.2
  • Typescript 3.8.3

エラー内容

下のコードをyarn devで動かすと
Cannot set property 'components' of undefined
とエラーが出て動かない.

sample/index.ts
<script lang="ts">
import Vue from "vue";
import { Component } from "nuxt-property-decorator";
import { Context } from "@nuxt/types";

export interface FactResponse {
  id: string;
  text: string;
  type: string;
}

export default class index extends Vue {
  facts!: FactResponse[];

  async asyncData(context: Context): Promise<object | void> {
    const response = await context.app.$axios.$get(
      "https://cat-fact.herokuapp.com/facts"
    );
    const facts = response.all;

    return { facts };
  }

  get filterResponse(): FactResponse[] {
    return this.facts.filter(f => {
      return f.type == "cat";
    });
  }
}

エラーの原因

Component宣言が抜けていた.
Componentを外部から読み込んでいなくても書かないといけない.
自分は無意識に2回経験しました...

解決策

+ @Component({ computed: {} })

sample/index.ts
<script lang="ts">
import Vue from "vue";
import { Component } from "nuxt-property-decorator";
import { Context } from "@nuxt/types";

export interface FactResponse {
  id: string;
  text: string;
  type: string;
}
+ @Component({ computed: {} })
export default class index extends Vue {
  facts!: FactResponse[];

  async asyncData(context: Context): Promise<object | void> {
    const response = await context.app.$axios.$get(
      "https://cat-fact.herokuapp.com/facts"
    );
    const facts = response.all;

    return { facts };
  }

  get filterResponse(): FactResponse[] {
    return this.facts.filter(f => {
      return f.type == "cat";
    });
  }
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?