我已经看到这个问题在网上被回答了几次。但是,我只是没有让它工作。我希望你能提供帮助。
我正在使用Laravel 5.5和Laravel Mix。我没有手动将我需要的一些js文件加载到特定的目录中,而是想通过npm安装软件包,并在laravel mix中要求它们。
这适用于开箱即用的bootstrap。我将3.3.7版本替换为4.0.0 alpha6。
npm install bootstrap-v4-dev -D
然后在resources/assets/js/bootstrap.js
我将其改为看起来像这样
window._ = require('lodash');
/**
* We'll load jQuery and the Bootstrap jQuery plugin which provides support
* for JavaScript based Bootstrap features such as modals and tabs. This
* code may be modified to fit the specific needs of your application.
*/
try {
window.$ = window.jQuery = require('jquery');
require('bootstrap-v4-dev');
} catch (e) {}
它有效。我可以运行npm run dev
就好了。现在,我想用另一个npm包完成同样的事情。所以我npm install now-ui-kit -D
我的package.json现在看起来像这样
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.16.2",
"bootstrap-v4-dev": "^4.0.0-alpha.6",
"cross-env": "^5.0.1",
"jquery": "^3.1.1",
"laravel-mix": "^1.0",
"lodash": "^4.17.4",
"now-ui-kit": "^1.0.1",
"popper.js": "^1.11.1",
"vue": "^2.1.10"
},
"dependencies": {
"now-ui-kit": "^1.0.1"
}
}
然后在bootstrap.js文件中添加require('now-ui-kit');
,但是现在当我尝试编译时,我收到以下错误:
ERROR in ./resources/assets/js/bootstrap.js
Module not found: Error: Can't resolve 'now-ui-kit' in '/home/vagrant/code/user/resources/assets/js'
@ ./resources/assets/js/bootstrap.js 15:0-21
@ ./resources/assets/js/app.js
@ multi ./resources/assets/js/app.js
我也尝试将require('now-ui-kit');
放在app.js
或甚至webpack.mix.js
中。
没有任何效果,我真的不明白我做错了什么......有人可以帮忙吗?
答案 0 :(得分:0)
运行命令
npm install -D bootstrap@4.0.0-beta.3
然后使用以下代码
try {
window.$ = window.jQuery = require('jquery');
require('bootstrap');
} catch (e) {}
答案 1 :(得分:0)
请按照以下步骤操作:
npm
软件包,然后安装jQuery npm i jquery
。.js
的{{1}}文件现在打开resources/js/package.js
在webpack.mix.js
中添加package.js
webpack.mix.js
将此内容写在mix.js([
'resources/js/package.js',
'resources/js/otherJsFile.js'
], 'js/app.js').extract([
// Extract packages from node_modules to vendor.js
'jquery'
])
中:
resources/js/package.js
最后在import $ from 'jquery';
文件上使用js/vendor.js
,js/app.js
。
现在,您可以在从.blade
导入的网站上使用jQuery