【问题标题】:Vue typescript recursive componentVue typescript 递归组件
【发布时间】:2020-07-08 12:22:34
【问题描述】:

我有一个简单的带有递归的打字稿组件:

<template>
  <div :style="{ paddingLeft: depth * 20 + 'px' }">
    <h1>Level {{ depth }}</h1>
    <div v-if="depth < 2">
      <Recursive v-for="i in 3" :key="i" :depth="depth + 1"/>
    </div>
  </div>
</template>

<script lang="ts">
import * as Vue from "vue";
import { Component, Prop } from "vue-property-decorator";

@Component({
  components: { Recursive }
})
class Recursive extends Vue {
  @Prop({ required: true })
  depth!: number;
}

export default Recursive;
</script>

演示: https://codesandbox.io/s/vue-typescript-recursive-12u3q?file=/src/components/Recursive.vue

在开发模式下运行良好。

但是一旦我进行构建 - 递归导入将被编译成这些类型的字符串:

&lt;!--function(e,n,r,o){return ln(t,e,n,r,o,!0)}--&gt; 而不是 html。

我应该怎么做才能使递归正常工作?

【问题讨论】:

    标签: typescript vue.js recursion


    【解决方案1】:

    我忘了在@Component装饰器中设置组件的名称:

    @Component({
      name: 'Recursive',
      components: { Recursive }
    })
    

    此代码将在开发和编译模式下工作。

    【讨论】:

    • 我爱你!奇怪的事情,因为你没有得到任何提示什么是错的。而且我仍然不明白为什么它在开发模式下工作......
    • 很高兴它有帮助❤️不知道为什么它仍在开发模式下工作?‍♂️
    • 设置name 也为我解决了这个问题。但是,对我来说,定义 components: { Recursive } 部分会导致错误,所以我只是将其删除。我遇到了一些有用的链接:github.com/nuxt/nuxt.js/issues/6385#issuecomment-748076310medium.com/js-dojo/…(我知道第二个链接是针对非打字稿的,但它可能仍然对上下文有用)
    猜你喜欢
    • 2021-03-10
    • 2018-09-01
    • 2023-04-02
    • 2021-12-27
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 2019-12-10
    • 2019-09-13
    相关资源
    最近更新 更多