异步加载一组模块
在vue.js(2.x)中,我可以异步地进行组件加载 - 这意味着该组件的代码将存储在单独的捆绑包中 - 通过将相应路由的定义从
// routes.js
import Messages from '@/views/messages'
export default [
{
path: '/messages',
name: 'messages',
component: Messages
}
]
to
// routes.js
export default [
{
path: '/messages',
name: 'messages',
component: () => import('@/views/messages')
}
]
中更改为我可以捆绑2(或更多)路由组件在一起,使它们的代码被同时存储在同一捆绑包中并同时加载(异步)?
我要这样做的原因是因为该组中的组件只能访问具有一定角色的用户,因此用户都可以访问所有角色,或者它们都无法访问。
In Vue.js (2.X) I can make a component load asynchronously - which means the code for that component will be stored in a separate bundle - by changing the corresponding route's definition from
// routes.js
import Messages from '@/views/messages'
export default [
{
path: '/messages',
name: 'messages',
component: Messages
}
]
to
// routes.js
export default [
{
path: '/messages',
name: 'messages',
component: () => import('@/views/messages')
}
]
Is there a way that I can bundle 2 (or more) route components together, such that the code for them is stored in the same bundle and loaded (asynchronously) simultaneously?
The reason I want to do this is because the components in this group are only accessible to a user with a certain role, so either all of them are accessible to a user, or none of them are.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文档在这里 。
@vue/cli
如果您使用
@vue/cli
开发,则使用webpack在引擎盖下,您需要告诉WebPack如何将其块分组,使用import()
import()< /code>:
使用
vite
,您正在开发中的vite,您使用的是在引擎盖下的汇总,并且需要告诉crolup如何分组块,以
vite.config.js
:Documentation here.
@vue/cli
If you develop using
@vue/cli
, you're using webpack under the hood and you need to tell webpack how to group its chunks, using comments insideimport()
:vite
In you develop using
vite
, you're using rollup under the hood and you need to tell rollup how to group chunks, invite.config.js
: