vue router 跳转方式

router-link

(1). 不带参数

//name,path都行, 建议用name
// 注意:router-link中链接如果是’/‘开始就是从根路由开始,如果开始不带’/’,则从当前路由开始。

(2).带参数

// params传参数 (类似post)

// 路由配置 path: “/home/:id” 或者 path: “/home:id”

// 不配置path ,第一次可请求,刷新页面id会消失

// 配置path,刷新页面id会保留

// html 取参 $route.params.id

// script 取参 this.$route.params.id

// query传参数 (类似get,url后面会显示参数)

// 路由可不配置

// html 取参 $route.query.id

// script 取参 this.$route.query.id


2.this.$router.push() (函数里面调用)

(1). 不带参数
this. r o u t e r . p u s h ( ′ / h o m e ′ ) t h i s . router.push('/home') this. router.push(/home)this.router.push({name:‘home’})
this.$router.push({path:’/home’})

(2). query传参

this. r o u t e r . p u s h ( n a m e : ′ h o m e ′ , q u e r y : i d : ′ 1 ′ ) t h i s . router.push({name:'home',query: {id:'1'}}) this. router.push(name:home,query:id:1)this.router.push({path:’/home’,query: {id:‘1’}})
// html 取参 r o u t e . q u e r y . i d / / s c r i p t 取 参 t h i s . route.query.id // script 取参 this. route.query.id//scriptthis.route.query.id

(3). params传参

this.$router.push({name:‘home’,params: {id:‘1’}}) // 只能用 name
// 路由配置 path: “/home/:id” 或者 path: “/home:id” ,
// 不配置path ,第一次可请求,刷新页面id会消失
// 配置path,刷新页面id会保留
// html 取参 r o u t e . p a r a m s . i d / / s c r i p t 取 参 t h i s . route.params.id // script 取参 this. route.params.id//scriptthis.route.params.id

3. query和params区别
query类似 get, 跳转之后页面 url后面会拼接参数,类似?id=1, 非重要性的可以这样传, 密码之类还是用params刷新页面id还在

params类似 post, 跳转之后页面 url后面不会拼接参数 , 但是刷新页面id 会消失

4.this.$router.replace() (用法同上,push)

5.this.$router.go(n)

向前或者向后跳转n个页面,n可为正整数或负整数
ps : 区别
this.router.push跳转到指定url路径,并想history栈中添加一个记录,点击后退会返回到上一个页面this.router.push跳转到指定url路径,并想history栈中添加一个记录,点击后退会返回到上一个页面this.router.replace
跳转到指定url路径,但是history栈中不会有记录,点击返回会跳转到上上个页面 (就是直接替换了当前页面)
this.$router.go(n)
向前或者向后跳转n个页面,n可为正整数或负整数

<think>嗯,用户想了解如何在 Vue Router 中实现页面跳转。首先,我需要回忆一下Vue Router的基本使用方法。记得Vue RouterVue.js官方的路由管理器,用于构建单页面应用。页面跳转主要有两种方式:使用`<router-link>`组件和编程式导航。 首先,`<router-link>`是通过在模板中声明式地创建链接,用户点击后跳转到指定路由。需要设置`to`属性指定路径,比如`<router-link to="/home">Home</router-link>`。可能还需要提到动态路由参数,比如传递用户ID的情况,这时候可以用`:to`绑定对象,包含name和params。 然后是编程式导航,也就是在JavaScript代码中使用`this.$router.push`方法。比如在方法里调用`this.$router.push('/home')`或者传递一个对象`this.$router.push({ name: 'user', params: { id: 123 }})`。这里要注意,如果提供了path,params会被忽略,所以最好用name配合params。 另外,用户可能想知道如何传递参数,比如query参数和params的区别。query参数会显示在URL里,比如`/user?id=123`,而params需要预先在路由配置中定义,比如`path: '/user/:id'`,然后通过params传递。 还需要提到路由配置,比如在router/index.js中定义routes数组,每个路由对象要有path、component等属性。命名路由的使用可以让跳转更方便,避免硬编码路径。 可能还需要给出示例代码,比如一个简单的路由配置,以及如何在组件中使用两种跳转方式。要注意代码的格式是否正确,比如使用模板字符串和正确的语法。 用户提供的引用中提到了按钮跳转新页面,但Vue Router默认是单页面应用内的跳转,不会真正打开新标签页。如果用户需要打开新页面,可能需要使用`window.open`或者在`<router-link>`中添加target="_blank",但这可能不符合预期,因为Vue Router管理的是同一个页面内的视图切换。 另外,历史模式也是一个点,比如使用HTML5 History模式还是哈希模式。但用户的问题主要集中在跳转方法,可能暂时不需要深入讨论这个。 最后,要检查是否需要覆盖所有常见的跳转场景,比如带参数、命名路由、替换当前路由(使用replace方法)、前进后退等。但根据用户的问题,可能先聚焦在基本的使用方法和示例上,确保回答结构清晰,步骤明确。</think>### Vue Router 页面跳转使用方法详解 #### 一、基础配置步骤 1. **安装与引入** 通过npm安装Vue Router: ```bash npm install vue-router ``` 在项目中引入并配置路由: ```javascript // router/index.js import { createRouter, createWebHistory } from 'vue-router' import Home from '../views/Home.vue' const routes = [ { path: '/', name: 'Home', component: Home }, { path: '/user/:id', name: 'User', component: () => import('../views/User.vue') } ] const router = createRouter({ history: createWebHistory(), routes }) ``` #### 二、两种核心跳转方式 1. **声明式导航** 使用`<router-link>`组件实现页面跳转: ```html <!-- 基础跳转 --> <router-link to="/home">首页</router-link> <!-- 动态参数传递 --> <router-link :to="{ name: 'User', params: { id: 123 }}">用户页</router-link> <!-- 带查询参数 --> <router-link :to="{ path: '/search', query: { keyword: 'vue' }}">搜索</router-link> ``` 2. **编程式导航** 通过`$router`实例方法控制跳转: ```javascript // 基本跳转 this.$router.push('/home') // 命名路由带参数 this.$router.push({ name: 'User', params: { id: 123 }}) // 带查询参数(URL显示?key=value) this.$router.push({ path: '/search', query: { keyword: 'vue' }}) // 替换当前历史记录(无后退) this.$router.replace('/new-page') ``` #### 三、参数接收与使用 在目标组件中获取参数: ```javascript // 获取params参数 const userId = this.$route.params.id // 获取query参数 const searchKey = this.$route.query.keyword ``` #### 四、进阶功能示例 ```javascript // 前进/后退控制 this.$router.go(1) // 前进1步 this.$router.go(-2) // 后退2步 // 监听路由变化 watch: { '$route'(to, from) { console.log('从', from.path, '跳转到', to.path) } } ``` #### 五、注意事项 1. 使用`params`时需预先在路由配置中声明参数占位符,如`/user/:id` 2. `query`参数会显示在URL地址栏中 3. 编程式导航的路径参数优先级:`name > path`[^1] 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值