zoukankan      html  css  js  c++  java
  • vue路由 routers的写法:require用与不用

    vue路由的写法有很多种,这里我只说routers的写法,一种是compcomponent后面直接写路径,另一种是用require的方式,来看代码

    import Vue from 'vue'
    import Router from 'vue-router'
    //首页==================
    //import index from '@/components/index'
    //产品中心================
    import productCenter from '@/components/productCenter/productCenter.vue'
    //商务合作==================
    import teamwork from '@/components/teamwork/teamwork.vue'
    //咨询中心=================
    import askCenter from '@/components/askCenter/askCenter.vue'
    //关于中棋==================
    import aboutUs from '@/components/aboutUs/aboutUs.vue'
    
    
    Vue.use(Router)
    
    export default new Router({
      routes: [
      //首页
        {
          path: '/',
          name: 'index',
          component:resolve =>require(['index'],resolve)
        }
        //产品中心
        ,{
          path: '/productCenter',
          name: 'productCenter',
          component: productCenter
        }
        //商务合作
        ,{
          path: '/teamwork',
          name: 'teamwork',
          component: teamwork
        }
        //咨询中心
        ,{
          path: '/askCenter',
          name: 'askCenter',
          component: askCenter
        }
        //关于我们
        ,{
          path: '/aboutUs',
          name: 'aboutUs',
          component: aboutUs
        }
      ]
    })
    

    首先先说明一点,import引入时用到的“@”就相当于“..”;

    第二点,重点来了,我们看到index页面是用的require的方式写的路由,所以上面的import就注释掉了,这种写法的好处,不仅仅是简单,还有这样写是按需加载,访问此路由时才加载这个js,会避免进入首页时加载内容过多,因为import引入,当项目打包时路由里的所有component都会打包在一个js中,而用require会将你的component分别打包成不同的js。

  • 相关阅读:
    struts2重点——ValueStack和OGNL
    struts2基础——请求与响应、获取web资源
    struts2基础——最简单的一个例子
    servlet、filter、listener、interceptor之间的区别和联系
    服务器端组件
    自定义JSTL标签和函数库
    常见前端效果实现
    HTTP Cookie/Session
    获取动态SQL查询语句返回值(sp_executesql)
    WPF数据绑定
  • 原文地址:https://www.cnblogs.com/eyed/p/9606277.html
Copyright © 2011-2022 走看看