赞
踩
(1)vue异步组件-路由懒加载
component:resolve=>(['需要加载的路由的地址',resolve])
(2)import 实现路由懒加载 [ 推荐 ]
const HelloWorld = ()=>import('需要加载的模块地址')
作用:
首屏组件加载速度更快一些,解决白屏问题;更好的客户体验;
也是性能优化的一种方式;
什么是路由懒加载?
懒加载简单来说就是延迟加载或按需加载,就是用到的时候再进行加载。
语法:component:resolve=>(require(['需要加载的路由的地址']),resolve)
- import Vue from 'vue'
- import Router from 'vue-router'
- /* 此处省去之前导入的HelloWorld模块 */
- Vue.use(Router)
-
- export default new Router({
- routes: [
- {
- path: '/',
- name: 'HelloWorld',
- component: resolve=>(require(["@/components/HelloWorld"],resolve))
- }
- ]
- })
语法:const HelloWorld = ()=>import('需要加载的模块地址')
- import Vue from 'vue'
- import Router from 'vue-router'
-
- Vue.use(Router)
-
- export default new Router({
- routes: [
- {
- path: '/',
- name: 'HelloWorld',
- component: ()=>import("@/components/HelloWorld")
- }
- ]
- })
- {
- path: '/home',
- name: 'Home',
- component: r => require.ensure([],() => r(require('@/components/HelloWorld')), 'home')
- }
- <template>
- <div class="hello">
- <One-com></One-com>
- </div>
- </template>
-
- <script>
- export default {
- components:{
- "One-com":resolve=>(['./one'],resolve)
- },
- data () {
- return {
- msg: 'Welcome to Your Vue.js App'
- }
- }
- }
- </script>
- <template>
- <div class="hello">
- <One-com></One-com>
- </div>
- </template>
-
- <script>
- export default {
- components:{
- "One-com": ()=>import("./one");
- },
- data () {
- return {
- msg: 'Welcome to Your Vue.js App'
- }
- }
- }
- </script>
[1] 参考文章
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。