2016-10-07 19 views
0

任何人都能夠與REDXREDX thunk建立一個應用程序?在我的情況下,它與ionic serve一起工作,但與npm run build一起失敗。我無法爲設備構建,它只能在瀏覽器中運行。Ionic2 RC0和REDX/REDEX-THINK - 創建REDO商店與AoT

我得到這些錯誤NGC

[19:25:46] ngc: Error: Error encountered resolving symbol values statically. Calling function 'createStore', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol AppModule in c:/Ionic/ionic-redux-test/.tmp/app/app.module.ts, resolving symbol AppModule in c:/Ionic/ionic-redux-test/.tmp/app/app.module.ts at simplifyInContext (c:\Ionic\ionic-redux-test\node_modules\@angular\compiler-cli\src\static_reflector.js:469:23) at StaticReflector.simplify (c:\Ionic\ionic-redux-test\node_modules\@angular\compiler-cli\src\static_reflector.js:472:22) at StaticReflector.annotations (c:\Ionic\ionic-redux-test\node_modules\@angular\compiler-cli\src\static_reflector.js:61:36) at _loop_1 (c:\Ionic\ionic-redux-test\node_modules\@angular\compiler-cli\src\codegen.js:53:54) at CodeGenerator.readFileMetadata (c:\Ionic\ionic-redux-test\node_modules\@angular\compiler-cli\src\codegen.js:66:13) at c:\Ionic\ionic-redux-test\node_modules\@angular\compiler-cli\src\codegen.js:100:74 at Array.map (native) at CodeGenerator.codegen (c:\Ionic\ionic-redux-test\node_modules\@angular\compiler-cli\src\codegen.js:100:35) at codegen (c:\Ionic\ionic-redux-test\node_modules\@angular\compiler-cli\src\main.js:7:81) at Object.main (c:\Ionic\ionic-redux-test\node_modules\@angular\tsc-wrapped\src\main.js:30:16)

[19:25:46] ngc: Compilation failed

[19:25:46] ngc failed: NGC encountered an error [19:25:46] Error: NGC encountered an error at ChildProcess. (c:\Ionic\ionic-redux-test\node_modules\@ionic\app-scripts\dist\ngc.js:62:24) at emitTwo (events.js:87:13) at ChildProcess.emit (events.js:172:7) at ChildProcess.cp.emit (c:\Ionic\ionic-redux-test\node_modules\cross-spawn\lib\enoent.js:40:29) at maybeClose (internal/child_process.js:821:16) at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5) Error running ionic app script "build": Error: NGC encountered an error

這些都是我在app.module.ts所做的修改只是@NgModule

//Redux - ReduxThunk - rootReducer 
import { createStore, applyMiddleware } from 'redux'; 
import ReduxThunk from 'redux-thunk'; 
import { rootReducer } from '../modules/rootReducer'; 

const appStore = createStore(
    rootReducer, 
    applyMiddleware(ReduxThunk) 
); 

前和添加此提供者陣列:

providers: [ 
    { provide: 'AppStore', useValue: appStore } 
] 

編輯:在app.module.ts和轉彎常量的AppStore導出功能

export function appStore(): any { 
    return createStore(
      rootReducer, 
      applyMiddleware(ReduxThunk) 
     ); 
}; 

我能夠編譯並運行該項目,但然後讓在home.ts此錯誤

this.appStore.getState is not a function TypeError:

this.appStore.getState is not a function

這是我在home.ts

export class HomePage { 
    constructor(
     @Inject('AppStore') public appStore: any, 
    ) { } 

testRedux(){ 
    this.appStore.dispatch((dispatch)=> { 
     dispatch({type: 'INCREMENT'});}); 
    console.log(this.appStore.getState().increment.counter); 
    } 
} 

任何如何解決這個問題的想法?

回答

1

我能夠在以下改變來解決它app.module.ts

// const appStore = createStore(
// rootReducer, 
// applyMiddleware(ReduxThunk) 
//); 

export function appStore(): any { 
    return createStore(
      rootReducer, 
      applyMiddleware(ReduxThunk) 
     ); 
}; 

編輯:由供應商陣列改變useValueuseFactory。它會解決第二個問題。

providers: [ 
    { provide: 'AppStore', useFactory: appStore } 
    ] 
+0

很確定const是es6,ngc轉譯器是es6,這就是爲什麼你有這個解決方法 – misha130