3 ways to achieve on-demand loading vue: vue asynchronous component, es proposal import (), webpack the require.ensure ()

1. vue asynchronous ASSEMBLY

However, in this case a component to generate a js file.
For example as follows:

        {
            path: '/promisedemo',
            name: 'PromiseDemo',
            component: resolve => require(['../components/PromiseDemo'], resolve) }

2. es proposals import ()

vue official document: Route lazy loading (using the import ())

  • vue-router configured to route code is as follows:
// 下面2行代码,没有指定webpackChunkName,每个组件打包成一个js文件。
const ImportFuncDemo1 = () => import('../components/ImportFuncDemo1') const ImportFuncDemo2 = () => import('../components/ImportFuncDemo2') // 下面2行代码,指定了相同的webpackChunkName,会合并打包成一个js文件。 // const ImportFuncDemo = () => import(/* webpackChunkName: 'ImportFuncDemo' */ '../components/ImportFuncDemo') // const ImportFuncDemo2 = () => import(/* webpackChunkName: 'ImportFuncDemo' */ '../components/ImportFuncDemo2') export default new Router({ routes: [ { path: '/importfuncdemo1', name: 'ImportFuncDemo1', component: ImportFuncDemo1 }, { path: '/importfuncdemo2', name: 'ImportFuncDemo2', component: ImportFuncDemo2 } ] })

3. webpack provided require.ensure ()

  • vue-router configured to route the use webpack require.ensure technique, loading may also be implemented as needed.

In this case, a plurality of identical specified routing chunkName, will be merged into a packaged file js.
For example as follows:

 
        {
            path: '/promisedemo',
            name: 'PromiseDemo',
            component: resolve => require.ensure([], () => resolve(require('../components/PromiseDemo')), 'demo') }, { path: '/hello', name: 'Hello', // component: Hello component: resolve => require.ensure([], () => resolve(require('../components/Hello')), 'demo') }

Guess you like

Origin www.cnblogs.com/zlq92/p/11200353.html