角型6上不存在“订阅”

时间:2018-09-18 13:21:02

标签: angular

我目前正在将4号角代码升级为6号角代码。我收到错误消息,指出类型'(o:NavigationEnd)=>可观察| UnaryFunction,Observable <[{},字符串,...'

它是一个语法错误,不明白我为什么得到它

现有代码

public static DateTime ToTimeZoneTime(this DateTime time, string timeZoneId = "Taipei Standard Time")
        {
            TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
            return time.ToTimeZoneTime(tzi);
        }

修改后的代码

@Injectable()
export class TitleService {
    constructor(private _config: ConfigService, private _title: Title, private _translate: TranslateService, private _router: Router) {
        this._router.events.filter(o => o instanceof NavigationEnd).flatMap((o: NavigationEnd) => {
            if (!o.urlAfterRedirects.startsWith('/run/') || o.urlAfterRedirects.indexOf('~') > -1)
                return this._translate.get('CURRENT_MODEL.TITLE');
            else {
                const url = o.urlAfterRedirects.substring(1)/*skip starting slash*/.split('/').slice(2);
                let list = this._config.runPages;
                let find = this._config.runPages[0];
                for (let i = 0; i < url.length; ) {
                    const index = list.findIndex(rp =>
                        rp.identifier.split('/').every((part, index2) =>
                            part.startsWith(':') || part === url[i + index2])
                    );

                    if (index === -1) throw new Error(url[i] + ' not found in run page hierarchy');
                    find = list[index];

                    list = find.children || [];
                    i += find.identifier.split('/').length;
                }
                return Rx.Observable.zip(this._translate.get('CURRENT_MODEL.TITLE'), this._translate.get(`CURRENT_MODEL.RUN_PAGES.${find.displayTitle.toUpperCase()}`)).map(r => `${r[0]} - ${r[1]}`);
            }
        }).subscribe(ret => this._title.setTitle(ret));
    }
}

2 个答案:

答案 0 :(得分:1)

使用RxJS 6,您需要导入所需的运算符,并使用管道使Observables工作。这样做是为了启用摇树功能,您可以在官方网站上阅读有关此内容的更多信息。

对于您的用例,以下带有正确导入的代码段应该起作用。

@Injectable()
export class TitleService {
    constructor(private _config: ConfigService, private _title: Title, private _translate: TranslateService, private _router: Router) {
        this._router.events.pipe(
            filter(o => o instanceof NavigationEnd),
            flatMap((o: NavigationEnd) => {
                if (!o.urlAfterRedirects.startsWith('/run/') || o.urlAfterRedirects.indexOf('~') > -1)
                    return this._translate.get('CURRENT_MODEL.TITLE');
                else {
                    const url = o.urlAfterRedirects.substring(1)/*skip starting slash*/.split('/').slice(2);
                    let list = this._config.runPages;
                    let find = this._config.runPages[0];
                    for (let i = 0; i < url.length; ) {
                        const index = list.findIndex(rp =>
                            rp.identifier.split('/').every((part, index2) =>
                                part.startsWith(':') || part === url[i + index2])
                        );

                        if (index === -1) throw new Error(url[i] + ' not found in run page hierarchy');
                        find = list[index];

                        list = find.children || [];
                        i += find.identifier.split('/').length;
                    }
                    return Rx.Observable.zip(this._translate.get('CURRENT_MODEL.TITLE'), this._translate.get(`CURRENT_MODEL.RUN_PAGES.${find.displayTitle.toUpperCase()}`)).map(r => `${r[0]} - ${r[1]}`);
                }
            })
        ).subscribe(ret => this._title.setTitle(ret));
    }
}

答案 1 :(得分:0)

您的括号不匹配-您的第二个pipe调用稍后将包围subscribe调用。您需要修正括号。