【问题标题】:Nuxt.js with TypeScript: Property 'x' does not exist on type 'y'带有 TypeScript 的 Nuxt.js:类型“y”上不存在属性“x”
【发布时间】:2020-12-09 17:08:38
【问题描述】:

我是 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. 的东西都会抛出错误。

这是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
    },
  },
})

【问题讨论】:

  • 这意味着这没有“路线”。您将路由嵌入到数据方法的返回中。如果要将它们作为 this.routes 访问,则必须将它们作为属性添加到 AboutMe.vue 组件中。或者,您可以执行 this.data().routes
  • 如何将它们添加为属性?
  • 看起来你在使用 Vue 组件类型的东西。你需要告诉 typescript 这个对象是一个 vue 组件。我不了解 Vue,所以我无法真正帮助您。

标签: javascript typescript vue.js nuxt.js


【解决方案1】:

要获得类型推断,您应该使用 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: ``,
    }
  },

【讨论】:

  • 非常感谢!我一直在寻找这个几个小时。但你是怎么知道的?它在文档中的哪个位置进行了说明?
  • @boussadjra-brahim 一个问题:是否应该在所有组件上执行此操作?我以为 vue-shim.d.ts 文件中的设置可以做到这一点。
  • @JG_GJ 这应该为所有组件完成
【解决方案2】:

尝试使用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,
    },
  },
...
</script>

希望对你有所帮助

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 2020-12-17
    • 2016-11-14
    • 1970-01-01
    • 2019-10-20
    • 2021-12-14
    相关资源
    最近更新 更多