简体   繁体   English

Laravel 应用程序中的延迟加载 vue 组件 - 未加载

[英]Lazy load vue components in a Laravel application - Not loading

TLDR TLDR

How do you properly lazy load Vue components within a Laravel application, using Vue routing in addition to Laravel's own web routes?除了 Laravel 自己的 web 路由之外,如何使用 Vue 路由在 Laravel 应用程序中正确延迟加载 Vue 组件?

Full story:全文:

I have a Laravel application that is using Vue.我有一个使用 Vue 的 Laravel 应用程序。 Now that the application has grown considerably in size, I would like to lazy load the Vue components.现在应用程序的大小已经大大增加,我想延迟加载 Vue 组件。

To do this, I have set up these files:为此,我设置了这些文件:

  • app.js应用程序.js
  • router.js路由器.js
  • Test.vue测试.vue
  • app.blade.php app.blade.php
  • home.blade.php home.blade.php
  • news.blade.php news.blade.php

If I import like: import Test from './components/Test';如果我像这样导入: import Test from './components/Test'; , then everything works fine, but the component isn't lazy loaded. ,然后一切正常,但组件不是延迟加载的。

If I import like如果我像导入

const Test = resolve => {
    require.ensure(['./components/Test'], () => {
        resolve(require('./components/Test'));
    });
}

then, the component is lazy loaded.然后,组件被延迟加载。 I can tell this because I am logging to the console.我可以告诉这一点,因为我正在登录控制台。 However, this seems to break all other JS and the CSS.但是,这似乎破坏了所有其他 JS 和 CSS。

I can also see the new JS file created in the Network tab.我还可以看到在 Network 选项卡中创建的新 JS 文件。 It creates a 0.js file.它创建一个 0.js 文件。

Based on this other question , I have also tried:基于this other question ,我也尝试过:

function loadView(view) {
    return () => import(/* webpackChunkName: "[request]" */ `./components/${view}.vue`)
}

Everything works fine here as well, but the component isn't lazy loaded.这里一切正常,但组件不是延迟加载的。

app.js应用程序.js

import Vue from 'vue';
import router from './router.js';

new Vue({
    el: '#app',
    router
})

router.js路由器.js

import Vue from 'vue';
import Router from 'vue-router';
// import Test from './components/Test'; // Everything works, but doesn't lazy load

// Lazy loads and logs to the console, but breaks all of the other JS and the CSS
const Test = resolve => {
    require.ensure(['./components/Test'], () => {
        resolve(require('./components/Test'));
    });
}

// Also tried this way, which works, but does not lazy load
// function loadView(view) {
//     return () => import(/* webpackChunkName: "[request]" */ `./components/${view}.vue`)
// }

Vue.use(Router);

export default new Router({
    routes: [
        {
            path: '/',
            components: {
                test: Test
                // test: loadView('Test') // 
            }
        },
        {
            path: '/news',
            components: {}
        }
    ],
    mode: 'history',
});

Test.vue测试.vue

<template>
    <div>
        <h1>This is a test</h1>
    </div>
</template>

<script>
    export default {
    }
    console.log('hey, this is from test.vue');
</script>

<style lang="scss" scoped>
</style>

app.blade.php app.blade.php

  • Includes <div id="app"></div> within body在正文中包含<div id="app"></div>

home.blade.php home.blade.php

  • Includes <router-view name="test"></router-view>包括<router-view name="test"></router-view>

news.blade.php news.blade.php

  • Also includes <router-view name="test"></router-view> , just to test.还包括<router-view name="test"></router-view> ,只是为了测试。

Update 1:更新1:

Based on this question: Lazy Loading Components not working with Vue Router , I have updated the loadView function, but am still not able to load the CSS .基于这个问题: Lazy Loading Components not working with Vue Router ,我更新了loadView function,但仍然无法加载 CSS

function loadView(view) {
    return () => import(`./components/${view}.vue`)
}

Update 2:更新 2:

Based on this question: vuejs lazy loading components without the router , I tried to import the component and set it to a constant:基于这个问题: vuejs lazy loading components without the router ,我尝试导入组件并将其设置为常量:

const Test = () => import(
  /* webpackChunkName: "./js/Test" */ './components/Test'
)

The lazy loading works perfectly, but the CSS still doesn't load.延迟加载完美运行,但CSS 仍然无法加载。

Update 3:更新 3:

When using import Test from './components/Test';使用import Test from './components/Test'; , I notice that the app.css file is compiled successfully to 560 KiB. ,我注意到 app.css 文件成功编译为 560 KiB。

However, when using const Test = () => import(/* webpackChunkName: "./js/Test" */ './components/Test');但是,当使用const Test = () => import(/* webpackChunkName: "./js/Test" */ './components/Test'); , this same file fails to compile, and instead remains at 0 bytes. ,同一文件无法编译,而是保持在 0 字节。

The problem here was with Webpack and not the Vue router.这里的问题在于 Webpack 而不是 Vue 路由器。 The routing was working fine, but as noted in Update 3, the app.css would compile to 0 bytes.路由工作正常,但如更新 3 中所述,app.css 将编译为 0 字节。

I noticed that this was a common problem that others had as well.我注意到这是其他人也遇到的常见问题。 The best solution that I found comes from a comment by Daljeet Singh, aka Ilamp, here: https://github.com/JeffreyWay/laravel-mix/issues/1914#issuecomment-628791041我发现的最佳解决方案来自 Daljeet Singh(又名 Ilamp)的评论,这里是: https://github.com/JeffreyWay/laravel-mix/issues/1914#issuecomment-628791041

To save you a click, here is what he did:为了节省您的点击,这是他所做的:

Install laravel-mix-merge-manifest安装 laravel-mix-merge-manifest

npm install laravel-mix-merge-manifest --save-dev

webpack.css.mix.js webpack.css.mix.js

const mix = require('laravel-mix');
require('laravel-mix-merge-manifest');

mix.sass('resources/sass/app.scss', 'public/css')
  .version();

if (!mix.inProduction()) {
  mix.sourceMaps();
}

mix.mergeManifest();

webpack.js.mix.js webpack.js.mix.js

const mix = require('laravel-mix');
require('laravel-mix-merge-manifest');

mix.react('resources/js/app.js', 'public/js')
  .extract([])
  .setPublicPath('public')
  .version();

if (!mix.inProduction()) {
  mix.sourceMaps();
}

mix.mergeManifest();

Update your package.json's scripts section to:将 package.json 的脚本部分更新为:

    "dev": "npm run development-js && npm run development-css",
    "development-js": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js --env.mixfile=webpack.js.mix",
    "development-css": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js --env.mixfile=webpack.css.mix",
    "prod": "npm run production-js && npm run production-css",
    "production-js": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js --env.mixfile=webpack.js.mix",
    "production-css": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js --env.mixfile=webpack.css.mix",
    "watch": "npm run development-js -- --watch & npm run development-css -- --watch",
    "watch-poll": "npm run watch -- --watch-poll",

Happy coding快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM