2016-08-18 74 views
4

我使用webpack作爲我的打包程序/加載程序,我可以加載物化css(js/css),但是當我嘗試使用toast時,它說materialize-css未捕獲TypeError:Vel不是函數

Uncaught TypeError: Vel is not a function

我包括在主index.js文件庫:

import 'materialize-css/bin/materialize.css' import 'materialize-css/bin/materialize.js'

有誰知道這可能是爲什麼發生?看着捆綁的來源,物化的js就在那裏。

+0

同樣的問題對我來說......你有沒有找到原因? – vjarysta

回答

0

您必須在您的index.html 分別導入CSS和JS文件,你不能導入index.js

1

我也試圖用兌現,CSS用的WebPack CSS文件,並已還碰上這個問題(儘管不是出於同樣的原因)。物化不是真的用模塊加載器來構建,而是使用全局變量。他們還將依賴關係直接綁定到他們的腳本中,而這種方式在webpack-workflow中可能不需要。

我有一個不完全相同的設置,但我會分享它,希望它會有所幫助,我的webpack +在我創建的文件中實現像這樣的作品;

/** 
* custom-materialize.js 
*/ 

// a scss file where we include the parts I use. 
require('./custom-materialize.scss'); 

/** 
* materialize script includes 
* we don't use all the plugins so no need to 
* include them in our package. 
*/ 
require('materialize-css/js/initial'); 
require('materialize-css/js/jquery.easing.1.3'); 
require('materialize-css/js/animation'); 
// note: we take these from npm instead. 
//require('materialize-css/js/velocity.min'); 
//require('materialize-css/js/hammer.min'); 
//require('materialize-css/js/jquery.hammer'); 
require('materialize-css/js/global'); 
//require('materialize-css/js/collapsible'); 
require('materialize-css/js/dropdown'); 

接着剛剛從NPM npm install velocity-animate 安裝速度,並指出全球Vel兌現使用該程序包中,而不是的WebPack。

new webpack.ProvidePlugin({ 
    '$': 'jquery', 
    'jQuery': 'jquery', 
    'Vel': 'velocity-animate' 
    }), 
0

確保uglifyJsPlugin是這樣的。

new webpack.optimize.UglifyJsPlugin({sourceMap: true, mangle: false}) 

mangle屬性應該是假的,這樣你的源文件的變量名,當您壓縮不會改變。

2

有同樣的問題&想出了較爲簡單的解決方案:需要 只有兩件事情要做:

第一:導入按照你根模塊像app.js

//given you have installed materialize-css with npm/yarn 

import "materialize-css"; 
import 'materialize-css/js/toasts'; 

其次:如果webpack,設置下面的插件或得到velocity.min.js作爲全球變量就像你會使用jQuery:

new webpack.ProvidePlugin({ 
    "$": "jquery", 
    "jQuery': "jquery", 
    "Vel": "materialize-css/js/velocity.min.js" 
    }), 
相關問題