Angular 页面解析器角度应用中的竞争条件
我有一个angular应用程序,需要从外部CMS获取标签数据 我有我所有的路由,有一个PageResourceResolver,在那里我传递我想从CMS加载的组件,将它们作为设置注入到服务中 然后,我将该服务注入到我想要的组件中,并读取这些设置来设置标签 如果按原样实施,我会遇到比赛条件方面的问题吗?我担心组件会加载,并且在某些情况下并非所有的信息都在那里 我使用angular不到一个月,所以这是全新的 这是我的设置服务:Angular 页面解析器角度应用中的竞争条件,angular,rxjs,race-condition,Angular,Rxjs,Race Condition,我有一个angular应用程序,需要从外部CMS获取标签数据 我有我所有的路由,有一个PageResourceResolver,在那里我传递我想从CMS加载的组件,将它们作为设置注入到服务中 然后,我将该服务注入到我想要的组件中,并读取这些设置来设置标签 如果按原样实施,我会遇到比赛条件方面的问题吗?我担心组件会加载,并且在某些情况下并非所有的信息都在那里 我使用angular不到一个月,所以这是全新的 这是我的设置服务: interface IOnBoarding { ContractAn
interface IOnBoarding {
ContractAndZipCodes: any
}
export interface ICMSContentSettings {
cmsConfiguration: ICMSConfiguration | null;
LoginPage: ILoginPage | null;
Footer: any;
}
@Injectable({
providedIn: 'root'
})
export class CMSContentService {
settings: Observable<Readonly<ICMSContentSettings>>;
private settingsSource: BehaviorSubject<ICMSContentSettings> = new BehaviorSubject<
ICMSContentSettings
>({
cmsConfiguration: null,
LoginPage: null,
Footer: null
});
constructor(
private httpClient: HttpClient
) {
this.settings = this.settingsSource.asObservable();
}
setSettings(settings: Partial<ICMSContentSettings>) {
const updatedConfig = { ...this.settingsSource.value, ...settings };
this.settingsSource.next(updatedConfig);
}
getSettings(): Readonly<ICMSContentSettings> {
return this.settingsSource.value;
}
}
@Injectable()
export class PageResourcesResolver implements Resolve<any> {
constructor(
private cmsContentService: CMSContentService,
private cmsService: CMSService
) { }
async resolve(route: ActivatedRouteSnapshot, rstate: RouterStateSnapshot) {
const component: string = route.data.component;
let resources;
if (component && !this.doesCacheDataExists(component)) {
switch (component) {
case 'LoginPage':
resources = await this.cmsService.getLoginPage<ILoginPage>();
break;
case 'Footer':
resources = await this.cmsService.getFooter<IFooter>();
break;
default:
resources = null;
}
this.cmsContentService.setSettings({ [component]: resources });
}
return resources;
}
}
@Component({
selector: 'app-footer',
templateUrl: './footer.component.html',
styleUrls: ['./footer.component.scss']
})
export class FooterComponent implements OnInit {
public footerSettings: IFooter = null;
constructor(
private cmsContentService: CMSContentService
) {
}
ngOnInit() {
this.footerSettings = this.cmsContentService.getSettings().Footer;
}
}