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
Preface Since many friends say they don’t have Ma...
Table of contents Overview From Binary Tree to B+...
Table of contents What is axios? Axios request ty...
1. Download the required packages wget -P /usr/lo...
Preface I have been busy developing a cold chain ...
1. System environment The system version after yu...
I have always used Loadrunner to do performance t...
There are two ways to delete data in MySQL: Trunc...
Since HTML email is not an independent HOST page o...
Because the Raspberry Pi is based on ARM architec...
Recent projects involve the creation of a lot of ...
Table of contents describe accomplish The project...
1. Resume nacos database Database name nacos_conf...
Just like this effect, the method is also very si...
If you are using Alibaba Cloud Server, you need t...