Vue-router基本学习(1)

多页面应用:网页HTML文件是请求后台发过来的。每次切换页面都会从后台把页面文件传输回来。
单页面应用:网页只有在第一次进入页面的、的时候请求服务器的HTML文件,接下来的页面跳转是基于内部的router
两种应用的优缺点:

  1. 多页面应用只需要加载当前页面所需要的资源,所以首屏时间快。但是每切换一次页面都要去请求一次服务器资源。单页面应用第一次要将所有的资源全部加载,所以首屏时间慢,但是后续的跳转不需要再次向服务器发请求。
  2. 多页面应用可以直接实现SEO搜索,但是单页面得有一套单独的SEO方案。
  3. 单页面可以实现局部刷新,多页面实现不了。

这里稍微的讲了一些单页面和多页面的一些知识,大家要知道 Vue 是一个单页面应用,其页面的跳转需要通过路由:Vue-router!!! vue-router的安装我们已经在前面的文章里讲过了,今天这篇文章就讲vue-router的使用。

基本使用

src/router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import Parent from '@/components/Parent'
import Childs1 from '@/components/Childs1'
import Childs2 from '@/components/Childs2'
Vue.use(Router)

export default new Router({
  mode:'history',
  routes: [
 {
      path:'/parent',
      name:'Parent',
      component:Parent
    },
    {
      path:'/child1',
      name:'Childs1',
      component: Childs1
    },
   {
     path: '/child2',
     name:'Childs2',
    component:Childs2
   }
  ]
})

运行结果如下图:
basicUse

我们输入不同的路由不同的组件被渲染出。首先我们将需要在路由里面渲染的组件引入,然后配置路由。path:是我们需要配置的路径名,component: 是我们需要在该路径下渲染的组件。

路由嵌套

我们在开发的过程中不应该只有一级路由。比如上面的例子,child应该放在`parent的下面,name我们将怎么样实现路由的嵌套呢?
当然是用路由嵌套啦~

src/router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import Parent from '@/components/Parent'
import Childs1 from '@/components/Childs1'
import Childs2 from '@/components/Childs2'
Vue.use(Router)

export default new Router({
  mode:'history',
  routes: [
   {
      path:'/parent',
      name:'Parent',
      component:Parent,
      children: [
        {path:'child1', component: Childs1},
        {path:'child2', component: Childs2}
      ]
    }
  ]
})

Parent.vue

<template>
<div>
    Parent
    <router-view> </router-view>
</div>
</template>

运行结果如下图:
路由嵌套
Parent.vue<router-view> </router-view>是渲染其组路由组件的地方。我们可以看到url为/parent的时候,页面上只有paernt的字符串,当我们路由为两层的时候,parentchild全部展示在页面上。vue-router 会根据不同的路由加载不同的组件。

动态路由

如果我们要将某一种模式下的路由全部映射到同一个组件上,比如我们要将'/user/tom''/user/David' 都匹配到同样组件下面,那么动态路由是我们不二的选择。

src/router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import Parent from '@/components/Parent'
import Childs1 from '@/components/Childs1'
Vue.use(Router)

export default new Router({
  mode:'history',
  routes: [
   {
      path:'/parent',
      name:'Parent',
      component:Parent,
      children: [
        {path: 'child1/:name', component:Childs1}
      ]
    }
  ]
})

Parent.vue

<template>
<div>
    Parent
    <router-view></router-view>
</div>
</template>

Childs1.vue

<template>
    <div>
       Childs1-- -{{$route.params.name}}
   </div>
</template>

运行结果如下图:
动态路由
我们虽然在/child1后面输入不同的路径,但是最后全部映射到同一个组件上。this.$route.params对象存放我们的动态路由的内容。

动态路由引起的组件复用

动态路由就是将不同的路由映射到同一个组件上,如果两个路由是匹配到同一组件,那么Vue不会将当前组件销毁重建,而是直接替换不一样的内容,实现组件的复用。

src/router/index.js
同上

Parent.vue

<template>
<div>
    Parent
    <router-view></router-view>
</div>
</template>

Childs1.vue

<template>
    <div>
       Childs1-- -{{$route.params.name}}
       <button @click="change">点我去aa</button>
   </div>
</template>
<script>
export default {    
    name:'Childs1', 
    data(){
        return{
            title: 1
        }
    },
    methods:{
        change(){
            this.$router.push('/parent/child1/aa' + this.title++);
        }
    },
     mounted(){
        console.log('child1 mounted',new Date().toLocaleString());
    }
}
</script>

运行结果如下图:
动态路由啊
动态路由3
我们使用编程式导航来进行路由切换,title的初始值唯一,在我们点击按钮进行页面切换的时候,title没有变成初始值,而是复用了前一个页面的组件,在原来的基础上自增。第二章图片也显示,只有第一次进入的时候进入了生命周期钩子,以后的页面切换不再进入钩子

编程式导航和声明式导航

编程式导航是调用方法push进行路由跳转,声明式导航是类似于a标签一样的<router-link to='/parent'></router-link>的标签进行跳转。to属性的内容就是我们要跳转的目标路由。声明式导航最终渲染到页面也是a标签。

声明式导航在被点击的时候会调用编程式导航的方法。
Parent.vue*

<template>
  <div>
    <ul>
      <router-link to='/parent/child1/bb'>
        <li>点我去bb</li>
      </router-link>
      <router-link to='/parent/child1/cc'>
        <li>点我去cc</li>
      </router-link>
      <router-link to='/parent/child1/dd'>
        <li>点我去dd</li>
      </router-link>
    </ul>
    <router-view></router-view>
  </div>
</template>

Childs1.vue
同上

运行结果如下图:
图片描述

li的外面包裹着router-link,当我们点击的时候,路由就会跳转到我们to指向的URL,我们点击按钮的时候,调用了'this.$router.push(url)'方法来进行跳转。这两种方法没有好与坏的区别,只是使用于不同的场景。

router.push()

push往history栈中加入一条记录,所以当我们使用浏览器的后退按钮时,还能够回到这一页。

router.replace()

replace是替换栈中当前页的记录,意味着history栈中不会再有当前页的记录。这种方法通常用来授权页,这样就不会有二次授权的情况出现。

router.go()

go是告诉浏览器在history栈中前进或者后退几步,所以我们一般的页面跳转用push才能在栈中新增一条记录,便于go使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值