layuiAdmin侧边栏菜单刷新保持当前页面

layuiadmin框架,每次刷新整个页面,都会回到首页,现在想保持在当前页面,
layuiAdmin侧边栏菜单刷新保持当前页面,利用缓存来处理,供自己参考如下:

   layui.config({
        base: '__ROOT__/homeStatic/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use('index',function (){
        var $ = layui.$
            , admin = layui.admin
            , form = layui.form
            , table = layui.table


        var active = {
            //点击菜单缓存当前页面
            menuEvent(){
                // 刷新
                $('#LAY-system-side-menu li').click(function(){
                    var tabNum = $(this).index();
                    var tabUrl = $(this).find('a').attr('lay-href');
                    var homeTabMenu = {};
                    if(typeof tabUrl=='undefined') {
                        //二级菜单处理,缓存路径及tab的索引
                        $('#LAY-system-side-menu .layui-nav-child').click(function (){
                             let tabUrl = $(this).find('a').attr('lay-href');
                             let tabNum = $(this).index();
                             let homeTabMenu = {};
                            // console.log(tabUrl)
                            // console.log(homeTabNum)
                            // if(typeof tabUrl=='undefined') {
                            //
                            // }
                            homeTabMenu.tabNum = tabNum;
                            homeTabMenu.tabUrl = tabUrl;
                            sessionStorage.setItem("homeTabMenu", JSON.stringify(homeTabMenu));
                        });
                    }else{
                        // console.log(tabUrl)
                        // console.log(homeTabNum)
                        homeTabMenu.tabNum = tabNum;
                        homeTabMenu.tabUrl = tabUrl;
                        sessionStorage.setItem("homeTabMenu",  JSON.stringify(homeTabMenu));
                    }
                })
            },
            //刷新保持页面
            stopPage(){
                // // //刷新保持在当前页面
                var homeTabMenu = sessionStorage.getItem("homeTabMenu");
                homeTabMenu = JSON.parse(homeTabMenu);
                // console.log(homeTabMenu)
                // console.log(homeTabMenu!='' && homeTabMenu.hasOwnProperty('tabUrl'))
                // // 将之前的URL设置为iframe的src
                if (homeTabMenu!=null && homeTabMenu.hasOwnProperty('tabUrl')) {
                    var a = homeTabMenu.tabUrl;
                    $(`#LAY-system-side-menu a[lay-href="${a}"]`).click();
                    $(`#LAY-system-side-menu a[lay-href="${a}"]`).parents('li').addClass('layui-nav-itemed');
                }

            },
            init:function (){
                this.menuEvent();
                this.stopPage();
            }
        }
        active.init();
    });

你可能感兴趣的:(前端,javascript,开发语言)