如何使用 @vue/coptions-api在TS文件中创建新组件?

发布于 2025-02-07 22:49:46 字数 660 浏览 4 评论 0原文

我有一些需要在TS文件中创建的组件。通常,我通过调用新组件来创建,因为在我们的项目上,我们有vue-component-decorator,但是现在我开始使用@vue/composition-api,我可以' t将任何组件称为新的。

工作:

// SomeComponent.vue
import { Vue, Component } from 'vue-component-decorator';

@Component
export default SomeComponent extends Vue {}

// Any .ts file
const component = new SomeComponent();

不工作:

// SomeComponent.vue
import { defineComponent } from '@vue/composition-api';

export default defineComponent({});

// Any .ts file
const component = new SomeComponent();

I have some components that I need to create in the TS file. Usually, I create by calling new Component because on our project we have vue-component-decorator but now I'm starting using @vue/composition-api and I can't call any component as new.

Working:

// SomeComponent.vue
import { Vue, Component } from 'vue-component-decorator';

@Component
export default SomeComponent extends Vue {}

// Any .ts file
const component = new SomeComponent();

Not working:

// SomeComponent.vue
import { defineComponent } from '@vue/composition-api';

export default defineComponent({});

// Any .ts file
const component = new SomeComponent();

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

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

发布评论

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

评论(1

只有一腔孤勇 2025-02-14 22:49:46

您需要使用createApp来编程创建组件并安装IT:

const myComponent = defineComponent({/* */})
createApp(myComponent, { /* Pass props here */ }).mount('#container');

来源:

You need to use createApp to create the component and mount it programmatically:

const myComponent = defineComponent({/* */})
createApp(myComponent, { /* Pass props here */ }).mount('#container');

Sources:

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