您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

VueRouter 编程式导航

本小节我们介绍如何使用 VueRouter 编程式导航。 push、replace、go 三种的使用和区别。其中了解和掌握三种使用方式以及他们之区别是本节的重点。本节的相对容易,同学们只需要在学完本节的后稍加记忆,并通过一两个案例进行调试,相信一定可以对这三种的使用游刃有余。

在之前的小节中,我们的路由是通过 <router-link> 来完成的。但有时候,我们可能需要通过普通的 onClick 事件来完成。router.push 就可以帮我们实现这一点。

让我们来看简单的示例:

<!DOCTYPE html>
<html lang="en">
<head>
  < charset="UTF-8">
  < name="viewport" content="width=device-width, initial-scale=1.0">
  < http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <button @click="jump('index')"></button>
      <button @click="jump('article')"></button>
    </div>
    <router-view></router-view>
  </div>
</body>

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script type="text/javascript">

const Index = Vue.component('index', {
  template: '<div>Hello,欢迎使用网学习 Vue 教程!</div>',
})

const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 计算的学习</li><li>2. Vue 侦听器的学习</li></ul>`,
})


const routes = [
  { path: '/index', component: Index },
  { path: '/article', component: Article }
]

const router = new VueRouter({
  routes: routes
})

  var vm = new Vue({
    el: '#app',
    router,
    data() {
    	return {}
    },
    methods: {
      jump(name) {
        this.$router.push(name)
      }
    }
  })
</script>
</html>

HTML 第 12-13 行,我们定义了两个按钮,并给他们点击事件 jump。
HTML 第 15 行,我们使用 <router-view></router-view> 组件来渲染匹配组件。
JS 第 5-7 行,我们定义了组件 Index。
JS 第 9-11 行,我们定义了组件 Article。
JS 第 13-16 行,我们定义了路由数组:
1. 路由,地址为 ‘/index’,匹配组件 Index。
2. 路由,地址为 ‘/article’,匹配组件 Article。
JS 第 18-20 行,创建 router 实例,然后传 routes 配置。
JS 第 24 行,通过 router 配置参数注入路由。
JS 第 29-31 行,我们定义来 jump ,通过 router.push 实现路由。

在上例子中,我们通过 router.push 的实现了路由,该接收路径作为参数。实际上,router.push 也可以接收描述地址的对象作为参数。例如:


// 字符串形式的参数
router.push('home')

// 通过路径描述地址
router.push({ path: 'home' })

// 通过命名的路由描述地址
router.push({ name: 'user' }})

当以对象形式传递参数的时候,还可以有一些其他,例如参数 params、query。路由传参我们将有专门的小节来学习,同学们只需要有印象即可。

跟 router.push 很像,唯一的不同就是,它不会向 history 新记录,而是跟它的名一样 —— 替换掉当前的 history 记录。我们将上例子中的 jump 稍加:

<!DOCTYPE html>
<html lang="en">
<head>
  < charset="UTF-8">
  < name="viewport" content="width=device-width, initial-scale=1.0">
  < http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <button @click="jump('index')"></button>
      <button @click="jump('article')"></button>
    </div>
    <router-view></router-view>
  </div>
</body>

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script type="text/javascript">

const Index = Vue.component('index', {
  template: '<div>Hello,欢迎使用网学习 Vue 教程!</div>',
})

const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 计算的学习</li><li>2. Vue 侦听器的学习</li></ul>`,
})


const routes = [
  { path: '/index', component: Index },
  { path: '/article', component: Article }
]

const router = new VueRouter({
  routes: routes
})

  var vm = new Vue({
    el: '#app',
    router,
    data() {
    	return {}
    },
    methods: {
      jump(name) {
        this.$router.replace(name)
      }
    }
  })
</script>
</html>

解释:
JS 第 29-31 行,我们定义来 jump ,通过 router.replace 实现路由。

这个的参数是整数,意思是在 history 记录中向前或者后退多少步。例如:


// 在浏览器记录中前进一步
router.go()

// 后退一步记录
router.go(-)

// 前进 3 步记录
router.go()

// 如果 history 记录不够用,路由将不会进行
router.go(-)
router.go()

接下来我们仍然对第案例稍加:

<!DOCTYPE html>
<html lang="en">
<head>
  < charset="UTF-8">
  < name="viewport" content="width=device-width, initial-scale=1.0">
  < http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <router-link to="index"></router-link>
      <router-link to="article"></router-link>
    </div>
    <button @click="go(1)">前进一步</button>
    <button @click="go(-1)">后路一步</button>
    <button @click="go(3)">前进三步</button>
    <button @click="go(-3)">后路三步</button>
    <router-view></router-view>
  </div>
</body>

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script type="text/javascript">

const Index = Vue.component('index', {
  template: '<div>Hello,欢迎使用网学习 Vue 教程!</div>',
})

const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 计算的学习</li><li>2. Vue 侦听器的学习</li></ul>`,
})


const routes = [
  { path: '/index', component: Index },
  { path: '/article', component: Article }
]

const router = new VueRouter({
  routes: routes
})

  var vm = new Vue({
    el: '#app',
    router,
    data() {
    	return {}
    },
    methods: {
      go(n) {
        this.$router.go(n)
      }
    }
  })
</script>
</html>

解释:
HTML 第 15-18 行,我们定义了四个按钮,并给他们点击事件 go。
JS 第 29-31 行,我们定义来 go ,通过 router.go 实现路由。

本节,我们带大家学习了 VueRouter 如何通过来实现。主要知识点有以下几点:


联系我
置顶