zoukankan      html  css  js  c++  java
  • vue-router路由懒加载

    require: 运行时调用,理论上可以运用在代码的任何地方,
    import:编译时调用,必须放在文件开头

    懒加载:component: resolve => require(['@/view/index.vue'], resolve)
    用require这种方式引入的时候,会将你的component分别打包成不同的js,加载的时候也是按需加载,只用访问这个路由网址时才会加载这个js

    非懒加载:component: index
    如果用import引入的话,当项目打包时路由里的所有component都会打包在一个js中,造成进入首页时,需要加载的内容过多,时间相对比较长

    vue的路由配置文件(routers.js),一般使用import引入的写法,当项目打包时路由里的所有component都会打包在一个js中,在项目刚进入首页的时候,就会加载所有的组件,所以导致首页加载较慢,

    而用require会将component分别打包成不同的js,按需加载,访问此路由时才会加载这个js,所以就避免进入首页时加载内容过多。

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    
    import Detail from '../pages/goodsDetail'
    import Msg from '../components/message.vue'
    // 使用路由
    Vue.use(VueRouter)
    export default new VueRouter({
      mode: 'history',
      routes: [
        {
          // 进行路由配置,规定'/'引入到home组件
          path: '/',
          component: resolve => require(['../pages/home.vue'], resolve),
          meta: {
            title: 'home'
          }
        },
        {
          path: '/msg',
          component: Msg
        },
        {
          path: '/detail',
          component: Detail,
          children: [
            {
              path: 'msg',
              component: Msg
            }
          ]
        }
      ]
    })
    

      

    ES6 提出的import方法,(------最常用------)

        方法如下:const HelloWorld = ()=>import('需要加载的模块地址')

        (不加 { } ,表示直接return)



    import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) const HelloWorld = ()=>import("@/components/HelloWorld") export default new Router({ routes: [ { path: '/', name: 'HelloWorld', component:HelloWorld } ] })

    如果用import引入的话,当项目打包时路由里的所有component都会打包在一个js中,造成进入首页时,需要加载的内容过多,时间相对比较长。
    当你用require这种方式引入的时候,会将你的component分别打包成不同的js,加载的时候也是按需加载,只用访问这个路由网址时才会加载这个js。
    你可以打包的时候看看目录结构就明白了。

    转:https://www.cnblogs.com/xiaoxiaoxun/p/11001884.html

  • 相关阅读:
    INode满的处理方法
    分布式转码集群思路
    FreeBSD Set a Default Route / Gateway
    ssh遇到port 22:No route to host问题的解决方法
    debian 开启SSH
    virsh 查看信息
    virsh console配置
    virsh console hangs at the escape character “^]”
    virt-install命令---详解
    kmv 学习笔记 工具
  • 原文地址:https://www.cnblogs.com/ygyy/p/12792174.html
Copyright © 2011-2022 走看看