2016-09-16 76 views
0

我已经将我的项目更新到最新版本的Angular 2.0.0。 看来,this.location.back()不再按预期工作。 我的意思是:从'@ angular/common'导入{Location};Angular 2.0.0 location.back()破

如果this.location.back()被直接调用url更改,但html网站不会更改。

如果被放在里面this.zone.run(()=> this.goBack()) URL是改变,但HTML网站被正确地改变。

我很确定,它与Angular2的旧版本候选版一起工作。

也许这是一个问题,它只与路由中的参数canActivate结合使用。

{path: 'thing', component: ThingComponent, canActivate: [AuthGuard]}, 

回答

0

看来,问题不在于location.back()。

这个愚蠢的问题是,当你在click事件中不使用event.preventDefault时,浏览器会做一些奇怪的事情。

正确的解决方法是: Angular2 router.navigate refresh page

不要忘记HTML类型的属性。

1

我试过this.location.back(),它正常工作与角2.0

import { Component, Input, OnInit } from '@angular/core'; 
import {Location} from '@angular/common'; 
import { ActivatedRoute, Params } from '@angular/router'; 
import {Hero} from './hero' 
import { HeroService } from './hero.service'; 


@Component({ 
    selector: 'my-hero-detail', 
    templateUrl: 'app/hero-detail.component.html', 
    styleUrls: ['app/hero-detail.component.css'] 

}) 
export class HeroDetailComponent implements OnInit { 
    constructor(
     private heroService: HeroService, 
     private route: ActivatedRoute, 
     private location: Location) { 
     } 

    ngOnInit(): void { 
    this.route.params.forEach((params: Params) => { 
     let id = +params['id']; 
     this.heroService.getHero(id) 
     .then(hero => this.hero = hero); 
    }); 
    } 
    goBack(): void {  
    this.location.back(); 
    } 
    save(): void { 
    this.heroService.update(this.hero) 
     .then(this.goBack); 
    } 

    @Input() 
    hero: Hero; 
} 
+0

姆姆。对我来说,它不工作,我不能创建一个Plunker的例子,因为它会尝试重新加载router.back()http://plnkr.co/edit/v9QvgF3Xqix7fbFzz2ze?p=preview – Johannes