Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👾 BUG | keep-alive 带参数路由缓存bug #350

Open
jy02534655 opened this issue Nov 2, 2021 · 0 comments
Open

👾 BUG | keep-alive 带参数路由缓存bug #350

jy02534655 opened this issue Nov 2, 2021 · 0 comments

Comments

@jy02534655
Copy link

vue2+版本keep-alive如果使用自定义key来缓存视图,关闭视图后缓存是无法被删除干净的
参考资料
可以维护一个keepExclude 数组来处理,大概是这样

watch: {
        // 需实时移除缓存fullPath集合
        keepExclude(v) {
            const me = this;
            // 判断是否空数组,避免死循环
            if (!isEmpty(v)) {
                // 遍历清除缓存
                v.forEach((name) => {
                    // 在前视图的子视图中查找是否存在动态清除缓存的页面
                    const view = find(me.$children, (item) => {
                        return item.$vnode && item.$vnode.key == name;
                    });
                    if (view) {
                        // 如果存在则销毁视图来清除缓存
                        // 需要先从keep-alive缓存中清除数据,避免下次使用时无法缓存
                        const { parent } = view.$vnode,
                            // 缓存组件列表
                            cache = parent.componentInstance.cache,
                            // 缓存组件key列表
                            keys = parent.componentInstance.keys;
                        remove(keys, (data) => {
                            return data == name;
                        });
                        // 销毁缓存
                        delete cache[name];
                        // 销毁组件
                        view.$destroy();
                        // console.log(keys);
                    }
                });
                // 清空需要动态清除缓存的页面集合
                me.keepExcludeClean();
            }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant