2017-08-30 10 views
0

Webページのタイトルを取得/設定したい:コンストラクタの型が "Title"のパラメータを持つクラスを拡張する方法 - @ angle/platform-b​​rowser型パラメータ

GetTitle.ts

import { Title }  from '@angular/platform-browser'; 
...... 
export class GetTitle { 

    public constructor(private titleService: Title) { } 

    public setTitle(newTitle: string) { 
     this.titleService.setTitle(newTitle); 
    } 
} 

しかし!新しいサブクラスを作成したい場合は、これを継承しますか?
子のTestGetTitleClassにどのようにコンストラクタを書きますか?
親GetTitleクラスのインスタンスを作成するにはどうすればよいですか?

TestGetTitleClass.ts

import {GetTitle} from "../path.."; 
let getTitleInstance : GetTitle = new GetTitle(??? ??); 

export class TestGetTitleClass extends GetTitle{ 
    constructor(){ 
     super(????); 
    } 
} 
+3

役立ちますそれにスーパーです。 – toskv

+0

だから何が問題なの? JavaScriptでクラスを継承する方法を知っていますか?あなたはTypeScriptのOOPで新しいですか? – lilezek

+0

新しいクラスのコンストラクタを全くオーバーライドする必要はありません –

答えて

2

私は、私が前にやった作業例とここにあなたはそれがどのように動作するかを理解するかもしれないそのようにコードのビットを投稿します:

ComponentViewRightModeクラス:このクラスを使用すると、特定のURL(Routerの依存関係)のApplicationServiceを介して現在のユーザー権利を知ることができます。

export class ComponentViewRightMode { 
    private _routerReference: Router; 
    private _applicationsServiceReference: ApplicationsService; 
    viewRight: string; 

    constructor(
     router: Router, 
     applicationsService: ApplicationsService 
    ) { 
     this._routerReference = router; 
     this._applicationsServiceReference = applicationsService; 
     this.getCurrentUserViewRight(); 
    } 

    getCurrentUserViewRight() { 
     this.viewRight = this._applicationsServiceReference.currentViewReadMode(this._routerReference.url); 
    } 
} 

StockComponentクラス:は、ここに使用される構文です。 super()を使用して依存関係を注入するためにコンポーネントを必要としていることに注意してください。

export class StockComponent extends ComponentViewRightMode implements OnInit { 

    constructor(
     private router: Router, 
     private applicationsService: ApplicationsService, 
     ... 
    ) { 
     super(router, applicationsService); 
    } 
} 

は基本的に、私の必要性は、私のComponentViewRightModeクラスからviewRightプロパティへのアクセス権を持っている多くのコンポーネントのためでした。プロパティはトップクラスのコンストラクタに直接設定されているため、クラスを拡張するすべてのコンポーネントは(サービスのため)非同期にアクセスできます。したがって

、私は予想通り、私のテンプレートを自分の財産を使用することができるよ:

StockComponentテンプレート:

<div class="stock-actions"> 
    >>>>> This is where I use the viewRight property <<<<< 
    <button [disabled]="viewRight !== 'Write'" md-raised-button (click)="navigateTo('someURL')"> 
    {{'STOCK.EQUIPMENT_ADD'|translate}} 
    </button> 
    ... 
</div> 

・ホープ本実施例あなただけそれを再度インポートして、コール

関連する問題