无法解析同一文件夹中的组件Vue 3

发布于 2025-01-13 12:29:15 字数 1860 浏览 1 评论 0 原文

我正在使用 Vue 3 以及基于类的组件和 Typescript。如果我尝试在另一个组件中使用一个组件,则会收到此错误:

[Vue warn]: Failed to resolve component: Button
If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement. 

Button 组件在其他地方工作没有问题。 这是我尝试使用 Button 并引发错误的组件:

<template>
    <form>
        <input type="email" />
        <Button text="Submit" />
    </form>
</template>

<script>
import { Vue, Options } from "vue-class-component";
import Button from "@/components/Button";

Options({
  components: {
    Button,
  },
});
export default class Registration extends Vue {}
</script>

我在视图 Status 内使用此 Registration 组件(只是另一个组件)。这是我的文件夹结构:

/src
--/components
----Button.vue
----Registration.vue
--/views
----Status.vue

我尝试在另一个组件中复制相同的错误,并且一切正常。看起来问题不在于组件的位置(同一文件夹)。

如果我尝试 console.log 组件 Button,控制台将显示该组件:

Object { props: {…}, components: {…}, extends: {}, methods: {}, computed: {}, setup: setup(props, ctx), render: render(_ctx, _cache, $props, $setup, $data, $options), __scopeId: "data-v-015de462", __file: "src/components/Button.vue", __hmrId: "015de462" }

但在模板中不会呈现。这就是渲染的内容:

<button text="Submit" data-v-5d567869=""></button>

我尝试使用其他组件代替 Button,但我得到了相同的结果。 我尝试使用 ./Button 而不是 @/components/Button 导入组件,但没有成功。

编辑:

正如 @jeremy-castelli 的评论所暗示的那样,我尝试重命名 CustomButton 中的 Button 组件。我还重命名了 CustomButton.vue 中的文件名。在模板中,我使用了 。错误保持不变。

我做错了什么?

I am using Vue 3 with class based components and Typescript. If I try to use a component inside another component I get this error:

[Vue warn]: Failed to resolve component: Button
If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement. 

This Button component works without problems in other places.
This is the component where I'm trying to use the Button and that throws the error:

<template>
    <form>
        <input type="email" />
        <Button text="Submit" />
    </form>
</template>

<script>
import { Vue, Options } from "vue-class-component";
import Button from "@/components/Button";

Options({
  components: {
    Button,
  },
});
export default class Registration extends Vue {}
</script>

I use this Registration component inside the view Status (just another component). This is my folder structure:

/src
--/components
----Button.vue
----Registration.vue
--/views
----Status.vue

I tried to replicate the same error in another component, and everything works. It looks like the problem is not the location (same folder) of the components.

If I try to console.log the component Button, the console will show the component:

Object { props: {…}, components: {…}, extends: {}, methods: {}, computed: {}, setup: setup(props, ctx), render: render(_ctx, _cache, $props, $setup, $data, $options), __scopeId: "data-v-015de462", __file: "src/components/Button.vue", __hmrId: "015de462" }

But in the template is not rendered. This is what is rendered:

<button text="Submit" data-v-5d567869=""></button>

I tried to use also other components instead Button, but I get the same result.
I tried to import the component with ./Button instead of @/components/Button without success.

EDIT:

As the comment of @jeremy-castelli suggests, I tried to rename the Button component in CustomButton. I renamed also the filename in CustomButton.vue. In template then I used both <CustomButton ... and <custom-button .... The error stay the same.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金兰素衣 2025-01-20 12:29:15

很尴尬,但无论如何我都会把解决方案留在这里。让这次羞辱成为我的一个教训。

我的装饰器 Options({}) 缺少 @

它应该是@Options({})

:/

Is embarrassing, but I'll leave the solution here anyway. Let the humiliation serve as a lesson to me.

My decorator Options({}) is missing the @.

It should be @Options({}).

:/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文