简体   繁体   English

vue脚本的懒加载

[英]lazy loading of vue script

Recently I created an admin panel using vue and laravel.最近我使用 vue 和 laravel 创建了一个管理面板。

All things working correctly but one problem is that my compiled script file is almost 7mb file (ie app.js which is located in public folder) because of this compiled script browser take to long loading time to load initial (landing page) on browser ie it does not render page until 7mb script get downloaded in browser which is almost 1 min.一切正常,但一个问题是我编译的脚本文件几乎是 7mb 文件(即位于公共文件夹中的 app.js),因为这个编译的脚本浏览器需要很长时间才能在浏览器上加载初始(登陆页面),即在浏览器中下载 7mb 脚本之前它不会呈现页面,这几乎是 1 分钟。

Is it possible to load initially one component at start after that according to user click load other component.是否可以根据用户单击加载其他组件在启动时加载最初的一个组件。

In more detail: suppose my app.js file has 1K lines of code and at start if few lines of code get downloaded更详细地说:假设我的 app.js 文件有 1K 行代码,并且在开始时如果下载了几行代码

then it renders page ie it should not wait to download complete 7mb script after that it render page.然后它呈现页面,即它不应该等待在它呈现页面之后下载完整的 7mb 脚本。

Is there anyway to reduce this loading time .有没有办法减少这个加载时间。 I did lots of google but haven't find any solution我做了很多谷歌但没有找到任何解决方案

Try to use es6 dynamic import.尝试使用 es6 动态导入。 This entry in vue document and link in vue-router may help you vue 文档中的这个条目vue-router 中的链接可能会对您有所帮助

In addition, 7 mb of pure JavaScript code, on my perspective, is a very complicated spa.此外,在我看来,7 mb 的纯 JavaScript 代码是一个非常复杂的 spa。 Are you sure you minify your code and avoid bundling something like big images?您确定要缩小代码并避免捆绑大图像之类的东西吗?

Now that you use vue-router, I suggest that you use the method described in the vue-router document, as it's based on vue component's async loading and webpack's code splitting feature.既然你使用了 vue-router,我建议你使用 vue-router 文档中描述的方法,因为它基于 vue 组件的异步加载和 webpack 的代码拆分特性。

Remember to import syntax-dynamic-import plugin to avoid the unexpected token error you mentioned above.请记住导入 syntax-dynamic-import 插件以避免您上面提到的意外令牌错误。 在 vue-router 文档中提到

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

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