【发布时间】: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