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()


基础教程推荐
猜你喜欢
- layui自定义组件根据id获取id内的form值 2023-08-31
- HTML5 文件域+FileReader 分段读取文件并上传到服务 2022-09-16
- 详解浏览器的缓存机制 2022-11-16
- js如何根据id删除数组中对象 2023-11-30
- 我如何用innerhtml中的ajax和php更新mysql数据库 2023-10-26
- WebGL 多重纹理的使用介绍 2024-01-06
- 关于ajax网络请求的封装实例 2023-01-20
- 图文解析AJAX的原理 2023-01-20
- 将多个查询数据合并到单个HTML表中(PHP,MySQL) 2023-10-26
- document.execCommand()的用法小结 2023-12-20