Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Angular 2让它等待服务初始化大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个管道解析消息密钥并相应地返回消息到区域设置.

但我想要有角度等待,直到我的服务将完成它的加载消息,并且只有在继续呈现主页面之后.我的本地化管道使用的地方.
如何让它工作?

服务:

@Injectable()
export class I18nservice implements OnInit {

    private cachedmessages: {String: String};

    activeLocale:I18Enum = null;

    constructor(private http: http) {
        this.reloadLocale(I18Enum.ru);
    }

    ngOnInit(): void {
        this.reloadLocale(I18Enum.ru);
    }

    getmessage(key: String) {
        if (this.cachedmessages) {
            return this.cachedmessages[key];
        }
    }

    reloadLocale(locale: I18Enum) {
        this.activeLocale = locale;
        this.http.get(basePath + i18nPath + I18Enum[locale]).subscribe(
            res => {
                this.cachedmessages = res.json();
            }
        );
    }
}

和管道:

@Pipe({name: 'i18nPipe'})
export class I18nPipe implements PipeTransform {

    constructor(private i18service: I18nservicE) {
    }

    transform(value: any,args: any): any {
        return this.i18service.getmessage(value);
    }

}

解决方法

好.最后我做到了.我找到了这个答案,它帮助了我.
https://stackoverflow.com/a/40379803/5203127

所以我的代码现在看起来如下:

服务:(添加一个initmessages方法)

@Injectable()
export class I18nservice {

    private cachedmessages: {String: String} = null;

    activeLocale: I18Enum = null;

    constructor(private http: http) {

    }

    getmessage(key: String) {
        if (this.cachedmessages) {
            return this.cachedmessages[key];
        }
    }

    reloadLocale(locale: I18Enum) {
        this.activeLocale = locale;
        this.http.get(basePath + i18nPath + I18Enum[locale]).subscribe(
            res => {
                this.cachedmessages = res.json();
            }
        );
    }

    initmessages(locale: I18Enum) {
        this.activeLocale = locale;

        return new Promise((resolve,reject) => {
            this.http.get(basePath + i18nPath + I18Enum[locale]).map(res => res.json()).catch((error: any): any => {
                reject(false);
                return Observable.throw(error.json().error || 'Server error');
            }).subscribe((callResult: {String: String}) => {
                this.cachedmessages = callResult;
                resolve(true);
            });

        });
    }
}

我的bootstrap :(添加了APP_INITIALIZER提供者)

@NgModule({
        imports: [...,httpR_756_11845@odule,...],declarations: [...,I18nPipe
        ],bootstrap: [AppComponent],providers: [...,I18nservice,{
                provide: APP_INITIALIZER,// useFactory: (i18Nservice: I18nservicE) => () => i18Nservice.initmessages(I18Enum.ru),// or
            useFactory: initApp,deps: [I18nservice],multi: true
        }
    ]
})
export class TopJavaModule {

}

export function initApp(i18nservice: I18nservicE){
    // Do iniTing of services that is required before app loads
    // NOTE: this factory needs to return a function (that then returns a promisE)
    return () => i18nservice.initmessages(I18Enum.ru);  // + any other services...
}

大佬总结

以上是大佬教程为你收集整理的Angular 2让它等待服务初始化全部内容,希望文章能够帮你解决Angular 2让它等待服务初始化所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。