router.push()是vue-router 2.0的一种基础跳转方式,用于在当前路由下添加一个新路由,并且将浏览器URL跳转到新路由地址,这是vue-router中最常用的一种跳转方式之一。
详解vue-router 2.0常用基础知识点之router.push()
1. 概述
router.push()是vue-router 2.0的一种基础跳转方式,用于在当前路由下添加一个新路由,并且将浏览器URL跳转到新路由地址,这是vue-router中最常用的一种跳转方式之一。
2. 语法
router.push(location, onComplete?, onAbort?)
location(type:Object | string): 目标路由的解析对象或路径onComplete(type:Function): 跳转成功后需要执行的自定义回调函数onAbort(type:Function): 在跳转被阻止时需要执行的自定义回调函数
3. 如何使用
- 通过路径跳转
我们可以使用一个包含目标路径的字符串作为router.push()里面的参数来实现路径跳转,例:
this.$router.push('/home');
- 通过命名路由跳转
我们可以使用一个包含目标命名路由的对象作为router.push()里面的参数来实现通过命名路由的方式进行跳转,例:
this.$router.push({ name: 'home' });
4. 注意事项
- 如果调用
router.push()时在beforeEach和beforeResolve中return一个false或者调用next(false),跳转将会被取消。 - 如果调用
router.push()失败,会被console.error()方法提醒。
5. 示例说明
<template>
<div>
<!-- 通过按钮触发跳转 -->
<button @click="goHome">回首页</button>
</div>
</template>
<script>
export default {
methods: {
goHome() {
this.$router.push('/home');
}
}
};
</script>
<template>
<div>
<!-- 通过按钮触发跳转 -->
<button @click="goHome">回首页</button>
</div>
</template>
<script>
export default {
methods: {
goHome() {
this.$router.push({ name: 'home' });
}
}
};
</script>
编程基础网
本文标题为:详解vue-router 2.0 常用基础知识点之router.push()
基础教程推荐
猜你喜欢
- 用纯CSS实现禁止鼠标点击事件示例代码 2023-12-21
- window.location 对象所包含的属性 2023-12-13
- 全面解析$.Ajax()方法参数(推荐) 2023-01-20
- 微信小程序 自己制作小组件实例详解 2023-12-13
- jQuery前端框架easyui使用Dialog时bug处理 2023-12-20
- css3+伪元素实现鼠标移入时下划线向两边展开的效果 2023-12-08
- JavaScript仿windows计算器功能 2022-08-31
- Ajax 入门之 GET 与 POST 的不同处详解 2023-01-31
- css reset样式重置介绍 重置css样式工具分享 2023-12-09
- Angular获取ngIf渲染的Dom元素示例 2023-07-09
