【问题标题】:goBack button not working in Safari with Angular2goBack按钮在带有Angular2的Safari中不起作用
【发布时间】:2016-07-27 09:41:02
【问题描述】:

说明:

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

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

  1. 显示顶级英雄列表的图像。我会点击“毒枭”
  2. 详细显示英雄毒枭的图片,我将点击“返回”按钮。
  3. 图像再次显示英雄列表,但这次是空的。 (如果我再次点击“Dashboard”,英雄列表就会被“填满”。)

图片 1: 上图显示了顶级英雄的列表。


图 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


    【解决方案1】:

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

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

    constructor(protected ref: ChangeDetectorRef)
        {
    
        }
    
        protected change()
        {
                this.ref.detectChanges();
        }
    

    【讨论】:

    • 感谢您的回答。您能否更具体地说明一下如何实现 ChangeDetectorRef?
    • 看这里,上面的评论已经回答了所有的可能性:stackoverflow.com/questions/34827334/…,可能更容易更新到最新的角度路由器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    相关资源
    最近更新 更多