vue路由跳转的两种方式

query和params区别

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

一 . 声明式 router-link (利用标签跳转)

  1.0 不带参数   形如:http://localhost:3000/#/home/newslist
 2
 3 // router.js 路由配置
 4 { path: ‘/home/newslist‘, name:‘newslist‘ component: Newslist }
 5
 6 <router-link to="/home/newslist">
 7 <router-link :to="{name:‘newslist‘}">
 8 <router-link :to="{path:‘/home/newslist‘}"> //name,path都行, 建议用name
 9
10 // 注意:router-link中链接如果是‘/‘开始就是从根路由开始,如果开始不带‘/‘,则从当前路由开始
11
12
 2.1带参数   形如(例如从商品进入详情):http://localhost:3000/#/home/newsinfo/13
14
15 // router.js 路由配置
16 { path: ‘/home/newsinfo/:id‘, name: ‘newsinfo‘, component: Newsinfo } //或者
17 { path: ‘/home/newsinfo:id‘, name: ‘newsinfo‘, component: Newsinfo }
18
19 <router-link :to="‘/home/newsinfo/‘ + item.id">
20 <router-link :to="{name:‘newsinfo‘, params: {id:item.id}}"> //router.js用上面的
21
22 // params传参数 (类似post) 直接跟参数的形式
23 // 不配置path ,第一次可请求,刷新页面id会消失
24 // 配置path,刷新页面id会保留
25
26 // html 取参  $route.params.id
27 // script 取参  this.$route.params.id
28
29 2.2带参数 形如(例如从商品进入详情):http://localhost:3000/#/home/newsinfo?id=13
30
31 // router.js 路由配置
32 { path: ‘/home/newsinfo‘, name: ‘newsinfo‘, component: Newsinfo }
33
34 <router-link :to="‘/home/newsinfo?id=‘ + item.id">
35 <router-link :to="‘/home/newsinfo?id=‘ + item.id">
36
37 // query传参数 (类似get,url后面会显示参数)
38 // 路由可不配置
39 // html 取参  $route.query.id
40 // script 取参  this.$route.query.id

 二: 编程式:this.$router.push() (点击事件传id,函数里面接收调用)

1.  不带参数 形如:http://localhost:3000/#/home/newslist

 // router.js 路由配置
{ path: ‘/home‘, name:‘home‘ component: Home }

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

2.1. params传参   形如(例如从商品进入详情):http://localhost:3000/#/home/13

// router.js 路由配置
{ path: ‘/home/:id‘, name: ‘home‘, component: Home } //或者
{ path: ‘/home:id‘, name: ‘home‘, component: Home }

this.$router.push(‘/home/‘ + id)
this.$router.push({name:‘home‘,params: {id:id}})  // 只能用 name

 // params传参数 (类似post) 直接跟参数的形式
// 不配置path ,第一次可请求,刷新页面id会消失
// 配置path,刷新页面id会保留

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

2.2. query传参 形如(例如从商品进入详情):http://localhost:3000/#/home?id=13

 // router.js 路由配置
{ path: ‘/home‘, name: ‘home‘, component: Home }

this.$router.push(‘/home?id=‘ + id)
this.$router.push({name:‘home‘,query: {id:id}})
this.$router.push({path:‘/home‘,query: {id:id}})

// query传参数 (类似get,url后面会显示参数)
// html 取参  $route.query.id
// script 取参  this.$route.query.id

 

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

this.$router.go(n)

原文地址:https://www.cnblogs.com/shun1015/p/12337857.html

时间: 2024-07-30 06:08:16

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

vue 页面跳转的两种方式

1,标签跳转     <router-link to='two'><button>点我到第二个页面</button></router-link> 2,点击事件跳转    html : <button @click="hreftwo" class="test-one">点我到第二个页面</button>   js : methods:{ //跳转页面 hreftwo(){ this.$router

Vue路由传参的几种方式

原 Vue路由传参的几种方式 2018年07月28日 23:52:40 广积粮缓称王 阅读数 12613 前言:顾名思义,vue路由传参是指嵌套路由时父路由向子路由传递参数,否则操作无效.传参方式可以划分为params传参和query传参,params传参又可以分为url中显示参数和不显示参数两种方式.具体区分和使用后续分析. 参考官网:https://router.vuejs.org/zh/guide/essentials/navigation.html params传参(url中显示参数)

利用intent跳转的两种方式(有无参数返回)

从一个activity跳转到另一个activity有两种方式,一种是无参数返回的,一种是有参数返回的: 1,无参数返回 Intent  intent=new   intent(this,activity.class) this.startActivity(intent); 2,有参数返回 Intent  intent=new   intent(this,activity.class); this.startActivityForResult(Intent,requestCode); 复写onAc

web项目中实现页面跳转的两种方式

<a href="javascript:"></a>跳转在网页本身,URL不改变 <a href="#"></a> 跳转在网页本身,URL 改变 java web项目中实现页面跳转的主要方式有两种:第一种,<% response.sendRedirect("index.jsp");%>第二种<jsp:forward page="index.jsp"/>我做

使用JS实现页面内跳转的两种方式

第一种方式是直接使用锚点配合链接标签: <h2 id="h2-anchor">Scroll to here</h2> <a href="#h2-anchor">Jump to H2</a> 现在大多数实现都采用该种方式.但是这种方式没有动画效果,跳转是直接发生的. 第二种方式使用jQuery中的animate方法实现: var target= $('#h2-anchor').offset().top; $('body'

Vue路由实现页面跳转的两种方式(router-link和JS)

Vue.js 路由可以通过不同的 URL 访问不同的内容,实现多视图的单页 Web 应用 1.通过 <router-link> 实现 <router-link> 组件用于设置一个导航链接,切换不同 HTML 内容 使用方法: 简单写法 <router-link to="demo2">demo2</router-link> 使用 v-bind 的写法 <router-link :to="'demo2'">de

vue实现组件切换的两种方式

<!DOCTYPE html> <html> <head> <title>组件的切换</title> <meta charset="utf-8"> </head> <body> <!-- 方式一:结合v-if及v-else 只能实现两个组件的切换--> <div id="app"> <!-- 添加事件修饰符prevent,阻止跳转 -->

vue页面是否缓存的两种方式

第一种 <keep-alive> <router-view v-if="$route.meta.keepAlive"></router-view> </keep-alive> <router-view v-if="!$route.meta.keepAlive"></router-view> 配合路由 //在router文件加上meta判断 import Vue from 'vue' import

js实现页面跳转的两种方式

CreateTime--2017年8月24日08:13:52Author:Marydon 方式一: window.location.href = url 说明:我们常用来在js中实现页面跳转的方法,使用get方式发送请求,传参有限 更多介绍,见文章:js操作当前窗口 方式二: 通过POST请求实现页面跳转 /** * 发送POST请求跳转到指定页面 * @param URL * 跳转路径 * @param PARAMS * 参数:格式-JSON对象 */ function httpPost(UR