Home > Web Front-end > JS Tutorial > How to pass router parameters in Vue.js

How to pass router parameters in Vue.js

php中世界最好的语言
Release: 2018-06-06 15:12:43
Original
1218 people have browsed it

This time I will show you how to pass router parameters in Vue.js, and what are the precautions for passing router parameters in Vue.js. The following is a practical case, let's take a look.

Vue-router parameter passing

Why do we need to pass parameters in the router

Imagine a scenario. Currently on the homepage, you need to click on a certain item to view detailed information about the item. Then at this time, you need to pass the id of the item to the details page on the home page, and the details page can obtain the detailed information through the id.

vue-router parameter passing method

Parma passing parameter

Post code:

/ router/index.vue

export default new Router({
 routes: [
  {
   path: '/',
   name: 'Home',
   component: Home
  },
  {
   path: '/work',
   name: 'Work',
   component: Work
  }
 ]
  })
Copy after login

The component Works passes a work id to the component Work

/components/Home/Comtent/Works.vue

// 触发它传递一个对象到组件Work
getIt (id) {
 this.$router.push({
  path: '/work',
  name: 'Work',
  params: {
   id: id
  }
 })
  }
Copy after login

/components/Work/ Index.vue

<template>
  <p class="work">
   work: {{id}}
  </p>
 </template>
 
 <script>
 export default {
  name: 'Work',
  data () {
   return {
    id: this.$route.params.id //拿到id
   }
  }
 }
 </script>
Copy after login

Running screenshot:

##query parameter

Just change the above parmas to query, that is:

// 传入
 this.$router.push({
path: '/work',
name: 'Work',
query: {
 id: id
}
 })
 
 ... ...
 
 this.$route.query.id // 获取
Copy after login
The difference between parmas and query

query passes parameters through the url and is always displayed in the url

parmas transfer parameters, there will be no data after refreshing the page, and the obtained parameters cannot be saved

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

React application development scaffolding use case

How to operate vue.js data binding

The above is the detailed content of How to pass router parameters in Vue.js. 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