英文:
Vue3 Composables in NPM Package
问题
我正在尝试创建一个NPM包来共享一些Vue3组件。我有以下内容来注册组件本身。
import components from "./components/components";
const plugin = {
install(Vue) {
for (const prop in components) {
if (components.hasOwnProperty(prop)) {
const component = components[prop];
Vue.component(component.name, component);
}
}
}
}
但我还有一些函数(可组合函数),我也想要提供。这应该如何正确实现?是否有类似于组件安装的设置吗?
谢谢。
英文:
I am attempting to make an NPM Package to share some Vue3 Components. I have the following to register the components themselves.
import components from "./components/components";
const plugin = {
install (Vue) {
for (const prop in components) {
if (components.hasOwnProperty(prop)) {
const component = components[prop]
Vue.component(component.name, component)
}
}
}
}
But I also have some functions (composables) that I would like to make available as well. What is the correct way to do this? Is there a similar setup to the component install?
Thanks
答案1
得分: 1
Composable只是JavaScript函数。您可以从文件中导出它们,并通过package.json
中的exports
键使文件可用。
以下是来自VueUse的示例
"exports": {
".": {
"types": "./index.d.ts",
"require": "./index.cjs",
"import": "./index.mjs"
},
"./*": "./*",
"./metadata": {
"types": "./metadata.d.ts",
"require": "./metadata.cjs",
"import": "./metadata.mjs"
}
},
英文:
Composable are just JavaScript functions. You can export them from a file and make the file available through exports
key in package.json
.
Here an example taken from from VueUse
"exports": {
".": {
"types": "./index.d.ts",
"require": "./index.cjs",
"import": "./index.mjs"
},
"./*": "./*",
"./metadata": {
"types": "./metadata.d.ts",
"require": "./metadata.cjs",
"import": "./metadata.mjs"
}
},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论