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

How to implement route jump and value passing in vuejs

青灯夜游
青灯夜游Original
2021-10-26 14:51:442392browse

Vuejs route jump value transfer method: 1. Use the "7c87333a3ceb3b82d97d1b332587be67" statement to jump to transfer value; 2. Use "this.$router. push({ path:'/user'})" statement to jump and transfer values.

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两点皆可传递

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>

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!

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