vue.js - 延迟加载组件不适用于 Vue Router

标签 vue.js vuejs2 vue-router

当尝试在我的 routes.js 文件中延迟加载组件时,我的 CSS 没有被编译。

在文件顶部使用 import 语句时,一切都按预期工作。没有控制台错误或编译错误。我正在使用 Vue.js、Vue-Router、Laravel 和 Laravel-Mix(所有最新版本)

//Working (CSS is present)
import Home from '../../components/admin/Home';

//Not Working (No CSS is present)
function loadView(view) {
    return () => import(/* webpackChunkName: "view-[request]" */ `../../components/admin/${view}.vue`)
}

//How I'm initializing the component
let routes = [
    { 
        path: '/', 
        name: 'home',
        component: loadView('Home'),
        meta: { 
            requiresAuth: true
        }
    }]

//Laravel Mix Config
const mix = require('laravel-mix');
const webpack = require('webpack');

mix.js('resources/js/apps/registration/app.js', 'public/js/apps/registration/app.js')
   .js('resources/js/apps/admin/app.js', 'public/js/apps/admin/app.js')
   .js('resources/js/material-dashboard.js', 'public/js/material-dashboard.js')
   .js('resources/js/material-dashboard-extras.js', 'public/js/material-dashboard-extras.js')
   .sass('resources/sass/app.scss', 'public/css');

 mix.webpackConfig({
   plugins: [
      /**
       * Bug with moment.js library causing ./locale not to be found
       * https://github.com/moment/moment/issues/2979
       * Created an empty module running npm install --save empty-module and then doing the below
       */
      new webpack.ContextReplacementPlugin(/\.\/locale$/, 'empty-module', false, /js$/),
      /**
       * Global objects are not getting recognized via require
       * Set them up here
       * global_name: path or module
       */
      new webpack.ProvidePlugin({
         /**
          * There was an error with Popper not being defined due to Popper being included in Bootstrap
          * https://github.com/FezVrasta/bootstrap-material-design/issues/1296
          */
         'Popper': 'popper.js/dist/umd/popper'
     })
  ]
 });

附件是我所说的未应用 CSS 的截图

How page looks with import

How page looks when trying to lazy load components in routes.js

最佳答案

您不应该导入 webpack 块名称,而应该像这样引用组件的适当路径:

//Not Working (No CSS is present)
function loadView(view) {
    return () => import(  `../../components/admin/${view}.vue`)
}
注意:未测试,希望有帮助:)

关于vue.js - 延迟加载组件不适用于 Vue Router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56103608/

相关文章:

javascript - Vue - 多个 radio 输入,检查值?

javascript - NUXT如何将数据从页面传递到布局

javascript - TypeScript 错误导入 vue 包 (vue-cli3 proj)

javascript - Vue2.js - 在对象内部调用方法并回调(本期)

javascript - 使用导入的 Javascript 函数 ES6 访问主文件中的全局变量

vue.js - 如何清除 vuex store 中的状态?

javascript - Vue.js 2 - vue-youtube-embed 如何检测视频结束状态?

javascript - 单击 vue-router 项目时更改组件并更新数据(ajax)

javascript - vue-router匹配查询

javascript - Vue.js 路由器查询数组