Home > Web Front-end > Front-end Q&A > How to implement route jump and value passing in vuejs

How to implement route jump and value passing in vuejs

青灯夜游
Release: 2021-10-26 14:51:44
Original
2494 people have browsed it

Vuejs route jump value transfer method: 1. Use the "

How to implement route jump and value passing in vuejs

The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.

There are many types of route jump parameters in vue. The ones I commonly use are the following ones

  • Jump through router-link

  • Route jump through programmatic navigation

1. router-link

<router-link 
 :to="{
  path: &#39;yourPath&#39;, 
  params: { 
   name: &#39;name&#39;, 
   dataObj: data
  },
  query: {
   name: &#39;name&#39;, 
   dataObj: data
  }
 }">
</router-link>

 1. path -> 是要跳转的路由路径,也可以是路由文件里面配置的 name 值,两者都可以进行路由导航
 2. params -> 是要传送的参数,参数可以直接key:value形式传递
 3. query -> 是通过 url 来传递参数的同样是key:value形式传递

 // 2,3两点皆可传递
Copy after login

2, $router way to jump

// 组件 a
<template>
 <button @click="sendParams">传递</button>
</template>
<script>
 export default {
 name: &#39;&#39;,
 data () {
  return {
  msg: &#39;test message&#39;
  }
 },
 methods: {
  sendParams () {
  this.$router.push({
   path: &#39;yourPath&#39;, 
   name: &#39;要跳转的路径的 name,在 router 文件夹下的 index.js 文件内找&#39;,
   params: { 
    name: &#39;name&#39;, 
    dataObj: this.msg
   }
   /*query: {
    name: &#39;name&#39;, 
    dataObj: this.msg
   }*/
  })
  }
 },
 computed: {

 },
 mounted () {

 }
 }
</script>
<style scoped></style>
----------------------------------------
// 组件b
<template>
 <h3>msg</h3>
</template>
<script>
 export default {
 name: &#39;&#39;,
 data () {
  return {
  msg: &#39;&#39;
  }
 },
 methods: {
  getParams () {
  // 取到路由带过来的参数 
  let routerParams = this.$route.params.dataobj
  // 将数据放在当前组件的数据内
  this.msg = routerParams
  }
 },
 watch: {
 // 监测路由变化,只要变化了就调用获取路由参数方法将数据存储本组件即可
  &#39;$route&#39;: &#39;getParams&#39;
 }
 }
</script>
<style scoped></style>
Copy after login

Related recommendations: "vue.js tutorial"

The above is the detailed content of How to implement route jump and value passing in vuejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template