我有这样的代码:
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>并发出警告:
未知自定义元素:< schedule-week >-您正确注册了组件吗?对于递归组件,请确保提供"name“选项。
如何解决这个问题?如何正确注册组件?
发布于 2019-09-28 17:13:59
在使用类型记录时,有多种方法来声明vue-组件。基于类的证监会方法(您正在使用的方法)需要遵循稍微不同的语法。您在计划周组件中使用了两次类型记录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: 'ScheduleTable',
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>##EDIT:
来自当然的TypeScript文档:
随着TypeScript和ES6中类的引入,现在存在某些需要额外特性来支持注释或修改类和类成员的场景。修饰器提供了一种为类声明和成员添加注释和元编程语法的方法。
装饰器基本上是TypeScript(JavaScript)函数,用于向以下class (您的组件)或类成员添加附加信息。这也意味着装潢师不能独处。@Component()装饰器将一个Object作为参数,该参数是“as - is”转换成组件-选项。
发布于 2020-08-13 08:36:36
TL;DR
若要在SubOne和SubTwo中注册组件,请将其传递给@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 {
…
}@MarcRo回答中的错误
设置及产生的错误
在组件ScheduleTable.vue中,装饰器@Component将参数name设置为要导入的组件。这将导致对所述组件的递归调用,从而导致错误。
修复
设置name: 'ScheduleTable' --甚至放弃它。类名似乎被使用了(这就是我们正在做的)。
撇开
我找不到一个文档,说明可以传递给装饰师的内容,但是在App.vue和这里中@MarcRo的答案中都是这样使用的。(评论中的链接似乎是旧的)。
我没有很好的声誉来评论和一个固定的编辑被拒绝,所以很抱歉额外的帖子。
谢谢你@MarcRo教我如何使用这个和解决递归错误后的面部表情时间太长;)
https://stackoverflow.com/questions/58148497
复制相似问题