【问题标题】:Vue router always loads the lazy loaded modules on intial loadingVue 路由器总是在初始加载时加载延迟加载的模块
【发布时间】:2019-01-22 13:34:10
【问题描述】:

这里是使用Vue官方路由器的延迟加载实现

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");

路由按预期工作,但是延迟加载无法正常工作,当我在第一次加载时检查网络选项卡时,我可以看到 web 包生成的延迟加载文件

【问题讨论】:

    标签: javascript vuejs2 lazy-loading vue-router


    【解决方案1】:

    问题是 webpack preloadplugin 为所有异步块添加了一个 prefetch 标签。为了防止这种情况,请将以下内容添加到您的 vue.config.js

      chainWebpack: config => {
        config.plugins.delete('prefetch');
      }
    

    来源: https://github.com/vuejs/vue-cli/issues/979#issuecomment-373027130

    【讨论】:

      【解决方案2】:

      我相信你做的有点不对..

      如果您想启用块拆分,然后延迟加载路由的组件,您的方法应该看起来像这样:

      src/router/index.js

      import Vue from "vue";
      import VueRouter from "vue-router";
      
      Vue.use(VueRouter);
      
      export default new VueRouter({
        mode: "history",
        routes: [
          {
            path: "/test",
            name: "test",
            component: () => import(/* webpackChunkName: "bar" */ '@/components/Test.vue')
          },
          {
            path: "/test2",
            name: "test2",
            component: () => import(/* webpackChunkName: "foo" */ '@/components/Test2.vue')
          }
        ]
      });
      

      这将创建名为 'bar''foo' 的单独块,仅在输入的路由上才会延迟加载。

      【讨论】:

      • 我已经尝试了解决方案,但仍然是相同的完全重启工作:)
      • 第一次工作,但缓存清除加载后又来了
      • 投了反对票,因为这个解决方案似乎等同于问题中的版本,只是这里明确命名了块。
      猜你喜欢
      • 1970-01-01
      • 2017-08-21
      • 2018-04-20
      • 2015-03-28
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多