Vue Router 与懒加载组件无法正常工作

4

尝试在我的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未被应用的截图:

使用import时页面的外观

尝试在routes.js中延迟加载组件时页面的外观


不确定是否能够在编译时动态导入变量组件文件名。您是否尝试使用静态文件名(文件路径中没有变量部分)?() => import(/* webpackChunkName: "view-1" */ "../../components/admin/view1.vue") - ssc-hrep3
1个回答

1

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

//Not Working (No CSS is present)
function loadView(view) {
    return () => import(  `../../components/admin/${view}.vue`)
}

NB: 没有测试过,希望它能帮到你 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接