【发布时间】:2021-08-16 13:52:50
【问题描述】:
我正在学习 Angular、DI、Services 和 Ngrx,但我遇到了一个我无法理解的问题
当我记录 this.getBase() 和 this.baseRateSaving 时,我在控制台中得到了值,但是当我尝试将它注入到我的字符串插值中时,我得到了未定义。
我尝试不使用 getbase() 方法,所以使用 this。 baseRateSaving 但我的编辑器告诉我'属性'baseRateSaving' 在其初始化之前使用'
代码如下:
import { BodyCard, HeaderCard, IconDetails } from '@fortuneo/components/src/app/components/card/card.model';
import { CardsOptionsContextService } from './cards-options-context.service';
import { Injectable, OnDestroy } from '@angular/core';
import { combineLatest } from 'rxjs';
import { subscriptionSelector } from '../../../store/subscription/subscription.state';
import { savingsParametersSelector } from '../../../store/parameters/parameters.state';
import { Subscription as RXSubscription } from 'rxjs/internal/Subscription';
import { Store } from '@ngrx/store';
import { AppState } from '../../../store/store.model';
import { Product } from '@fortuneo-models/subscription';
export interface CardOptions {
id: string;
iconDetails: IconDetails;
headerCard: HeaderCard;
bodyCard: BodyCard;
context?: {}
}
@Injectable()
export class CardsOptionsService implements OnDestroy {
products: Product[];
baseRateSaving: number;
private data$: RXSubscription;
constructor(private cardsOptionsContext: CardsOptionsContextService,
private store: Store<AppState>) {
this.data$ = combineLatest([
this.store.select(subscriptionSelector),
this.store.select(savingsParametersSelector)
]).subscribe(([subscription, savingsParameters]) => {
this.products = subscription.products;
this.baseRateSaving = savingsParameters.baseRate;
});
console.log(this.baseRateSaving);
console.log(this.getBase());
}
getBase() {
return this.baseRateSaving;
}
}
...
...
headerCard: {
title: '<h2 class="title">Livret +</h2>',
subTitle: `<p class='subtitle'>Taux de base : ${this.getBase()}% bruts(2)</p>`
},
我真的迷路了
【问题讨论】:
标签: angular service ngrx-store