【问题标题】:Wow.js with webpack and reactWow.js 与 webpack 并做出反应
【发布时间】:2016-01-30 07:55:14
【问题描述】:

我尝试使用 react 和 webpack 来实现 wow.js。

我通过 npm 安装它。

npm install --save wow.js

安装完美。现在的问题是如何正确导入它。我无法让它继续工作,变得不确定。

我尝试了几种方法:

第一:

import wow from 'wow.js';

但是 webpack 无法编译。它说找不到模块。即使我使用完整的网址import wow from /node_modules/wow.js


第二:

我正在使用来自here的这个解决方案:

require('imports?this=>window!js/wow.js');

但我仍然找不到模块(我将路径更改为我的 node_modules)。


第三:

来自here

我正在使用暴露模块并尝试new WOW().init(); 它显示Wow is undefined

我没有使用他的第一个解决方案,因为我希望我的 html 看起来很简单,只有 bundle.js 脚本。


我找不到任何其他解决方案。我应该怎么做才能让它工作?

谢谢!


我的 webpack.config.js:

module.exports = {
    entry: [
        'webpack-dev-server/client?http://localhost:8080',
        'bootstrap-loader',
        'webpack/hot/only-dev-server',
        './src/js/index.js'
    ],
    output: {
        path: __dirname + "/build",
        publicPath: "/build/",
        filename: 'bundle.js'
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery"
        })
    ],
    module: {
        loaders: [
            {
                exclude: /node_modules/,
                loader: 'react-hot!babel'
            },
            {
                test: /\.css$/,
                loader: 'style!css!postcss'
            },
            {
                test: /\.scss$/,
                loader: 'style!css!postcss!sass'
            },
            { test: /\.(woff2?|ttf|eot|svg|otf)$/, loader: 'url?limit=10000' },
            {
              test: 'path/to/your/module/wow.min.js',
              loader: "expose?WOW"
            }
        ]
    },
    resolve: {
        extensions: ['', '.js', '.jsx']
    },
    devServer: {
        historyApiFallback: true,
        contentBase: './'
    },
    postcss: [autoprefixer]
};

【问题讨论】:

标签: javascript reactjs webpack


【解决方案1】:

无需更新您的 wepack 配置的替代选项。

  1. 安装WOW.js:npm install wowjs
  2. 在您的组件中导入:import WOW from 'wowjs';
  3. 在您的组件的componentDidMount() 下:new WOW.WOW().init();

    import React, {Component} from 'react';
    import WOW from 'wowjs';
    
    class Cmp extends Component {
      componentDidMount() {
        new WOW.WOW().init();
      }
    
      render() {
        /* code */
      }
    }
    
    export default Cmp;
    

【讨论】:

  • 这也不起作用。我使用了new WOW.WOW().init(),但它仍然不起作用。我可以在渲染的 html 中看到确实添加了动画类,但是动画没有运行。
  • 这对我很有用!按照你的指示,瞧!
  • 这些错误怎么办:MutationObserver is not supported by your browser. & WOW.js cannot detect dom mutations, please call .sync() after loading new content. 有人知道如何摆脱它们吗?
  • 从'wowjs'导入{WOW}; not working in version 1.1.3 有一些关于弱图的奇怪错误
【解决方案2】:

执行以下步骤

  1. 安装exports-loader

    npm i exports-loader --save-dev

  2. 添加到webpack.config.js这个加载器

    {
       test: require.resolve('wow.js/dist/wow.js'), 
       loader: 'exports?this.WOW'
    }
    
  3. import 添加到您的应用中

    import WOW from 'wow.js/dist/wow.js';

【讨论】:

【解决方案3】:

您还可以将.call(this) 更改为.call(window)(wow.js 最后一行)。在这里找到了这个解决方案https://foundation.zurb.com/forum/posts/46574-how-to-add-js-library-from-bower

【讨论】:

    猜你喜欢
    • 2018-06-16
    • 2017-06-08
    • 1970-01-01
    • 2015-07-22
    • 2018-03-28
    • 2021-04-15
    • 2021-03-29
    • 2017-01-17
    相关资源
    最近更新 更多