无法在不纯管道中读取未定义的属性“订阅”

问题描述

我正在尝试创建不纯净的语言管道,但出现错误 Cannot read property 'subscribe' of undefined当我打电话时 this.languageService.translate(key) in languagePipe。

语言管道:

import { Pipe,PipeTransform } from '@angular/core';
import { LanguageService } from './language.service';

@Pipe({
    name:'lang',pure: false
})
export class LanguagePipe implements PipeTransform {


    private inCycle;
    private value = null;

    constructor(
        private languageService: LanguageService) { }

    transform = (key: string): string => {
        
        if (!key || typeof key == "undefined") {
            return "";
        }
        
        if (this.value && this.inCycle == this.languageService.getLang()) {
            return this.value;
        }
        
        if(this.inCycle != this.languageService.getLang()) {
            this.inCycle = this.languageService.getLang();

            this.languageService.translate(key)
                .subscribe((response: string) => {
                    this.value = response;
                }
            );
        }
    }
}

正在使用

public translate = (key: string): Observable<string> => {
        
        // Drop back an empty string if key is invalid
        if (key == "" || key == "null" || typeof key == "undefined" || key == "undefined") {
            return new Observable((observer) => {
                observer.next('');
            }); 
        }

        //If the language have not exist yet,create the empty object
        if (!this.cache.getCache("language")) {
            this.cache.setCache("language",{});
        }

        // Get text from cache
        let cachedValue = this.cache.getCacheRecursive("language",this._currentLang + "/data/" + key); 
        if (cachedValue) {
            console.log('returning from translate2')
            return new Observable((observer) => {
                observer.next(cachedValue);
            });
        }

        // Call server for page texts if page not exists yet and call not started yet
        let page = this.getPageId();

        if (!this.isPageExists(page) && !this.langPackCallInProgress) {
            let value = '';
            return this.getLangPack(page,key)
                .pipe(map((response: string) => {
                    value = response;
                    return value;
                }
            ));
        }


        // Call server for only one text if page exists and key is missing
        //return this.translateKey(key);
    }



    // getting language pack for whole page
    private getLangPack(page,key): Observable<string> {
        let url = this.requestPaths[1] + "?page=" + page + "&lang=" + this._currentLang;
        let isCommonRequested = false;
        let value: string;
        if (!this.isPageExists("common")) {
            url += "&needCommon=true";
            isCommonRequested = true;
        }

        if (!this.isPageExists(page)) {
            this.langPackCallInProgress = true;
            return this.httpClientService.postData(url,'')
                .pipe(map((response: MessageResource[]) => {

                    //Put texts to the cache
                    for(let i = 0; i < response.length; i++) {
                        this.cache.setCacheRecursive("language",this._currentLang + "/data/" + response[i].resourceKey,response[i].value);   
                    }

                    this.setPageExists(page);
                    if (isCommonRequested) {
                        this.setPageExists("common");
                    }
                    for(let i = 0; i < response.length; i++) {
                        if(response[i]["resourceKey"] === key) {
                            value = response[i]["value"];
                        }
                    }
                    this.langPackCallInProgress = false;
                    return value;
                }

            ));
        }
    }

错误

TypeError: Cannot read property 'subscribe' of undefined
    at LanguagePipe.transform (language.pipe.ts:38)
    at Object.eval [as updateRenderer] (LoginButtonComponent.html:5)
    at Object.debugUpdateRenderer [as updateRenderer] (core.js:45293)
    at checkAndUpdateView (core.js:44276)
    at callViewAction (core.js:44636)
    at execEmbeddedViewsAction (core.js:44593)
    at checkAndUpdateView (core.js:44271)
    at callViewAction (core.js:44636)
    at execComponentViewsAction (core.js:44564)
    at checkAndUpdateView (core.js:44277)

在此先感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)