vue路由跳转的方式

vue路由跳转有四种方式

1. router-link

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

3. this.$router.replace() (用法同push)

4. this.$router.go(n)

一、不带参

1.1 router-link

<router-link :to="{name:‘home‘}"> <router-link :to="{path:‘/home‘}"> //name,path都行, 建议用name // 注意:router-link中链接如果是‘/‘开始就是从根路由开始,如果开始不带‘/‘,则从当前路由开始。

1.2 this.$router.push()

this.$router.push(‘/home‘) this.$router.push({name:‘home‘}) this.$router.push({path:‘/home‘})

1.3 this.$router.replace() (用法同push)

二、带参

2.1 router-link

<router-link :to="{name:‘home‘, params: {id:1}}"> // params传参数 (类似post) // 路由配置 path: "/home/:id" 或者 path: "/home:id" // 不配置path ,第一次可请求,刷新页面id会消失 // 配置path,刷新页面id会保留 // html 取参 $route.params.id // script 取参 this.$route.params.id <router-link :to="{name:‘home‘, query: {id:1}}"> // query传参数 (类似get,url后面会显示参数) // 路由可不配置 // html 取参 $route.query.id // script 取参 this.$route.query.id

2.2 this.$router.push(query传参

this.$router.push({name:‘home‘,query: {id:‘1‘}}) this.$router.push({path:‘/home‘,query: {id:‘1‘}}) // html 取参 $route.query.id // script 取参 this.$route.query.id params传参 this.$router.push({name:‘home‘,params: {id:‘1‘}}) // 只能用 name // 路由配置 path: "/home/:id" 或者 path: "/home:id" , // 不配置path ,第一次可请求,刷新页面id会消失 // 配置path,刷新页面id会保留 // html 取参 $route.params.id // script 取参 this.$route.params.id

2.3 this.$router.replace() (用法同push)

this.$router.go(n)

this.$router.go(n) this.$router.go(n) 向前或者向后跳转n个页面,n可为正整数或负整数

区别:

this.$router.push 跳转到指定url路径,并想history栈中添加一个记录,点击后退会返回到上一个页面

this.$router.replace 跳转到指定url路径,但是history栈中不会有记录,点击返回会跳转到上上个页面 (就是直接替换了当前页面)

this.$router.go(n) 向前或者向后跳转n个页面,n可为正整数或负整数

__EOF__

来        源:麋鹿鲁哟

出  处:https://www.cnblogs.com/miluluyo/p/11190648.html

原文地址:https://www.cnblogs.com/huchong-bk/p/11613138.html

时间: 2024-08-26 17:43:06

vue路由跳转的方式的相关文章

详解vue 路由跳转四种方式 (带参数)

详解vue 路由跳转四种方式 (带参数):https://www.jb51.net/article/160401.htm 1.  router-link ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1. 不带参数  <router-link :to="{name:'home'}"> <router-link :to="{path:'/home'}"> //name,path都行, 建议用name /

2种方式解决vue路由跳转未匹配相应路由避免出现空白页面或者指定404页面

https://www.cnblogs.com/goloving/p/9254084.html https://www.cnblogs.com/goloving/p/9254084.html 1.路由全局守卫 在做项目的时候,遇到需要做路由跳转,但当用户输入错误url地址,或是其它非法url路由地址,我们或许会想到跳转至404页面.不管你有没有写一个404页面,当出现未匹配路由都需重新指定页面跳转.可能大家首先想到会是路由重定向,redirect来解决这个问题.但实际上通过redirect是没办

vue路由跳转时判断用户是否登录功能

通过判断该用户是否登录过,如果没有登录则跳转到login登录路由,如果登录则正常跳转. 一丶首先在用户登录前后分别给出一个状态来标识此用户是否登录(建议用vuex): 简单用vuex表示一下,不会可以自己去官网多看看: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); var state = { isLogin:0, //初始时候给一个 isLogin=0 表示用户未登录 }; const mutations = { cha

Vue路由跳转问题记录

最近项目上需要用Vue用来做app,在Vue中使用路由时遇到下面的问题. 路由设置如下: { path:'/tab', component:Tab, children:[{ path:'layoutList', name:'LayoutList', component:LayoutList },{ path:'layoutView/:layoutId', name:'LayoutView', component:LayoutView },{ path:'layoutDetail/:viewId'

vue 路由跳转出现的问题

在 export defaul new Router({ )} 这个路由配置中一定要加mode : 'history' 否者就会在路由前面默认添加# 路由跳转的几种方式: 原文地址:https://www.cnblogs.com/lzzey/p/8461214.html

Vue路由跳转

路由跳转 this.$router.push('/course'); this.$router.push({name:'course'}) this.$router.go(-1) // js逻辑使用history,完成返回上一页 this.$router.go(1) // 前进一页 <router-link to="/course">课程页</router-link> <router-link :to="{name:'course'}"

vue 路由跳转,路由传参的几种方式

1. router-link <router-link :to="{ path: 'yourPath', params: { name: 'name', dataObj: data }, query: { name: 'name', dataObj: data } }"> </router-link> 2. $router方式跳转 this.$router.push({ path: 'yourPath', query: { name: 'name', dataO

vue路由跳转的多种方式

1.router-link to 跳转 <router-link to="/child"><button>跳转</button></router-link> 2.this.$router.push("ComponentName") ,通过路由名称跳转 <button @click="go()">跳转</button> go(){ this.$router.push("

vue路由跳转的两种方式

query和params区别 query类似 get, 跳转之后页面 url后面会拼接参数,类似?id=1, 非重要性的可以这样传, 密码之类还是用params刷新页面id还在 params类似 post, 跳转之后页面 url后面不会拼接参数 , 但是刷新页面id 会消失 一 . 声明式 router-link (利用标签跳转) 1.0 不带参数 形如:http://localhost:3000/#/home/newslist 2 3 // router.js 路由配置 4 { path: '