【发布时间】:2016-07-27 09:41:02
【问题描述】:
说明:
我正在关注angular2 教程,当window.history.back() 无法正常工作时,我感到很困惑。有些信息在某种意义上会“丢失”。
在以下三张图片中,您将看到以下内容:
- 显示顶级英雄列表的图像。我会点击“毒枭”
- 详细显示英雄毒枭的图片,我将点击“返回”按钮。
- 图像再次显示英雄列表,但这次是空的。 (如果我再次点击“
Dashboard”,英雄列表就会被“填满”。)
图 2:
上图详细展示了英雄毒枭。
图 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();
}
}
问题:
为什么“返回”按钮在 Safari 中不起作用?我在 Chrome 中尝试过这个没有问题。有没有办法让它在 Safari 中工作?
【问题讨论】:
标签: javascript typescript angular safari angular2-routing