2016-09-14 31 views
1

我試圖在我的測試應用程序中使用jQuery驗證插件webpack,當我在我的文件中import我得到這個錯誤$(...).validate is not a function

我知道jquery-validation基於IIFE和要求jquery在全球範圍內,我已經暴露我的配置。

這裏自我:

{ 
    entry: { 
     app: "./assets/js/app.jsx" 
    }, 
    output: { 
     path: path.join(__dirname, "/build/js"), 
     publicPath: "/build/js", 
     filename: "[name].bundle.js" 
    }, 
    resolve: { 
     modulesDirectories: ["node_modules", "bower_components"], 
     alias: { 
      "jquery": "jquery/dist/jquery.min", 
      "jquery.validate": "jquery-validation/dist/jquery.validate" 
     }, 
     extensions: ["", ".js", ".jsx", ".es6"] 
    }, 
    amd: { 
     jQuery: true 
    }, 
    module: { 
     loaders: [ 
      { 
       test: /\.jsx?$/, 
       exclude: /node_modules/, 
       loader: "babel" 
      }, 
      { 
       test: /jquery.validate/, 
       include: /node_modules/, 
       loader: "imports?jQuery=jquery" 
      } 
     ] 
    }, 
    plugins: [ 
     new webpack.ProvidePlugin({ 
      "$": "jquery", 
      "jQuery": "jquery" 
     }) 
    ], 
    devtool: "source-map" 
} 

,並用它喜歡:

import "jquery.validate" 

$("#test").validate(); 

附:看起來像jQuery驗證插件試圖使用自己的jQuery實例,我想通過比較我的文件[jquery: "3.1.0"]中的$.fn和驗證插件[jquery: "2.2.4"]的內部IIFE,但如何塞住它來使用我的版本?

任何幫助,將不勝感激。

+0

你jqury.js文件之前或jquery.js和文件後添加jqury.validate.js文件? –

+0

'在新的webpack.ProvidePlugin({})'實例中提供了作爲全局webpack配置的jquery',所以它已經有 – Smile0ff

回答

0
resolve: { 
     modulesDirectories: ["node_modules", "bower_components"], 
     alias: { 
"jquery.validate": "jquery-validation/dist/jquery.validate",   
"jquery": "jquery/dist/jquery.min" 

     }, 
     extensions: ["", ".js", ".jsx", ".es6"] 
    }, 

只是改變文件的位置。

嘗試這種解決方案,我希望它爲你的作品

+0

感謝您的回答,但不幸的是它沒有辦法。 – Smile0ff

+0

好的,沒問題。它在許多應用程序中適用於我。 –

相關問題