When we develop a single-page application, sometimes we need to enter a certain route and get data from the server based on parameters. Then we must first get the parameters passed by the route to complete the server request. Therefore, we need to understand several ways of passing parameters by the route. The following methods are the same as Programmatic routing parameters In addition to using 1. Pass through paramsRouting Configuration Path parameters are represented by a colon const routes = [ // Dynamic sections start with a colon { path: 'details/:id', name: "details", component: Details }, ] The parameter of const Home = { template: '<div @click="toDetails">To Details</div>', methods: toDetails() { // string path this.$router.push('/details/001') // Object with path this.$router.push({path: '/details/001'}) // Name the route. When configuring the route, the name field is required this.$router.push({ name: 'details', params: { id: '001' } }) } } } Note that if // `params` cannot be used with `path` router.push({ path: '/details', params: { id: '001' } }) // -> /details Components get data When a route is matched, the value of its params will be exposed in every component as const Details = { template: '<div>Details {{ $route.params.id }} </div>', created() { // Listen for route changes this.$watch( () => this.$route.params, (toParams, previousParams) => { // Respond to route changes... } ) }, } 2. Pass through query In this case, the parameters passed by Routing Configuration When using this.$router.push('/details/001?kind=car') this.$router.push({ path: '/details/001', query: { kind: "car" }}) this.$router.push({ name: 'details', params: { id: '001' }, query: { kind: 'car' }}) Components get data Components are obtained through const Details = { template: '<div>Details {{ $route.query.kind }} </div>', created() { // Listen for route changes this.$watch( () => this.$route.query, (toParams, previousParams) => { // Respond to route changes... } ) }, } To respond to parameter changes within the same component, you can simply watch any property on the 3. Passing via hash In this way, the url path contains Routing Configuration When using this.$router.push('/details/001#car') this.$router.push({ path: '/details/001', hash: '#car'}) this.$router.push({ name: 'details', params: { id: '001' }, hash: 'car'}) Components get data The component is obtained through const Details = { template: '<div>Details {{ $route.hash.slice(1) }} </div>', } Passing via props Using Use 1. Boolean mode When For example, the following code obtains the dynamic field const User = { template: '<div>User {{ $route.params.id }}</div>' } const routes = [{ path: '/user/:id', component: User }] Replace the above code with const User = { props: ['id'], // Get id through props in the component template: '<div>User {{ id }}</div>' } // In the routing configuration, add the props field and set the value to true const routes = [{ path: '/user/:id', component: User, props: true }] Note: For routes with named views, you must define const routes = [ { path: '/user/:id', components: { default: User, sidebar: Sidebar }, // Provide props for User props: { default: true, sidebar: false } } ] 2. Object Mode When Routing Configuration const routes = [ { path: '/hello', component: Hello, props: { name: 'World' } } ] Get data in component const Hello = { props: { name: { type: String, default: 'Vue' } }, template: '<div> Hello {{ name }}</div>' } The 3. Functional Mode You can create a function that returns props. This allows you to convert parameters to other types, combine static values with route-based values, and more. Routing Configuration When using the function mode, the function that returns props receives // Create a function that returns props const dynamicPropsFn = (route) => { return { name: route.query.say + "!" } } const routes = [ { path: '/hello', component: Hello, props: dynamicPropsFn } ] Components get data When the URL is const Hello = { props: { name: { type: String, default: 'Vue' } }, template: '<div> Hello {{ name }}</div>' } At this point the page will render: Note: Keep the Other ways1. Pass through Vuex 1. store storage status; 2. Through front-end local storage and other methods 1. Local Storage; This concludes this article about 8 ways to pass parameters in Vue routing components. For more information about passing parameters in Vue routing components, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: MySQL 5.5 installation and configuration graphic tutorial
Because Ubuntu 20.04 manages the network through ...
Table of contents Project Directory Dockerfile Fi...
Table of contents Overview 1. Compositon API 1. W...
Principle: First hide the input element, then use...
I used js to create a package for converting Chin...
1. CSS background tag 1. Set the background color...
Table of contents Preface 1. Split a string 2. JS...
1. Storage Engine In the last section, we mention...
Table of contents Make scrolling smoother BetterS...
Preface This article mainly shares with you an ex...
React Hooks is a new feature introduced in React ...
I'm working on electronic archives recently, ...
Table of contents Overview 1. Define store.js 2. ...
Table of contents 1. v-text text rendering instru...
Preface: MySQL is a relational database managemen...