2017-04-12 66 views
0

我在許多之前的SO問題中都看到過這個錯誤,但是在升級到最新版本4.0.2之後,它們都沒有與這種情況相關。這不是一個循環的直接投資,並且似乎與polifills沒有關係。錯誤:無法解析XXX的所有參數:(?)。 Angular 4.0.2

我正在使用SystemJSjspm(不是webpack),也許jspm是與問題有關,但我不知道如何。

的應用程序入口點是main.ts:

import 'core-js/es7/reflect'; 
import 'zone.js/dist/zone'; 
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 
import { AppModule2 } from './app/app.module2'; 

platformBrowserDynamic().bootstrapModule(AppModule2); 

我的模塊,app.module2.ts,很簡單:

import { NgModule, Component } from '@angular/core'; 
import { HttpModule, Http } from '@angular/http'; 
import { BrowserModule } from '@angular/platform-browser'; 

@Component({ 
    selector: 'app-home', 
    template: `<h2>Hola</h2>`  
}) 
export class HomeComponent { 
    constructor(private http : Http) { 
    } 
} 

@NgModule({ 
    imports: [ 
    BrowserModule, HttpModule 
    ], 
    declarations: [ 
    HomeComponent 
    ], 
    bootstrap: [ HomeComponent ], 
    entryComponents: [ ], 
    providers: [ 
    ] 
}) 
export class AppModule2 { } 

當我啓動的應用程序(使用Chrome 57),錯誤的是:

Error: Can't resolve all parameters for HomeComponent: (?). 
    Evaluating http://localhost:3000/src/main.ts 
    Loading myapp 
    at syntaxError (http://localhost:3000/jspm_packages/npm/@angular/[email protected]/bundles/compiler.umd.js:1513:34) [<root>] 
    at CompileMetadataResolver._getDependenciesMetadata (http://localhost:3000/jspm_packages/npm/@angular/[email protected]/bundles/compiler.umd.js:14363:35) [<root>] 
    at CompileMetadataResolver._getTypeMetadata (http://localhost:3000/jspm_packages/npm/@angular/[email protected]/bundles/compiler.umd.js:14231:26) [<root>] 
    at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (http://localhost:3000/jspm_packages/npm/@angular/[email protected]/bundles/compiler.umd.js:13840:24) [<root>] 
    at CompileMetadataResolver._getEntryComponentMetadata (http://localhost:3000/jspm_packages/npm/@angular/[email protected]/bundles/compiler.umd.js:14484:45) [<root>] 
    at eval (http://localhost:3000/jspm_packages/npm/@angular/[email protected]/bundles/compiler.umd.js:14067:108) [<root>] 
    at Array.map (native) [<root>] 
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:3000/jspm_packages/npm/@angular/[email protected]/bundles/compiler.umd.js:14067:73) [<root>] 
    at JitCompiler._loadModules (http://localhost:3000/jspm_packages/npm/@angular/[email protected]/bundles/compiler.umd.js:25144:64) [<root>] 
    at JitCompiler._compileModuleAndComponents (http://localhost:3000/jspm_packages/npm/@angular/[email protected]/bundles/compiler.umd.js:25103:52) [<root>] 
    at JitCompiler.compileModuleAsync (http://localhost:3000/jspm_packages/npm/@angular/[email protected]/bundles/compiler.umd.js:25065:21) [<root>] 
    at PlatformRef_._bootstrapModuleWithZone (http://localhost:3000/jspm_packages/npm/@angular/[email protected]/bundles/core.umd.js:4793:25) [<root>] 
    at PlatformRef_.bootstrapModule (http://localhost:3000/jspm_packages/npm/@angular/[email protected]/bundles/core.umd.js:4779:21) [<root>] 
    at Object.execute (http://localhost:3000/src/main.ts!transpiled:19:65) [<root>] 

如果刪除的Http注射在HomeComponent噸他應用程序加載成功。

我tsconfig.json

{ 
    "compilerOptions": { 
    "target": "es5", 
    "module": "commonjs", 
    "moduleResolution": "node", 
    "sourceMap": true, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "lib": [ "es2015", "dom" ], 
    "noImplicitAny": true, 
    "suppressImplicitAnyIndexErrors": true 
    } 
} 
+1

這是因爲你缺少@Injectable()裝飾器?來源:https://gist.github.com/gsans/d020de5f0fbe56a22c824e1158ba7ec8#file-mycomponent-ts – floor

+0

@floor問題是與一個角度的服務,我應該在這裏添加'@Injectable()'代碼? – Roberto

+1

嘗試:從'@ angular/common'導入{CommonModule,Injectable};並將@Injectable()放在主組件導出之上。 – floor

回答

0

多次測試之後,這個問題是不是在角代碼,但基於systemjs和熱蒙公司是加載打字稿文件(提供的默認配置的裝載系統。 ts),然後瀏覽器即時編譯成js。可能是transpiler plugin-typescript的問題。

在這個過程中有什麼問題,我不知道究竟是什麼,無論如何,如果我們配置systemjs直接在js中加載模塊,那麼問題就會消失。

我已經刪除所有引用的模塊格式打字稿,所以從原始文件中,配置:

transpiler: "plugin-typescript", 
packages: { 
    "main": { 
     "main": "main.ts", 
     "format": "esm", 
     "defaultExtension": "ts", 
     "meta": { 
     "*.ts": { 
      "loader": "plugin-typescript" 
     } 
     } 
    } 
}, 

它變成:

transpiler: false, 
packages: { 
    'app': { 
     defaultExtension: 'js' 
    }, 
    'main': { 
     main: 'main.js', 
     defaultExtension: 'js' 
    } 
}, 
map: { 
    'main': 'src', 
    'app': 'src/app' 
} 

現在的應用程序加載不問題。

我使用VS Code來編程該應用程序,所以當我編輯.ts文件時,自動生成.js和.map文件,這個轉換過程在開發時完成,除此之外,使用Chrome,I可以使用原始.ts文件進行調試,所以我認爲我不會因這種方法而失去任何東西。

如果有人有更好的方法,告訴我。

相關問題