【问题标题】:How to include JQuery from node_modules, except from cdn in Laravel 5.8 app?如何从 node_modules 中包含 JQuery,除了 Laravel 5.8 应用程序中的 cdn?
【发布时间】:2019-10-09 21:26:01
【问题描述】:

我有一个 Laravel 应用程序,我需要包含我的 npm 包中的所有脚本。从数据表和其他工作,我需要 JQuery 工作。我之前从 CDN 实现了 JQuery,现在想从我的节点包中使用它。

我已经使用 npm 安装了它

npm install jquery

但现在我无法使用它们,并且在 chrome-dev-tool 中它显示 $ 为未定义的符号。

资源文件夹中的 bootstrap.js 文件如下,我的 Laravel mix 构建在运行时成功

npm run dev
window._ = require('lodash');

try {
    window.Popper = require('popper.js').default; //dont know working or not
    window.$ = window.jQuery = require('jquery'); // Present earlier

    require('bootstrap'); // working
    require('font-awesome/css/font-awesome.css'); // working
    require('datatables.net-bs4'); // not working

    window.toastr = require('toastr'); // not working
    window.DataTable = require('datatables.net'); // not working as jQuery is not working
} catch (e) {}

window.axios = require('axios'); // present from earlier
... continues

我试过了

include('jquery'),

include('jquery/dist/jquery'),

include('jquery/dist/jquery.js')

但不工作

【问题讨论】:

    标签: jquery laravel npm package


    【解决方案1】:

    尝试在 bootstrap.js 中引入 jquery

    require('jquery);
    

    在你的 app.scss 中试试这个:

    //DataTable
    @import '~datatables.net-bs4/css/dataTables.bootstrap4.min.css';
    

    如果在此之后您的 jQuery 和 DataTable 仍然无法工作,请转到您的 node_modules 目录并检查 jquery 包和您的 datatables 包是否正常。如果没有,运行命令

    npm update
    

    并重新安装您的软件包。

    【讨论】:

    • 什么都试过了。要求,导入,@import 一切。并且包在节点模块文件夹中
    • 如果我在标头中包含 cdn,并尝试删除 require 语句,我的应用程序也会失败。不知道为什么。
    • 你是在布局的底部声明 app.js 吗?
    • 没有。我在我的头标签顶部声明
    • 在所有 DOM 元素加载完成后,尝试在布局的底部紧跟 stack('scripts') 声明,如下所示: @stack('scripts')
    猜你喜欢
    • 1970-01-01
    • 2015-08-02
    • 2017-02-08
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多