2

I have app.component like this.

import {Component} from "angular2/core"
import {HTTP_PROVIDERS} from "angular2/http"

import {ChartDataService} from '../service/chartData.service'
import {FilterService} from "../service/filter.service"

@Component({
    selector: 'app',
    template: `
        <sidebar (filterChange)="onFilterChange($event)"></sidebar>
        <div dsChart></div>
    `,
    directives: [SidebarComponent, ChartDirective],
    providers: [HTTP_PROVIDERS, FilterService, ChartDataService],
    styleUrls: ['app/main/app.component.css']
})

export class AppComponent {
     //some more code
}

chartData.service.ts:

import {Injectable} from 'angular2/core'
import {Http, Response, Headers, RequestOptions, RequestMethod} from 'angular2/http'

import {Url} from '../url'
import {FilterModel} from '../model/filter.model'
import {INode} from "../model/node.model"

@Injectable()
export class ChartDataService {
    constructor(private _http: Http) {
        this._headers.append('Content-Type', 'application/json');
    }    

    getData(model?: FilterModel) {
    }
}

Then I try to use ChartDataService inside chart.directive, which is a child component to app.component. chart.directive.ts:

import {Directive, ElementRef, Input, OnInit} from 'angular2/core'
import {ChartDataService} from "../service/chartdata.service"

@Directive({
    selector: '[dsChart]'
})
export class ChartDirective implements OnInit {    
    constructor(
        private el: ElementRef,
        private _dataService: ChartDataService) {
    }

    ngOnInit() {
        this._dataService.getData()
            .then(node => this._render(node))
            .catch(error => { throw error });
    }

    private _render(root: INode) {}
}

But it fails with Via the docs each component has an injector which creates dependencies at a component level scope. If there are no any appropriate component level providers parent component's provider is used. This works fine for classes with @Component() decorator. Adding providers: [ChartDataService] to @Directive declaration helps, but that means each decorator will have separate instance of ChartDataService which is undesiarable.

Any ideas? Or is it by design?

4

2 に答える 2

2

これはインポートの順序に問題がある可能性があります...前にapp.component.tsインポートすると、あなたが言及したエラーが発生する可能性があります:ChartDirectiveChartDataService

ChartDataService のプロバイダーがありません! (ChartDirective -> ChartDataService)

コンポーネント自体をインポートする前に、子コンポーネント/ディレクティブが使用するサービスをインポートする必要があります。

// no error
import {ChartDataService} from '../service/chartData.service'
import {ChartDirective} from '../chart.directive'

// can cause an error
import {ChartDirective} from '../chart.directive'
import {ChartDataService} from '../service/chartData.service'

http://plnkr.co/edit/NoZJnCMlqKkOMm5erfDWでこの問題を再現できませんでしたが、発生する可能性があることを確認できます。

// routes.ts
// no error
export {SettingsRoute} from './routes/settings/settings.route'
export {RootRoute} from './routes/root/root.route'

// this causes error
// export {RootRoute} from './routes/root/root.route'
// export {SettingsRoute} from './routes/settings/settings.route'

// root.route.ts
import {SettingsRoute} from './routes'
@RouteConfig([
  { path: '/settings/...', name: 'Settings', component: SettingsRoute },
])
export class RootRoute {...}

エラー エラーなし

于 2016-03-05T02:03:32.963 に答える
0

beta.7 を使用していたことを忘れていました。beta.8 にアップデートした後、問題はなくなりました。

于 2016-03-04T23:09:26.463 に答える