我有这个代码:
App.vue
<template>
<v-container>
<div>
<schedule-table
v-if = "schedule.length > 0"
:exercises="schedule[0].exercises"
/>
</div>
</v-container>
</template>
<script lang="ts">
import Vue from "vue";
import { Component } from "vue-property-decorator";
import ScheduleTable from '@/components/ScheduleTable.vue'
@Component({
components: {
ScheduleTable,
}
})
</script>
ScheduleTable.vue
<template>
<v-container>
<schedule-week
:exercises="exercises"
/>
</v-container>
</template>
<script lang="ts">
import Vue from "vue";
import { Component, Prop } from "vue-property-decorator";
import { IExercise } from "@/interfaces"
import ScheduleWeek from '@/components/ScheduleWeek.vue'
@Component({
name: 'ScheduleWeek',
components: {
ScheduleWeek,
}
})
@Component
export default class ScheduleTable extends Vue {
@Prop( {required: true, type: Array } ) readonly exercises!: IExercise;
}
</script>
ScheduleWeek.vue
<template>
<v-container
:ex="exercises"
>
here is tables (but this tables dosn't show and any other text dosn't show)
</v-container>
</template>
<script lang="ts">
import Vue from "vue";
import { Component, Prop } from "vue-property-decorator";
import { IExercise } from "@/interfaces"
@Component
export default class ScheduleWeek extends Vue {
@Prop( {required: true, type: Array } ) readonly exercises!: IExercise;
}
</script>
并发出警告:
未知的自定义元素:
-您是否注册了 组件正确吗?对于递归组件,请确保提供 “名称”选项。
如何解决此问题?如何正确注册组件?
答案 0 :(得分:2)
使用打字稿时,有多种方法可以声明vue组件。基于类的SFC方法(您正在使用的一种)需要遵循略有不同的语法。您在计划周组件中两次使用了打字稿decorator
App.vue
<v-container>
<div>
<schedule-table
v-if = "schedule.length > 0"
:exercises="schedule[0].exercises"
/>
</div>
</v-container>
</template>
<script lang="ts">
import { Component, Vue } from "vue-property-decorator"; // you can import vue here
import ScheduleTable from '@/components/ScheduleTable.vue'
@Component({
components: {
ScheduleTable,
}
})
export default class App extends Vue {} // the @Component() decorator needs to be followed by the exported class
相应地,您的其他组件:
ScheduleTable.vue
<template>
<v-container>
<schedule-week
:exercises="exercises"
/>
</v-container>
</template>
<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator"; // see above
import { IExercise } from "@/interfaces"
import ScheduleWeek from '@/components/ScheduleWeek.vue'
@Component({
name: 'ScheduleWeek',
components: {
ScheduleWeek,
}
}) // >>>don't use the decorator twice<<<
export default class ScheduleTable extends Vue {
@Prop( {required: true, type: Array } ) readonly exercises!: IExercise;
}
</script>
ScheduleWeek.vue
<template>
<v-container
:ex="exercises"
>
here is tables (but this tables dosn't show and any other text dosn't show)
</v-container>
</template>
<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator"; // see above
import { IExercise } from "@/interfaces"
@Component
export default class ScheduleWeek extends Vue {
@Prop( {required: true, type: Array } ) readonly exercises!: IExercise;
}
</script>
摘自官方TypeScript文档:
随着TypeScript和ES6中类的引入,现在存在某些情况,这些情况需要附加功能来支持注释或修改类和类成员。装饰器提供了一种为类声明和成员添加注释和元编程语法的方法。
装饰器基本上是TypeScript(JavaScript)函数,用于向以下class
(您的组件)或类成员添加附加信息。这也意味着装饰者不能独自站立。 @Component()
装饰器将Object
作为参数,按原样翻译为组件选项。
答案 1 :(得分:0)
要在SubOne
中注册组件SubTwo
和MyComponent
,请将其传递给@Component
装饰器:
import { Component, Vue } from "vue-property-decorator";
@Component({
components: {
SubOne,
SubTwo,
}
})
export default class MyComponent extends Vue {
…
}
并确保您还像这样装饰SubComponents;
import { Component, Vue } from "vue-property-decorator";
@Component
export default class SubOne extends Vue {
…
}
在组件ScheduleTable.vue
中,装饰器@Component
将参数name
设置为将要导入的组件。这将导致对该组件的递归调用,从而导致错误。
设置name: 'ScheduleTable'
-甚至删除它。似乎使用了类名(这就是我们正在做的事情)。
我找不到有关可以实际传递给装饰器的文档,但是在App.vue
和here中@MarcRo的答案中使用了该文档。 (评论中的链接似乎很旧)。
我没有足够的声誉来发表评论,并且固定的编辑被拒绝,所以对多余的帖子感到抱歉。
感谢@MarcRo教我如何解决此错误以及经过很长一段时间后解决了递归错误;)