vue-痴汉的路引-路由

vue-痴汉的路引-路由

**前言:本文仅用供学习之后理清知识点查看,详细知识点建议访问官方网站:**https://router.vuejs.org/

1.SPA概念

单页面应用(SinglePage Web Application,SPA)多页面应用(MultiPageApplication,MPA)
组成一个外壳和多个页面片段组成多个完整页面构成
资源共用(css,js)共用,只需在外壳部分加载不共用,每个页面都需要加载
刷新方式页面局部刷新或更改整页刷新
url模式a.com/#/pageone
a.com/#/pagetwo
a.com/pageone.html
a.com/pagetwo.html
用户体验页面片段间的切换快,用户体验良好页面切换加载缓慢,流畅度不够,用户体验比较差
转场动画容易实现无法实现
数据传递容易依赖url传参、或者cookie、localStorage等
搜索引擎优化(SEO)需要单独方案、实现较为困难、不利于SEO检索、可利用服务器端渲染(SSR)优化实现方法简易
试用范围高要求的体验度、追求界面流畅的应用试用于追求高度支持搜索引擎的应用
开发成本较高,常需借助专业的框架较低,但页面重复代码多
维护成本相对容易相对复杂

vue-router

  • 开始

    const Foo = { tempalte: '<div>foo</div>'}
    const Bar = { template: '<div>bar</div>'}
    
    const routes = [
    	{path: '/foo', component: Foo },
    	{path: '/bar', component: Bar }
    ]
    const router = new VueRouter({
    	routes //(缩写)相当于 routes:routes
    })
    
    // APP.vue 留有<route-view>为页面插入位置
    
  • 动态路由匹配

  • 嵌套路由

  • 编程式导航(js跳转)vs 声明式

    • 路径

      //点击事件
      this.$router.push(`/detail/${id}`)
      
      // index.js
      {
        path: '/detail/:myid',
        component: Detail,
      },
      
      // 跳转页面通过 this.$route.params.myid 获取id
      
    • 路由名字

      // 点击事件
      this.$router.push({
         name: 'huang',
         params: {
            myid: id
         }
      })
      
      // index.js
      {
        path: '/detail/:myid',
        component: Detail,
        name: 'huang'
      },
      
      // 跳转页面通过 this.$route.params.myid 获取id
      
    • query方式跳转详情

      // 点击事件
      this.$router.push(`/detail?id=${id}`)
      
      // index.js
      {
        path: '/detail',
        component: Detail
       },
      
      // 跳转页面通过 this.$route.query.id 获取id
      
  • 命名路由($route.name获取命名路由的名字)

  • 重定向和别名

    const router = new VueRouter({
    	routes:[
    		{path:'/a',redirect:'/b'}
    	]
    })
    
    const router = new VueRouter({
    	routes:[
    		{path:'/a',comppnent: A, alias: '/b'}
    	]
    })
    
  • HTML5 History模式

    vue支持两种模式

    • hash #/home
    • history /home
  • 路由守卫&路由拦截

    • 全局拦截

      router.beforeEach((to, from, next) => {
        const auth = ['/center', '/order', '/money', '/card']
      
        if (auth.includes(to.fullPath)) {
          if (!localStorage.getItem('token')) {
            next('/login')
          } else {
            next()
          }
        } else {
          next()
        }
      })
      
    • 单个拦截

      // 在需要验证的页面里
      beforeRouteEnter (to, from, next) {
          if (!localStorage.getItem('token')) {
            next('/login')
          } else {
            next()
          }
        }
      
  • 路由懒加载

    const Foo = () => import('./Foo.vue')
    

    在路由配置中什么都不需要改变,只需要像往常一样使用Foo:

    const router = new VueRouter({
    	routes: [
    		{ path: '/foo', component: Foo }
    	]
    })
    
    

反向代理

官方文档:https://cli.vuejs.org/zh/config/#devserver

在vue.config.js中配置

module.exports = {
    devServer: {
        proxy: {
            "/ajax":{
                target: "https://m.maoyan.com",
                changeOrigin: true
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值