2017-05-12 50 views
0

我試圖覆蓋另一個反應倉庫,並且遇到問題。使用=和=>符號覆蓋函數的反應問題

第1期) 我要重寫他們的方法

selectItem = (item, clickType, e) => { 
... 
} 

然而,項目不能與我試圖重寫上面的語法編譯代碼。

第2期)是什麼上述和使用之間的區別:

selectItem(item, clickType, e){ 
... 
} 

如果選擇信息(項目,點擊類型,E)覆蓋,而無需使用 「=」 和「=> 符號,我也應該是好的去,但它看起來不像我的代碼(如果我在一個覆蓋構造函數中設置「調試器」,所以我已經知道我正在實例化正確的類)。這種印象,無論哪種方式來覆蓋功能將沒事的。

我在想第1個問題涉及到webpack.config問題。我使用的是:

loaders: [ 
    { 
     test: /.jsx?$/, 
     loader: 'babel-loader', 
     exclude: /node_modules/, 
     query: { 
      presets: ['es2015', 'react'] 
     } 
    }, 
    { test: /\.css$/, loader: 'style-loader!css-loader' }, 
] 

,我期待在回購使用:

loaders: [ 
    { 
    test: /\.css$/, 
    loader: ExtractTextPlugin.extract('style-loader', 'css-loader') 
    }, 
    { 
    test: /\.scss$/, 
    loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass-loader') 
    }, 
    { 
    test: /\.(js|jsx)$/, 
    loaders: ['babel'], 
    exclude: /node_modules/ 
    } 
] 

他們使用ExtractTextPlugin,但不應該的問題,因爲它不是.js文件/ .jsx , 對?

我不明白問題2足夠了解從哪裏開始。

+0

信用爲Shubham誰的反應讓我答案: http://stackoverflow.com/questions/42063854/arrow-function-syntax-not-working-with-webpack – user2589339

回答

1

那是一個箭頭函數的語法,是ES6擴展的一部分,

安裝以下

npm intall -S babel-preset-state-0 babel-plugin-transform-decorators-legacy babel-plugin-react-html-attrs 

和像

loaders: [ 
    { 
     test: /.jsx?$/, 
     loader: 'babel-loader', 
     exclude: /node_modules/, 
     query: { 
      presets: ['es2015', 'react', 'stage-0'], 
      plugins: ['react-html-attrs', 'transform-decorators-legacy'] 
     } 
    }, 
    { test: /\.css$/, loader: 'style-loader!css-loader' }, 
] 

transform-decorators-legacy插件配置您的WebPack會給你的箭支持功能。

您的其他存儲庫可能有.babelrc文件與webpack-config一起使用。

+0

注意安靜,但你讓我正確的答案: http://stackoverflow.com/questions/42063854/arrow-function-syntax-not-working-with-webpack。謝謝! – user2589339

1

自從Shubham已經解決了babel之後,我將只回答符號問題。

的差異

之間
function foo(bar) { 
    console.log(bar) 
} 

const foo = (bar) => console.log(bar) 

是,第一個你定義一個名爲函數foo。第二個你定義了一個foo var,它有一個函數作爲它的值。你還可做:

var foo = function(bar) { console.log(bar) } 

箭頭函數語法()=>{}function(){}之間的主要區別在於箭頭的功能不會將該綁定到自己的範圍。

如要進一步瞭解,請訪問:MDN Arrow Function