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)
To improve the performance of web pages, many dev...
SQL is the main trunk. Why do I understand it thi...
Introduction 1.<iframe> tag: iframe is an i...
Table of contents Join syntax: 1. InnerJOIN: (Inn...
For reference only for Python developers using Ub...
0. Prepare relevant tables for the following test...
To learn content-type, you must first know what i...
This article example shares the specific code of ...
Table of contents tool: Login scenario: practice:...
Table of contents 1. What is Pinia? 2. Pinia is e...
CSS Selectors Setting style on the html tag can s...
This article shares the specific code for js to r...
1. Download the latest nginx docker image $ docke...
Table of contents 1. Process Control 2. Sequentia...
This article shares the installation and configur...