Here is the lazy loaded implementation using Vue official router
src/router/index.js
import Vue from "vue";
import VueRouter from "vue-router";
const Foo = () => import("@/components/Test2");
const Bar = () => import("@/components/Test");
Vue.use(VueRouter);
export default new VueRouter({
mode: "history",
routes: [
{
path: "/test",
name: "test",
component: Bar
},
{
path: "/test2",
name: "test2",
component: Foo
}
]
});
src/main.js
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
Vue.config.productionTip = false;
new Vue({
render: h => h(App),
router
}).$mount("#app");
Routes work as expected however the lazy loading not working properly, when I inspect the network tab on the first load I can able to see the web pack generated lazily loaded files
vue file. Lazy loading refers to an approach by which all the scripts are not loaded on the DOM as the application starts. Instead, they're only loaded when requested, which makes the JavaScript bundle size very small at initial load. Vue.
One form of lazy loading is infinity scroll, in which, the content of the web page is loaded as and when the user scrolls down the page. It is a popular technique being used by various websites. Advantages of Lazy loading: On-demand loading reduces time consumption and memory usage thereby optimizing content delivery.
The problem is the webpack preloadplugin adds a prefetch tag to all async chunks. To prevent this, add the following to your vue.config.js
chainWebpack: config => {
config.plugins.delete('prefetch');
}
Source: https://github.com/vuejs/vue-cli/issues/979#issuecomment-373027130
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With