introduction
This article willVue-Vben-AdminThe status management implementation of the source code is analyzed and interpreted. After reading it patiently, I believe you will definitely gain something!
System lock screen
documentsrc\store\modules\
Declare exporting a store instanceuseMultipleTabStore
, a methoduseMultipleTabWithOutStore()
For use without usesetup
Used when components.
// Multi-tag information storageexport const useMultipleTabStore = defineStore({ id: 'app-multiple-tab', state: { /*...*/ }, getters: { /*...*/ } actions:{ /*...*/ } }); export function useMultipleTabWithOutStore() { return useMultipleTabStore(store); }
State/Getter
The status object defines the tab route list, cached tab name, and index of the last drag tag. Also provided is a corresponding method for obtaining the status value.
// Multi-tab statusexport interface MultipleTabState { cacheTabList: Set<string>; // Cache Tab Routing Name // Tag page routing list RouteLocationNormalized The standardized version of the routing record tabList: RouteLocationNormalized[]; lastDragEndIndex: number; // The index of the label was dragged for the last time} state: (): MultipleTabState => ({ cacheTabList: new Set(), tabList: cacheTab ? (MULTIPLE_TABS_KEY) || [] : [], // Priority loading of cache/local storage content lastDragEndIndex: 0, }), getters: { // Get the tag page routing list getTabList(): RouteLocationNormalized[] { return ; }, // Get the cached tab page route name list getCachedTabList(): string[] { return (); }, // Get the index of the last drag label getLastDragEndIndex(): number { return ; }, },
Actions
methodaddTab
Method is used to open a tab.
- Determine whether the currently open special page (Error handling/login/redirect).
- If there is a tianj tab with the same path opened, update its tab routing record, otherwise add a new page routing record.
- Update the tag page route name that needs to be cached and persist with local storage.
// Open the tabasync addTab(route: RouteLocationNormalized) { // Basic routing properties const { path, name, fullPath, params, query, meta } = getRawRoute(route); // Error handling page Login Redirection etc page if ( path === PageEnum.ERROR_PAGE || path === PageEnum.BASE_LOGIN || !name || [REDIRECT_ROUTE.name, PAGE_NOT_FOUND_ROUTE.name].includes(name as string) ) { return; } let updateIndex = -1; // The tag page already exists, no tags are added repeatedly const tabHasExits = ((tab, index) => { updateIndex = index; return ( || ) === (fullPath || path); }); // The tag already exists, perform update operation if (tabHasExits) { const curTab = toRaw()[updateIndex]; // Get the current tab routing record if (!curTab) { return; } = params || ; // Dictionary of decoded parameters extracted from path = query || ; // Dictionary of decoded query parameters extracted from the search part of the URL. = fullPath || ; // The URL encoding is related to the routing address. Includes path, query and hash. (updateIndex, 1, curTab); // Replace the original tag page routing record } else { // Add a tag page // Get the number of dynamic route openings. If it exceeds 0, it means that it needs to control the number of openings. const dynamicLevel = meta?.dynamicLevel ?? -1; if (dynamicLevel > 0) { // If the setting is greater than 0, then the number of opens of the route must be limited // First get the real route and use configuration method to reduce calculation overhead. // const realName: string = (/(\S*)\//)![1]; const realPath = meta?.realPath ?? ''; // Get the number of dynamic routes that have been opened and determine whether they are greater than a certain value if ( ((e) => ?.realPath ?? '' === realPath).length >= dynamicLevel ) { // Close the first one const index = ((item) => === realPath); index !== -1 && (index, 1); } } (route); // Add to the routing list } (); // Persist using local storage cacheTab && (MULTIPLE_TABS_KEY, ); },
methodupdateCacheTab
Used to update the tag page route name that needs to be cached and return a Set collection. If the route is inmeta
Settings inignoreKeepAlive
fortrue
, this tab will not be cached.
// Update the cache according to the currently opened tagasync updateCacheTab() { // Set collection storage const cacheMap: Set<string> = new Set(); for (const tab of ) { const item = getRawRoute(tab); // If the KeepAlive cache is ignored, no cache const needCache = !?.ignoreKeepAlive; if (!needCache) { continue; } const name = as string; (name); } = cacheMap; // Set collection that stores routing record names},
methodsetTabTitle
usemeta
Attributes, attach the latest title content to the route.
// Set the tag titleasync setTabTitle(title: string, route: RouteLocationNormalized) { const findTab = ((item) => item === route); if (findTab) { = title; // meta implementation Set the title title of each page await (); } },
In the tab component, open the page routing record based on incomingtabItem
, Tag page title name binding calculation attributegetTitle
。
// src\layouts\default\tabs\components\ <template> <Dropdown :dropMenuList="getDropMenuList" :trigger="getTrigger" @menu-event="handleMenuEvent"> <div :class="`${prefixCls}__info`" @contextmenu="handleContext" v-if="getIsTabs"> <span class="ml-1">{{ getTitle }}</span> </div> </Dropdown> </template> props: { tabItem: { type: Object as PropType<RouteLocationNormalized>, default: null, }, isExtra: Boolean, }, // Get title informationconst getTitle = computed(() => { const { tabItem: { meta } = {} } = props; return meta && t( as string); });
The above is the detailed content of Vben Admin multi-tab status management source code learning. For more information about Vben Admin multi-tab status management, please pay attention to my other related articles!