Angular 6:类型“ typeof Observable”上不存在属性“ fromEvent”

时间:2018-08-14 22:36:32

标签: javascript angular typescript rxjs

我正在为我的angular 6应用中的粘性标头创建粘性导航栏指令

这是我到目前为止所拥有的:

import { Directive, Input, Renderer, ElementRef, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { fromEvent } from 'rxjs';

@Directive({
    selector: '[ngStickyNav]'
})

export class StickyNavDirective implements OnInit {
    private offsetTop: number;
    private lastScroll: number = 0;
    private isSticky: boolean = false;
    @Input('stickyClass') stickyClass: string;

    constructor(private elementRef: ElementRef, private renderer: Renderer) {

    }

    ngOnInit(): void {
        this.offsetTop = this.elementRef.nativeElement.offsetTop;

        Observable.fromEvent(window, 'scroll').subscribe(() => this.manageScrollEvent());
    }
}

我遇到以下错误:

  

类型为“可观察的类型”的属性“ fromEvent”不存在。

我的代码有什么问题?虽然是新手

1 个答案:

答案 0 :(得分:7)

在rxjs v6中,它只是fromEvent

fromEvent(window, 'scroll').subscribe(() => this.manageScrollEvent());