私は、私が前にやった作業例とここにあなたはそれがどのように動作するかを理解するかもしれないそのようにコードのビットを投稿します:
を
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>
・ホープ本実施例あなただけそれを再度インポートして、コール
役立ちますそれにスーパーです。 – toskv
だから何が問題なの? JavaScriptでクラスを継承する方法を知っていますか?あなたはTypeScriptのOOPで新しいですか? – lilezek
新しいクラスのコンストラクタを全くオーバーライドする必要はありません –