const index = state.cachedViews.indexOf(view.name); if (index > -1) { state.cachedViews = state.cachedViews.slice(index, index + 1); }
时间: 2024-04-07 19:29:37 浏览: 14
这段代码的作用是从 `state.cachedViews` 数组中找出第一个等于 `view.name` 的元素的下标 `index`。如果找到了这个元素,则将 `state.cachedViews` 数组的第 `index` 个元素删除,并且保留其它元素;如果没有找到这个元素,那么不需要进行任何操作。需要注意的是,`slice` 方法返回的是一个新的数组,这里只取了一个元素,实际上相当于删除了 `state.cachedViews` 数组中的第 `index` 个元素。
相关问题
DEL_OTHERS_CACHED_VIEWS: (state, view) => { const index = state.cachedViews.indexOf(view.name); if (index > -1) { state.cachedViews = state.cachedViews.slice(index, index + 1); } else { // if index = -1, there is no cached tags state.cachedViews = []; } },
这也是一个 Vuex store 中的 mutation,用于删除除了当前路由之外的其他缓存路由记录。具体来说,它会从 `state.cachedViews` 数组中找出当前路由对应的路由名 `view.name`,然后将这个路由名的缓存记录从数组中删除。如果找不到对应的路由名,则将 `state.cachedViews` 数组清空。这个 mutation 可以用于实现动态添加和删除缓存路由的功能,以控制页面缓存的数量和内容。
const getters = { sidebar: state => state.app.sidebar, size: state => state.app.size, device: state => state.app.device, dict: state => state.dict.dict, visitedViews: state => state.tagsView.visitedViews, cachedViews: state => state.tagsView.cachedViews, token: state => state.user.token, avatar: state => state.user.avatar, name: state => state.user.name, introduction: state => state.user.introduction, roles: state => state.user.roles, permissions: state => state.user.permissions, permission_routes: state => state.permission.routes, topbarRouters:state => state.permission.topbarRouters, defaultRoutes:state => state.permission.defaultRoutes, sidebarRouters:state => state.permission.sidebarRouters, } export default getters
这段代码是一个 Vuex 中的 getter,用于从 state 中获取数据。getter 可以被认为是 store 的计算属性。它们的用途是派生一些状态,用于计算和返回数据,而不是直接在 state 中存储。这有助于保持状态的一致性和可维护性。
这个 getter 中定义了一系列属性的获取方法,包括 `sidebar`、`size`、`device`、`dict`、`visitedViews`、`cachedViews`、`token`、`avatar`、`name`、`introduction`、`roles`、`permissions`、`permission_routes`、`topbarRouters`、`defaultRoutes`、`sidebarRouters`。这些属性都对应着 state 中的不同字段。以 `sidebar` 为例,它返回 `state.app.sidebar`,也就是 app 模块下的 sidebar 字段的值。其他属性的获取方法类似。
最后,这个 getter 通过 `export default` 导出,以便在其他组件或模块中使用。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)