goBack按钮在Safari中没有使用Angular2

时间:2016-07-27 09:41:02

标签: javascript typescript angular safari angular2-routing

说明

我正在关注angular2教程,当window.history.back()无法正常工作时我很困惑。一些信息会丢失"从某种意义上说。

在以下三张图片中,您将看到以下内容:

  1. 图片显示了顶级英雄列表。我将点击" Narco"
  2. 图片详细展示了英雄Narco,我将点击" Back"按钮。
  3. 图像再次显示英雄列表,但这次是空的。 (英雄列表获得"填充"如果我再次点击" Dashboard"。
  4. 图片1: list of top heroes 上图显示了顶级英雄列表。

    图片2:

    hero narco

    上图显示了英雄Narco的详细信息。

    图片3: list of empty heroes 上面的图片显示了在上一张图片中按下后的顶级英雄的空列表。

    英雄细节代码:

    import { Component, OnInit, OnDestroy } from '@angular/core';
    import { ActivatedRoute } from '@angular/router';
    import { HeroService } from './hero.service';
    import {Hero} from "./hero";
    import {Location} from '@angular/common';
    
    @Component({
        selector: 'my-hero-detail',
        templateUrl: 'app/hero-detail.component.html',
        styleUrls: ['app/hero-detail.component.css']
    })
    
    export class HeroDetailComponent implements OnInit, OnDestroy{
        hero: Hero;
        sub: any;
        constructor( private _location: Location, private heroService: HeroService, private route: ActivatedRoute){}
    
        ngOnInit():any {
            this.sub = this.route.params.subscribe(params =>{
                let id = +params['id'];
                this.heroService.getHero(id).then(hero => this.hero = hero)
            })
        }
    
        ngOnDestroy():any {
            this.sub.unsubscribe();
        }
    
        goBack(){
            //Not working
            this._location.back();
            //Also tried with window.history.back();
        }
    }
    

    问题:

    为什么" Back"按钮在Safari中不起作用?我在Chrome中试过没有问题。是否可以在Safari中使用它?

1 个答案:

答案 0 :(得分:1)

这是Angular中的一个已知错误,将在2.0-rc5中修复(错误修复已经在master中)。 (https://github.com/angular/angular/commit/f08060b0b00c98341c7e1fd9acb984402c280396

到目前为止,您可以手动触发变更检测:

constructor(protected ref: ChangeDetectorRef)
    {

    }

    protected change()
    {
            this.ref.detectChanges();
    }