Angular2 Safari Back Button

ⅰ亾dé卋堺 提交于 2019-12-07 03:02:11

问题


In the actual Angular2 beta 14 (and before) there seems to be an issue with the back button (when using routing, and several views) on Safari (actually using 9.1): https://github.com/angular/angular/issues/7722

I also experienced this problem, while this works fine e.g. on Chrome.

I'm looking for a workaround until the issue is fixed?


回答1:


On "angular2": "2.0.0-beta.14"

I had to run the tick inside a zone to get it to work for me.

import {ApplicationRef, <anything else you need>} from 'angular2/core';
import {Router,<anything else you need>} from 'angular2/router';

export class AppComponent {
    constructor(private _ref: ApplicationRef, private _router: Router) {
        _router.subscribe((value) => {
            _ref.zone.run(() => _ref.tick());
        });
    }
}



回答2:


There is partial workaround for this bug:

  1. Inject Router and ApplicationRef into Application component
  2. Subscribe for router changes and trigger full component tree check:
    router.subscribe((value)=>
    {
        //todo: check browser UA or any other parameters to detect back button, i.e. if (safari) {}

        //trigger change that will invoke init methods
        appRef.tick();
    });



回答3:


angular2 - 2.15.8

constructor(private _ref: ApplicationRef, private _router: Router) {
        _router.events.subscribe((value) => {
            _ref.zone.run(() => _ref.tick());
        });
    }

Using _router.subscribe is deprecated and will throw Undefined function .subscribe() on _router.



来源:https://stackoverflow.com/questions/36617698/angular2-safari-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!