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
Copy code The code is as follows: <div style=&...
There are currently three ways to display the cen...
HTML <div class="spinner"></di...
Table of contents introduce Object attributes in ...
1. When to execute setUp We all know that vue3 ca...
Preface The string types of MySQL database are CH...
Table of contents 1. View hook 1. Things to note ...
You can often see articles about CSS drawing, suc...
1. Simple configuration of nginx's dynamic an...
The vue project built with cli3 is known as a zer...
1. Cause: The effect after the subbox is set to f...
In actual project development, if we have a lot o...
In general, MySQL provides a variety of storage e...
sudo configuration file The default configuration...
The author has been working on a micro-frontend p...