我正在关注angular2教程,当window.history.back()
无法正常工作时我很困惑。一些信息会丢失"从某种意义上说。
在以下三张图片中,您将看到以下内容:
Dashboard
"。图片2:
上图显示了英雄Narco的详细信息。
图片3: 上面的图片显示了在上一张图片中按下后的顶级英雄的空列表。
英雄细节代码:
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中使用它?
答案 0 :(得分:1)
这是Angular中的一个已知错误,将在2.0-rc5中修复(错误修复已经在master中)。 (https://github.com/angular/angular/commit/f08060b0b00c98341c7e1fd9acb984402c280396)
到目前为止,您可以手动触发变更检测:
constructor(protected ref: ChangeDetectorRef)
{
}
protected change()
{
this.ref.detectChanges();
}