在Vue应用程序中使metisMenu正常工作

时间:2019-06-23 04:52:32

标签: vuejs2 metismenu

我要使用https://github.com/dpwilhelmsen/sb-admin-2-bootstrap-4中的SB Admin 2模板

在我的Laravel 5.7 / Vuejs 2.6 / Vuex 3.1 / Bootstrap 4.3应用程序中,我为此采取了一些步骤:

因为它基于metismenu在控制台中,我运行以下命令:

npm install --save metismenu
npm i popper

npm audit fix

在resources / js / app.js中,我加入了metismenu:

require('./bootstrap');


//require('popper');
/* If to uncomment line above in console I got lot of errors :
 WARNING in ./node_modules/popper/index.js 235:35-58
Critical dependency: the request of a dependency is an expression
 @ ./resources/js/app.js
 @ multi ./resources/js/app.js ./resources/sass/sb2/app.scss ./resources/sass/sb2/sb-admin-2.scss
 */

require('metismenu');

require('jquery');

window.Vue = require('vue');
...

在在所有vue页面中使用的resources / js / helpers / commonFuncs.js中,我通过方法init初始化菜单:

export function checkAuthorization(store, router) {
    // console.log("-11 checkAuthorization::")


    router.beforeEach((to, from, next) => {
        jQueryInit();

        next();


    ...

    function jQueryInit() {
    // alert( "jQueryInit"+(-55) )

    $(function () {
        // alert( "INSIDE::"+var_dump(-11) )
        $('#side-menu').metisMenu();
        console.log("Inside $('#side-menu')::")
        console.log($('#side-menu'))

    });

//Loads the correct sidebar on window load,
//collapses the sidebar on window resize.
// Sets the min-height of #page-wrapper to window size
    $(function () {
        var setupPage = function () {
            var topOffset = 50;

            console.log("++1 typeof window::")
            console.log(typeof window)
            console.log(window)

            var width = (window.innerWidth > 0) ? window.innerWidth : screen.width;
            if (width < 768) {
                $('div.navbar-collapse').removeClass('show');
                topOffset = 100; // 2-row-menu
            } else {
                $('div.navbar-collapse').addClass('show');
            }

            console.log("++2 typeof window::")
            console.log(typeof window)
            console.log(window)
            var height = ((window.innerHeight > 0) ? window.innerHeight : screen.height) - 1;
            height = height - topOffset;
            if (height < 1) height = 1;
            if (height > topOffset) {
                $("#page-wrapper").css("min-height", (height) + "px");
            }

        };
        // debugger


        $(window).bind("load resize", setupPage);

        var url = window.location;
        var element = $('ul.nav a').filter(function () {
            return this.href == url;
        }).addClass('active').parent();

        while (true) {
            if (element.is('li')) {
                element = element.parent().addClass('in').parent();
            } else {
                break;
            }
        }

        setupPage();
    });
    alert( "AFTER jQueryInit"+(-55) )
}

在控制台上我看不到任何JS错误,并且看起来该窗口是正确的JS对象。

我想这是开始metismenu的最佳位置。

我没有手动附加任何metisMenu.js / css文件,因此将该命令置于上面

require('metismenu');

做到了。

但是我的页面布局已损坏。 您可以在http://hostels-tours.nilov-sergey-demo-apps.tk/admin/dashboard现场观看 请注意控制台输出:https://imgur.com/a/xZ0EVNg

我添加了样式定义:

#wrapper {
  width: 100%;
  background-color: yellow;
  border: 2px dotted red;
}

确保已应用resources / sass / sb2 / sb2 / sb-admin-2.scss中的样式。 有什么问题,可以在vue应用程序metisMenu.js中工作吗?

谢谢!

0 个答案:

没有答案