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‘">demo2</router-link>

<!-- 也可以用{}包裹对应的path或name -->
<router-link :to="{ name: ‘demo2‘ }">demo2</router-link>
  • 传参的写法

<router-link :to="{ name: ‘demo2‘, params: { userId: 123 }}">demo2</router-link>

这里传参需要在 router.js 中对 demo2 的路径进行配置,在 path 中 demo2 后添加通配符 : 和对应的 userId,如下:

{
  path: ‘/demo2/:userId‘,
  name: ‘demo2‘,
  component: demo2
},

配置完成后,页面跳转的结果就为 /demo2/123

这里的“123”就是上面的 userId

那么,如何在新页面中获取到传过来的参数 userId 呢?

在 mounted 钩子中使用 this.$route.params.xx. 获取传过来的参数,如下:

mounted () {
    alert(this.$route.params.userId)
}

// 弹出123
  • 传入地址键值对

<router-link :to="{ path: ‘demo2‘, query: { plan: ‘private‘ }}">demo2</router-link>

页面跳转的结果为 /demo2?plan=private

(注意这里不用在 router.js 里配置路径)

在新页面中获取到传过来的地址键值对 plan,可以在 mounted 钩子中使用 this.$route.plan.xx. 获取传过来的地址键值对,如下:

mounted () {
  alert(this.$route.query.plan)
}

// 弹出private

2、通过 JS 实现

template 部分:

<button @click="toURL">跳转页面</button>

script 部分:

(注意这里是 router,上面是 route)

  • 简单写法

methods:{
  toURL(){
    this.$router.push({ path: ‘/demo2‘ })
  }
}
  • 传参的写法

methods:{
  toURL(){
    this.$router.push({ name: ‘demo2‘, params: { userId: 123 }})
  }
}
  • 传入地址键值对

methods:{
  toURL(){
    this.$router.push({ name: ‘demo2‘, params: { userId: 123 }, query: { plan: ‘private‘ } })
  }
}

原文地址:https://www.cnblogs.com/Leophen/p/11265833.html

时间: 2024-08-27 18:01:01

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

vue 路由传参 params 与 query两种方式的区别

初学vue的时候,不知道如何在方法中跳转界面并传参,百度过后,了解到两种方式,params 与 query.然后,错误就这么来了:  router文件下index.js里面,是这么定义路由的: { path:"/detail", name:"detail", component:home } 我想用params来传参,是这么写的,嗯~ this.$router.push({ path:"/detail", params:{ name:'nameV

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

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

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

vue 页面跳转的两种方式

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

JavaScript实现页面跳转的五种方式

JavaScript实现页面跳转的五种方式 第一种:<script type="text/javascript" language="javascript"> window.location.href="login.jsp?backurl="+window.location.href;</script> 第二种:<script type="text/javascript" language=&qu

实现前端页面跳转的几种方式

实现前端页面跳转的几种方式 推荐使用 <script language='javascript'> document.location = 'http://mail.qq.com/domain/longtimenosee.cc' </script> 相关阅读 http://www.jb51.net/article/25403.htm http://my.oschina.net/ososchina/blog/340854

php页面跳转的几种方式

@: PHP页面跳转的三种方式 第一种方式:header() header()函数的主要功能是将HTTP协议标头(header)输出到浏览器. 语法: void header ( string $string [, bool $replace = true [, int $http_response_code ]] ) 可选参数replace指明是替换前一条类似标头还是添加一条相同类型的标头,默认为替换. 第二个可选参数http_response_code强制将HTTP相应代码设为指定值. he

利用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

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: '