Requirements encountered in actual projectsThe same link needs to load different page components. Different pages are displayed depending on the services purchased by the user. There are some bad implementations
I personally feel that the better way to implementGet server data where the route is configured and dynamically load the corresponding components { path: 'shopKPI', // If you save the background data to the store in advance, you can directly judge it by accessing the store data here // But the data of this specific business page is placed in the global store and is not used anywhere else, so it is really unnecessary component: () => import('@/views/store/dataVersion'), name: 'store_KPI', menuName: 'Shop Consultant', meta: { codes: ['storeProduct.detail'] } } The ideal is beautiful, but the reality is that the method received by the component must return a promise synchronously. At this time, I thought of the bad implementation method 1 above and modified it slightly. <!-- ChooseShopKPI.vue --> <template> <dataVersion v-if="!useNewShopKPI" /> <ShopKPI v-else /> </template> <script> import { get } from 'lodash'; import { getStoreReportFormVersion } from '@/api/store'; import dataVersion from './dataVersion'; import ShopKPI from './ShopKPI'; export default { name: 'ChooseShopKPI', components: dataVersion, ShopKPI, }, data() { return { useNewShopKPI: false }; }, created() { getStoreReportFormVersion().then((res) => { if (get(res, 'data.data.new')) { this.useNewShopKPI = true; } }); }, }; </script> <style lang="css" scoped></style> Change the page corresponding to the route rendering to render this intermediate page ChooseShopKPI { path: 'shopKPI', // If you get the background data in advance, you can directly judge it by accessing the store data here // But the data of this specific business page is placed in the global store and is not used anywhere else, so it is really unnecessary - component: () => import('@/views/store/dataVersion'), + component: () => import('@/views/store/ChooseShopKPI'), name: 'store_KPI', menuName: 'Shop Consultant', meta: { codes: ['storeProduct.detail'] } } This achieves the functionality we expect. The function has been realized, but I started to think about it againAlthough this method solves the problem of dynamically loading page components very well. But some minor problems also arose.
The final solution - high-level componentsBy abstracting ChooseXXX, transform it into DynamicLoadComponent <!-- DynamicLoadComponent.vue --> <template> <component :is="comp" /> </template> <script> export default { name: 'DynamicLoadComponent', props: { renderComponent: { type: Promise, }, }, data() { return { comp: () => this.renderComponent } }, mounted() {}, }; </script> <style lang="css" scoped></style> Get the background data directly in the routing configuration and distribute the routes. In this way, the routing logic is concentrated in the routing configuration file, and there is no secondary routing. Maintenance won’t be a headache. The DynamicLoadComponent component can also be reused, and any subsequent routing configurations for determining the background data loading page can be directed to this intermediate component. { path: 'shopKPI', component: () => import('@/views/store/components/DynamicLoadComponent'), name: 'store_KPI', menuName: 'Shop Consultant', meta: { codes: ['storeProduct:detail'], }, props: (route) => ({ renderComponent: new Promise((resolve, reject) => { getStoreReportFormVersion() .then((responseData) => { const useNewShopKPI = get(responseData, 'data.data.shop_do'); const useOldShopKPI = get( responseData, 'data.data.store_data_show' ); if (useNewShopKPI) { resolve(import('@/views/store/ShopKPI')); } else if (useOldShopKPI) { resolve(import('@/views/store/dataVersion')); } else { resolve(import('@/views/store/ShopKPI/NoKPIService')); } }) .catch(reject); }), }) } View online example (only supports Chrome) This is the end of this article about Vue Router loading different components according to background data. For more information about Vue Router loading different components according to background data, 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:
|
<<: Solution to ERROR 1054 (42S22) when changing password in MySQL 5.7
>>: Detailed explanation of permission management commands in Linux (chmod/chown/chgrp/unmask)
The requirements are as follows Export the table ...
If you use docker search centos in Docker Use doc...
Table of contents 1. Component Communication 1. P...
1. Framework A browser document window can only d...
<br />It has been no more than two years sin...
1. Download MySQL Click on the official website d...
Deepin 2014 download and installation For downloa...
HTML tags have special tags to handle the title of...
<body> <div id="root"> <...
Problem description: The Linux system's netwo...
JDK download address: http://www.oracle.com/techn...
When developing for mobile devices, you often enc...
There are two ways to expose container ports in d...
How can you forget lazy loading of routes that al...
Preface I believe most people have used MySQL and...