加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

VueRouter 编程式导航用法

发布时间:2023-04-03 13:56:24 所属栏目:教程 来源:
导读:我们的路由跳转是通过标签 <router-link> 来完成的。但有时候,我们可能需要通过一个普通的 onClick 事件来完成跳转。router.push 就可以帮我们实现这一点。

让我们来看一个简单的示例:

<!DOCTYPE html>
<ht
我们的路由跳转是通过标签 <router-link> 来完成的。但有时候,我们可能需要通过一个普通的 onClick 事件来完成跳转。router.push 就可以帮我们实现这一点。

让我们来看一个简单的示例:

<!DOCTYPE html>
<html lang="en">
<head>
  <Meta charset="UTF-8">
  <Meta name="viewport" content="width=device-width, initial-scale=1.0">
  <Meta 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>
  <Meta charset="UTF-8">
  <Meta name="viewport" content="width=device-width, initial-scale=1.0">
  <Meta 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>
  <Meta charset="UTF-8">
  <Meta name="viewport" content="width=device-width, initial-scale=1.0">
  <Meta 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 实现路由跳转。

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章