javascript - Nuxt.js 与 TypeScript : Property 'x' does not exist on type 'y'

标签 javascript typescript vue.js nuxt.js

我是 Vue 和 Nuxt 的新手。目前正在使用 TypeScript 做一个教程。它抛出一堆错误 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.抛出错误。

这是 <script AboutMe.vue block 组件。

<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: ``,
    }
  },

Edit long-sun-7944y

关于javascript - Nuxt.js 与 TypeScript : Property 'x' does not exist on type 'y' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65221545/

相关文章:

javascript - Parse.com 文件未更新,导致数据浏览器的链接损坏

javascript - 计算/异步数据获取

vue.js - 视觉 : Update Component data from Vue instance

javascript - Vuetify v-autocomplete v-text 显示完整对象而不是仅文本

使用私有(private)成员创建 JavaScript 类

javascript - 在普通 JS 插件中可以将对象存储在 DOM 元素上吗?

javascript - 如何更改 chromes 日期解析/排序以匹配 ff/ie 日期解析?

javascript - 手动输入日期格式

javascript - 如何将 TypeScript 与 Loopback 一起使用

javascript - 为什么我应该在 Typescript 中使用接口(interface)