我是F2E世界的新手。 我刚刚使用create-react-app创建了一个Web应用程序。 (https://github.com/facebookincubator/create-react-app)
我想将owl.carousel导入到我的项目中,因此我遵循了NPM(https://www.npmjs.com/package/owl.carousel)的指南,其语法是:
import $ from 'jquery';
import 'imports?jQuery=jquery!owl.carousel';
但调试器控制台指示错误:
Unexpected '!' in 'imports?jQuery=jquery!owl.carousel'. Do not use import syntax to configure webpack loaders import/no-webpack-loader-syntax
我尝试了另一种语法:
import owlCarousel from 'owl.carousel'
,错误是:
Uncaught TypeError: Cannot read property 'fn' of undefined
有人可以帮我弄清楚发生了什么吗?感谢。
更新:我的webpack加载程序设置:
loaders: [
// Process JS with Babel.
{
test: /\.(js|jsx)$/,
include: paths.appSrc,
loader: 'babel-loader',
query: {
cacheDirectory: findCacheDir({
name: 'react-scripts'
})
}
},
{
test: /\.css$/,
loader: 'style!css?importLoaders=1!postcss'
},
{
test: /\.json$/,
loader: 'json'
},
{
test: /\.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/,
loader: 'file',
query: {
name: 'static/media/[name].[hash:8].[ext]'
}
},
{
test: /\.(mp4|webm|wav|mp3|m4a|aac|oga)(\?.*)?$/,
loader: 'url',
query: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]'
}
}
]
我的组件代码:
import React, { Component } from 'react';
import './App.css';
import './css/style.css';
import './css/bootstrap.min.css';
import './css/owl.carousel.css';
import FruitSelector from './containers/fruit_Selector';
import FruitDetail from './containers/fruit_Detail';
import $ from 'jquery';
import 'owl.carousel';
class App extends Component {
render() {
$(document).ready(function(){
$(".content-slider").owlCarousel({
slideSpeed: 350,
singleItem: true,
autoHeight: true,
navigation: true,
navigationText: ["<i class='fa fa-angle-left'></i>", "<i class='fa fa-angle-right'></i>"]
});
});
return (
<div className="App">
<div className="row">
<div className="col-sm-4 col-md-3 sidebar">
<FruitSelector/>
</div>
<div className="col col-md-8">
<FruitDetail/>
</div>
</div>
</div>
);
}
}
export default App;
我的webpack.config.dev.js插件设置:
plugins: [
new InterpolateHtmlPlugin({
PUBLIC_URL: publicUrl
}),
new HtmlWebpackPlugin({
inject: true,
template: paths.appHtml,
}),
new webpack.DefinePlugin(env),
new webpack.HotModuleReplacementPlugin(),
// Watcher doesn't work well if you mistype casing in a path so we use
// a plugin that prints an error when you attempt to do this.
// See https://github.com/facebookincubator/create-react-app/issues/240
new CaseSensitivePathsPlugin(),
// If you require a missing module and then `npm install` it, you still have
// to restart the development server for Webpack to discover it. This plugin
// makes the discovery automatic so you don't have to restart.
// See https://github.com/facebookincubator/create-react-app/issues/186
new WatchMissingNodeModulesPlugin(paths.appNodeModules),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
})]
错误弹出:
App.js:71 Uncaught TypeError: (0 , _jquery2.default)(...).owlCarousel is not a function(…)
答案 0 :(得分:13)
问题在于导入语法不是默认的webpack语法。您已安装在项目https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-webpack-loader-syntax.md中以阻止它,因为它确实是react-create-app的一部分。请删除它以启用此语法。
Owl.carousel 需要在其中导入jQuery
库,因为它使用 $ 变量,因此这是问题,这就是为什么webpack-loader-syntax
必须被删除。
如果我们尝试以标准方式导入owl
,那么jQuery
未定义(webpack中的每个文件都有自己的范围),因此会抛出错误:
Uncaught TypeError: Cannot read property 'fn' of undefined
如果删除插件有问题,那么您可以尝试将jQuery添加到每个模块,并将其用作匀场模块 - https://webpack.github.io/docs/shimming-modules.html。
在webpack配置中,它看起来像:
module.exports = {
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
})
]
//other config vars
};
只需添加:
import 'owl.carousel'
答案 1 :(得分:0)
我遇到了同样的错误“无法读取未定义的属性'fn'”的问题,但是请确保首先使用Jquery引用,然后为我修复它。
<!-- Webjar References -->
<script src="webjars/jquery/3.3.1/jquery.min.js"></script>
<script src="webjars/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="webjars/bootstrap/4.3.1/css/bootstrap.min.css"
rel="stylesheet">
答案 2 :(得分:0)
new webpack.ProvidePlugin({
$: path.resolve(path.join(__dirname, 'node_modules/jquery')),
jQuery: path.resolve(path.join(__dirname, 'node_modules/jquery')),
'window.jQuery': path.resolve(path.join(__dirname, 'node_modules/jquery')),
}),
对我有帮助
因为轮播有其自己的模块(node_modules/owl.carousel/node_modules/jquery
),并且插件从那里获取jQuery并将轮播写入此模块中
答案 3 :(得分:0)
dont import jquery as (import $ from 'jquery') as owl.carousel need jquery, instead in webpack.config file :
const webpack = require("webpack");
.....
----------
module.exports={
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),
]
}
this works perfectly alright.