类型'y'上不存在类型为:属性:的Nuxt.js [英] Nuxt.js with TypeScript: Property 'x' does not exist on type 'y'

查看:0
本文介绍了类型'y'上不存在类型为:属性:的Nuxt.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Vue和Nuxt的新手。目前正在用打字稿做教程。它向我抛出了一堆错误,Property 'x' does not exist on type 'y'.下面是一个例子;

ERROR in components/AboutMe.vue:56:27
TS2339: Property 'routes' does not exist on type '{ components: { CButton: any; }; props: { user: { type: ObjectConstructor; default: undefined; }; }; data(): { expand: boolean; showTitle: boolean; showReadMore: boolean; routes: string[]; compiledBio: string; }; beforeMount(): void; mounted(): void; }'.
    54 |   },
    55 |   mounted() {
  > 56 |     this.showTitle = this.routes.every((r) => this.$route.name !== r)
       |                           ^^^^^^
    57 |     if (this.$route.name === `users-userSlug`) {
    58 |       this.expand = true
    59 |       this.showReadMore = false

其他有Property 'showTitle' does not exist'expand'等。基本上,this.的所有内容都会引发错误。

这是AboutMe.vue组件的<script块。

<script lang="ts">
import { CButton } from '@chakra-ui/vue'

export default {
  components: {
    CButton,
  },
  props: {
    user: {
      type: Object,
      default: undefined,
    },
  },
  data() {
    return {
      expand: false,
      showTitle: false,
      showReadMore: true,
      routes: [`users-userSlug-posts`, `users-userSlug`],
      compiledBio: ``,
    }
  },
  mounted() {
    this.showTitle = this.routes.every((r) => this.$route.name !== r)
    if (this.$route.name === `users-userSlug`) {
      this.expand = true
      this.showReadMore = false
    }
  },
}
</script>

请帮帮我,我做错了什么?


nuxt.js(v2.14.6)


编辑1:回复@BoussadjraBrahim

谢谢,我添加了Vue.extend({}),现在大多数错误都消失了。但其中一些仍然存在。

ERROR in components/Description.vue:138:10
TS2339: Property 'showAboutUs' does not exist on type 'CombinedVueInstance<Vue, unknown, unknown, unknown, Readonly<{ post: any; }>>'.
    136 |   },
    137 |   mounted() {
  > 138 |     this.showAboutUs = this.$route.name !== `users-userSlug-posts`
        |          ^^^^^^^^^^^
    139 |   },
    140 |   methods: {
    141 |     open() {

ERROR in components/Description.vue:142:12
TS2339: Property 'isOpen' does not exist on type 'CombinedVueInstance<Vue, unknown, unknown, unknown, Readonly<{ post: any; }>>'.
    140 |   methods: {
    141 |     open() {
  > 142 |       this.isOpen = true
        |            ^^^^^^
    143 |     },
    144 |     close() {
    145 |       this.isOpen = false
export default Vue.extend({
  data() {
    const showAboutUs: Boolean = false
    const isOpen: Boolean = false

    return {
      showAboutUs,
      isOpen,
    }
  },
  mounted() {
    this.showAboutUs = this.$route.name !== `users-userSlug-posts`
  },
  methods: {
    open() {
      this.isOpen = true
    },
    close() {
      this.isOpen = false
    },
  },
})

推荐答案

若要获取类型推断,应使用Vue.extend({})创建组件:

<script lang="ts">
import { CButton } from '@chakra-ui/vue'

import  Vue from "vue"

export default Vue.extend({
  components: {
    CButton,
  },
  props: {
    user: {
      type: Object,
      default: undefined,
    },
  },
  data() {
    return {
      expand: false,
      showTitle: false,
      showReadMore: true,
      routes: [`users-userSlug-posts`, `users-userSlug`],
      compiledBio: ``,
    }
  },
  mounted() {
    this.showTitle = this.routes.every((r) => this.$route.name !== r)
    if (this.$route.name === `users-userSlug`) {
      this.expand = true
      this.showReadMore = false
    }
  },
})
</script>

我建议您键入数据属性以强制组件类型:

 data() {
    const routes:Array<string>: [`users-userSlug-posts`, `users-userSlug`];
  // do the same thing with the other properties
    return {
      expand: false,
      showTitle: false,
      showReadMore: true,
      routes,
      compiledBio: ``,
    }
  },

这篇关于类型&#39;y&#39;上不存在类型为:属性:的Nuxt.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆