2016-08-05 45 views
0

四處錯誤:錯誤需要( 'child_process')沒有定義的函數 - 打字稿

zone.js:260 Uncaught EXCEPTION: Error in ./AppComponent class AppComponent - inline template:8:48 
ORIGINAL EXCEPTION: ReferenceError: require is not defined 
ORIGINAL STACKTRACE: 
ReferenceError: require is not defined 
    at AppComponent.performCmdOperation (http://localhost:8000/app/app.component.js:43:67) 

app.component.ts

export class AppComponent{ 

SelectType = EnumSelectType; 
selectedSection: SelectType = EnumSelectType[EnumSelectType.Home]; 
selectPage(selectType:EnumSelectType) { 
    console.log('Global : ' + EnumSelectType[selectType]); 
    this.selectedSection = EnumSelectType[selectType]; 
    console.log(this.selectedSection + ' selected'); 
} 

performCmdOperation(){ 
    console.log('in perform action'); 
    const exec = require('sdk/system/child_process').exec; //this is the point where I'm getting error. 
    exec('ipconfig', (error, stdout, stderr) => { 
     if (error) { 
      console.error(`exec error: ${error}`); 
      return; 
     } 
     console.log(`stdout: ${stdout}`); 
     console.log(`stderr: ${stderr}`); 
    }); 
} 

請幫我找出這個問題。使用'child_process'是否需要import missing or any dependency

我現在的依賴是

"bootstrap": "^3.3.6", 

"@angular/common": "2.0.0-rc.4", 
"@angular/compiler": "2.0.0-rc.4", 
"@angular/core": "2.0.0-rc.4", 
"@angular/http": "2.0.0-rc.4", 
"@angular/platform-browser": "2.0.0-rc.4", 
"@angular/platform-browser-dynamic": "2.0.0-rc.4", 
"@angular/router": "2.0.0-rc.1", 
"@angular/router-deprecated": "2.0.0-rc.1", 
"@angular/upgrade": "2.0.0-rc.4", 

"systemjs": "0.19.27", 
"es6-shim": "^0.35.0", 
"reflect-metadata": "^0.1.3", 
"rxjs": "5.0.0-beta.6", 
"zone.js": "^0.6.12" 

如果這是它需要修改gulpfile.ts東西。請指導如何在gulpfile中添加「child_process」。

回答

0

嘗試要求child_process代替,例如:

const exec = require('child_process').exec; 

說實話,不知道你想要達到的,因爲它看起來像一個角2項目,它運行在瀏覽器中,但child_process是一個內置的Node.js組件,它可以在服務器環境中運行。

UPDATE:

在咕嘟咕嘟gulpfile的情況下,這裏有一個例子人會如何使用child_process調用外部可執行文件 - 在這種情況下,DOTNET CLI。

var ps = require("child_process"); 
gulp.task("serve:prod", function() { 
    const dotnetPs = ps.exec("dotnet run"); 
    // redirect standard output of the child process to the console. 
    dotnetPs.stdout.on("data", function(data) { console.log(data.toString()); }); 
    dotnetPs.stderr.on("data", function(data) { console.error(data.toString()); }); 
}); 
+0

我正在使用gulp來編譯這一切,所以如果你可以幫助我使用gulp任務函數在應用程序中使用它,這將有助於。 –

+0

同樣在這裏,我爲我的構建使用gulp,並使用'child_process'來調用外部可執行文件。我會用一個例子更新我的答案。不過,您不會在Angular 2組件中使用它。 –

+0

請儘可能使用gulpfile.js任務示例提供示例。謝謝! –