Import a library/dependency with Laravel Mix

空扰寡人 提交于 2021-02-11 13:43:30

问题


I have a small problem that took some time to find a solution. I'm trying to import this library to my project in Laravel.

https://www.adchsm.com/slidebars/help/usage/initializing-slidebars/

I have installed the library with NPM.

npm install slidebars --save-dev

Then I'm trying to import this library to my app.js file which has the following structure:

import jquery from 'jquery';
import popper from "popper.js";

try {
    window.$ = window.jQuery = jquery;
    window.Popper = popper.default;

    require('bootstrap');
    require('slidebars');

} catch (exception) {
    console.log(exception);
}

$(document).ready(function () {
    let constructor = new slidebars();
});

run npm run watch but then in my browser I get the following error in the console:

ReferenceError: slidebars is not defined

Please, if you could help me, I have searched in different places, but I can not find a solution for it. Thank you very much in advance.


回答1:


Run...

npm install slidebars

+ slidebars@2.0.2
added 1 package and audited 16905 packages in 9.487s
found 0 vulnerabilities

In /resources/js/app.js add...

window._ = require('lodash');

try {
    window.Popper = require('popper.js').default;
    window.$ = window.jQuery = require('jquery');
    window.slidebars = require('slidebars');

    require('bootstrap');
} catch (e) {}

Then compile the assets:

npm run prod


来源:https://stackoverflow.com/questions/55696234/import-a-library-dependency-with-laravel-mix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!