2016-04-13 25 views
0

我第一次嘗試Angular2,並在創建服務時遇到一些問題。其實在使用該服務。我曾經在一個組件創建了以下數據服務Angular2創建和使用服務

import {Injectable} from 'angular2/core'; 
import {recentActivity} from './app/components/recentActivity/model' 

@Injectable() 
export class RecentActivityDataService { 

    loadList() { 
     const items: Array<recentActivity> = []; 
     items.push({ 
      url: 'From Service', 
      name: 'From Service' 
     }); 

     return items; 
    } 

} 

然後,我有以下幾點:

import {Component, OnInit} from 'angular2/core'; 
import {recentActivity} from './model'; 
import {RecentActivityDataService} from './dataService'; 

@Component({ 
    selector: 'recentActivity', 
    templateUrl: './app/components/recentActivity/recentActivity.html', 
    providers: [RecentActivityDataService] 
}) 
export class RecentActivity implements OnInit { 

    items: Array<recentActivity> = []; 

    constructor(private dataService: RecentActivityDataService) { 

    } 

    ngOnInit() { 
     this.items = this.dataService.loadList(); 
    } 

} 

當我拉這個組件我收到以下錯誤消息

「未捕獲(承諾):無法解析 'RecentActivity'(?)的所有參數。確保所有參數均使用Inject修飾 或具有有效類型註釋,並且那'RecentActivity' 裝飾着Injectable。「

我不知道我在這裏失蹤,因爲我的服務有它的@Injectable並在我的組件我把它列爲供應商。但在構造函數中,它仍然沒有說它不能解決這個問題。

在app.Component我有以下

import {Component} from 'angular2/core'; 
import {StandardNavigation} from './navigation/standard'; 
import {RecentActivity} from './components/recentActivity/recentActivity'; 

@Component({ 
    selector: 'cranalytics', 
    templateUrl: './app/main.html', 
    directives: [StandardNavigation, RecentActivity] 
}) 
export class AppComponent { 

} 

我曾嘗試以下變化。在我的主要我bootstrapping AppComponent: bootstrap(AppComponent);

在AppComponent我已經把服務作爲提供有

import {Component} from 'angular2/core'; 
import {StandardNavigation} from './navigation/standard'; 
import {RecentActivity} from './components/recentActivity/recentActivity'; 
import {RecentActivityDataService} from './components/recentActivity/dataService'; 

@Component({ 
    selector: 'cranalytics', 
    templateUrl: './app/main.html', 
    directives: [StandardNavigation, RecentActivity], 
    providers: [RecentActivityDataService] 
}) 
export class AppComponent { 

} 

在最近的活動我已導入數據服務

import {Component, OnInit, Inject, forwardRef} from 'angular2/core'; 
import {recentActivity} from './model'; 
import {RecentActivityDataService} from './dataService'; 

@Component({ 
    selector: 'recentActivity', 
    templateUrl: './app/components/recentActivity/recentActivity.html', 
}) 
export class RecentActivity implements OnInit { 

    items: Array<recentActivity> = []; 

    constructor( private dataService: RecentActivityDataService) { 

    } 

    ngOnInit() { 
     this.items = this.dataService.loadList(); 
    } 

} 

但是,這給了我同樣的錯誤消息

+0

沒有課程申請'RecentActivity'。錯誤來自其他地方。你在哪裏有一個'RecentActivity'類型的構造函數參數? –

+0

如果您啓動服務而不是在組件上提供服務,會發生什麼情況? – Harangue

+0

查看更新的消息 –

回答

2

嘗試將recentActivity依賴關係放在bootstrap()中,以便所有服務都可以在任何地方無需導入就可以訪問它。

+1

即使它們在'bootstrap()'中列出,如果使用類型,仍然需要導入這些類。導入和提供大多是不相關的概念。前者是在DI中註冊類型,後者是在靜態代碼檢查器(linter)已知的代碼中使用的類型,並將它們用作DI的鍵,這仍然需要導入它們。 –