javascript - TypeError : element. summernote 不是函数

标签 javascript jquery angularjs webpack summernote

我正在从事基于 Angular、Webpack 等的项目。当我想使用 angular-summernote 时,我遇到了一个非常有趣的问题。

它不起作用,并给我一个错误“TypeError: element.summernote is not a function”。如果在没有任何构建工具的情况下将库作为脚本标签添加,它可以正常工作,但使用 Webpack 则不行。

那么,让我们看看文件和配置

webpack.config.js

let webpack = require('webpack');
let path = require('path');

module.exports = {

    entry: {
        bundle: [
            './app/app.js'
        ]
    },
    output: {
        path: path.join(__dirname, './public'),
        filename: '[name].js',
    },
    resolve: {
        root: __dirname,
        extensions: ['', '.js'],
        modulesDirectories: [
            'node_modules',
            'app'
        ],
    },
    externals: {

    },
    plugins: [
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery'
        })
    ],
    module: {
        loaders: [
            {
                loader: 'babel-loader',
                query: {
                    presets: ['es2015']
                },
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/
            }
        ]
    }

};

app/app.js

// Load libs
import angular from 'angular';
import ngRoute from 'angular-route';
import summernote from 'summernote';
import ngSummernote from 'angular-summernote/dist/angular-summernote.js';

// App
angular.module('app', [ngRoute, 'summernote'])

.config(function($routeProvider) {

    // Router
    $routeProvider

        .when('/', {
            template: '<summernote></summernote>'
        });
});

我找到了 github issue , 但它仍然处于打开状态。

我做了一些调试,检查了“summernote”指令的“link”方法,并将“element”记录到控制台,它的原型(prototype)对象不包含任何 jquery 方法,包括“summernote”方法。 <强>为什么?以及如何让它发挥作用?

最佳答案

看起来 jquery 是按需使用的,而不是在其他脚本之前加载的,所以它没有公开全局变量 jQuery,也没有为元素附加适当的方法。

下面的配置解决了这个问题

webpack.config.js

let webpack = require('webpack');
let path = require('path');

module.exports = {

    entry: {
        bundle: [
            // We tell webpack to add jquery as script tag before app script
            // It will expose global variable jQuery and init jquery methods
            'script!jquery/dist/jquery.min.js',

            './app/app.js'
        ]
    },
    output: {
        path: path.join(__dirname, './public'),
        filename: '[name].js',
    },
    resolve: {
        root: __dirname,
        extensions: ['', '.js'],
        modulesDirectories: [
            'node_modules',
            'app'
        ],
    },
    externals: {
        // This mean that require('jquery') will refer to global var jQuery
        'jquery': 'jQuery' 
    },
    plugins: [
        // ProvidePlugin helps to recognize $ and jQuery words in code
        // And replace it with require('jquery')
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery'
        })
    ],
    module: {
        loaders: [
            {
                loader: 'babel-loader',
                query: {
                    presets: ['es2015']
                },
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/
            }
        ]
    }

};

希望阐明 webpack 的工作原理对您有所帮助。如果我遗漏了什么,请分享您的想法和评论。

关于javascript - TypeError : element. summernote 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41852849/

相关文章:

c# - 启用/禁用按钮

javascript - 使用 Javascript 以文本形式对货币值执行算术运算

jQuery:如何检查日期输入字段是否为空

javascript - AngularJS $http 设置 header

javascript - Jquery Accordion 在单击时滑回?

javascript - 从后端到前端的日期时间值

javascript - 下拉值更改时更新 html 表

javascript - ng 如果字符串包含 Angular

javascript - 在AngularJS中禁用严格的上下文转义($ sce)不适用于流音频

javascript - 我没有得到我想要的结果