![](/img/trans.png)
[英]import components into components with vue3, vite and typescript
[英]is it necessary to import vue in all components? Nuxt and typescript
目前我已将 typescript 集成到 nuxt,如文档中所示: https ://typescript.nuxtjs.org/es/guide/setup/
但我有以下疑问:在组件中,您应该始终import vue from "vue"
并export default Vue.extend ({});
在文档中,我看到他们使用 1 个组件( https://typescript.nuxtjs.org/es/cookbook/components )执行示例,但就我而言,我怀疑是否真的应该将它们添加到所有组件或我可以通过什么方式在全球范围内做到这一点。
我认为我的types/vue-shim.d.ts
文件中的配置为我做了这个,但后来我的概念错了。
vue-shim.d.ts
:
import Vue from "vue";
declare module "*.vue" {
import Vue from "vue";
export default Vue;
}
在将 typescript 添加到 nuxt 之前,我的组件是这样的:
<script>
import comp from "../component.vue";
export default {
components: {
comp
},
data() {
return {
createUser: true,
params: {
rol_id: 0,
post_url: "",
update_url: ""
}
};
},
mounted() {
this.params.post_url = this.$config.routePrefix + "/agency/";
this.params.update_url = this.$config.routePrefix + "/agency/";
if (this.$route.params.uuid !== undefined) {
this.createUser = false;
}
},
methods: {
fetchForms() {}
}
};
</script>
不,并非绝对需要在所有组件中导入vue
。
如文档中所述,仅需要Vue.extend
在您的 TypeScript IDE 中启用类型推断。 例如,它让 TypeScript 根据data()
中的params
声明推断mounted()
中this.params
的类型。
Vue.extend
不添加任何功能,因此无论有无Vue.extend
,您的组件都将运行相同。 但是,要获得使用 TypeScript 的全部好处,您应该在组件中使用Vue.extend
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.