设备方向无法正常工作

时间:2019-06-10 15:48:28

标签: javascript angular typescript rxjs

我正在使用deviceorientation侦听器尝试一些代码,但它没有按我期望的那样工作。我尝试了几种不同的方法(如下所示),并且在手动更改Chrome的调试器传感器时都可以正常工作,但是,我无法使其自动在任何移动设备上运行。

我正在将此站点用作参考和演示:

-https://www.audero.it/demo/device-orientation-api-demo.html

-https://code.tutsplus.com/tutorials/an-introduction-to-the-device-orientation-api--cms-21067

上面的演示在我的手机上运行良好,但是当我尝试将相同的代码实现到我正在处理的代码中时,什么也没发生。

export class AppComponent {
    absolute: boolean = false;
    alpha: number = 0;
    beta: number = 0;
    gamma: number = 0;
    private orientation$ = fromEvent(window, 'deviceorientation');

    constructor() { }

    ngAfterViewInit() {
    // Method 1
        window.addEventListener('deviceorientation', (e) => console.log(e));

    // Method 2
        this.orientation$.subscribe((res: any) => console.log(res));
    }
}

有什么想法我想念的吗?

0 个答案:

没有答案
相关问题