VUE3+vite项目中动态引入组件与异步组件的详细实例
一、全量注册,随用随取
1. 把项目中所有vue文件注册成异步组件。
const app = createApp(App);
function registerGlobalAsyncComponents(app: VueApp) {
const modules = import.meta.glob('.*.vue');
for (const path in modules) {
const result = path.match(/.*\/(.+).vue$/);
if (result) {
const name = result[1];
const component = modules[path];
app.component(name, defineAsyncComponent(component));
}
}
}
2. 获取组件
在setup函数获取组件
const internalInstance = getCurrentInstance();
// 搂一眼,看看注册的组件名字是啥
console.log(internalInstance.appContext.components);
// 获取组件
internalInstance.appContext.components['你组件的名字']
3. 参考如下
Glob 导入
Vite 支持使用特殊的 import.meta.glob 函数从文件系统导入多个模块:
const modules = import.meta.glob('./dirbar.js'])
// vite 生成的代码
const modules = {
'./dir/foo.js': () => import('./dir/foo.js')
}
具名导入
也可能你只想要导入模块中的部分内容,那么可以利用 import 选项。
const modules = import.meta.glob('./dir*.js'
`./module-${name}.js` -> './module-*.js'
`./modules-${name}/index.js` -> './modules-*/index.js'
'./locales/' + locale + '.js' -> './locales*.js'
Code that looks like this:
function importLocale(locale) {
return import(`./locales/${locale}.js`);
}
Is turned into:
function __variableDynamicImportRuntime__(path) {
switch (path) {
case './locales/en-GB.js':
return import('./locales/en-GB.js');
case './locales/en-US.js':
return import('./locales/en-US.js');
case './locales/nl-NL.js':
return import('./locales/nl-NL.js');
default:
return new Promise(function (resolve, reject) {
queueMicrotask(reject.bind(null, new Error('Unknown variable dynamic import: ' + path)));
});
}
}
function importLocale(locale) {
return __variableDynamicImportRuntime__(`./locales/${locale}.js`);
}
Limitations
To know what to inject in the rollup bundle, we have to be able to do some static analysis on the code and make some assumptions about the possible imports. For example, if you use just a variable you could in theory import anything from your entire file system.
function importModule(path) {
// who knows what will be imported here?
return import(path);
}
To help static analysis, and to avoid possible foot guns, we are limited to a couple of rules:
Imports must start with ./ or …/.
All imports must start relative to the importing file. The import should not start with a variable, an absolute path or a bare import:
// Not allowed
import(bar);
import(`${bar}.js`);
import(`/foo/${bar}.js`);
import(`some-library/${bar}.js`);
Imports must end with a file extension
A folder may contain files you don’t intend to import. We, therefore, require imports to end with a file extension in the static parts of the import.
// Not allowed
import(`./foo/${bar}`);
// allowed
import(`./foo/${bar}.js`);
Imports to your own directory must specify a filename pattern
If you import your own directory you likely end up with files you did not intend to import, including your own module. It is therefore required to give a more specific filename pattern:
// not allowed
import(`./${foo}.js`);
// allowed
import(`./module-${foo}.js`);
Globs only go one level deep
When generating globs, each variable in the string is converted to a glob * with a maximum of one star per directory depth. This avoids unintentionally adding files from many directories to your import.
In the example below this generates ./foo//.js and not ./foo*.js.
import(`./foo/${x}${y}/${z}.js`);
总结
到此这篇关于VUE3+vite项目中动态引入组件与异步组件的文章就介绍到这了,更多相关VUE3+vite动态引入组件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341