Angular 2 - 替换历史而不是推动

时间:2016-08-11 08:23:14

标签: angular typescript angular2-routing

如何更换历史而不是推动角度2的新路由器(rc.1)?

例如我在问题列表中{/questions)在新路线(/questions/add)中打开新模态,在添加新问题后,我转到问题视图({{1} })。如果我按回去,我想转到/questions/1而不是/questions

2 个答案:

答案 0 :(得分:65)

如果您使用

location.replaceState

然后

this.router.navigate(['questions/1']);
this.location.replaceState('questions/1');

使用完全相同的路径,您可以触发通常发生的更改,以及替换历史记录。

例如,在您的情况下:

router.serializeUrl(router.createUrlTree(/* what you put in your router navigate call */));

如果您需要向路线添加参数,可以使用

创建位置的网址
this.router.navigate(['questions/1', {name:"test"}]);
this.location.replaceState(this.router.serializeUrl(this.router.createUrlTree(['questions/1', {name:"test"}])));

在你的例子中:

this.router.navigate(["questions/1"], {replaceUrl:true});

更新

角度路由器现在似乎带有replace url option。在您的示例中:

setTimeout(()=>{
    this.router.navigate(["questions/1"], {replaceUrl:true});
});

更新2

有一个当前issue,其中在短时间内完成的多次导航可能无法正确替换URL。作为临时解决方法,将导航功能包装在超时中,以便在单独的循环中触发每个导航功能:

create table #temp
(
       iden int identity(1,1),
       ID int,
       [rank] int,
       [date] date,
       dr_id int,
       rownum_id int,
       grouprecord int
)


Insert into #temp(id,rank,date)
select 1 , 7  ,'07/08/2015'
union all select  1 , 7  ,'09/08/2015'
union all select  1 , 8  ,'08/16/2015'
union all select  1 , 8  ,'08/17/2015'
union all select  1 , 7  ,'08/19/2015'
union all select  1 , 7  ,'08/15/2015'
union all select  2 , 7  ,'08/01/2015'
union all select  2 , 7  ,'08/02/2015'
union all select  2 , 8  ,'08/16/2015'
union all select  2 , 8  ,'08/17/2015'
union all select  2 , 7  ,'08/26/2015'
union all select  2 , 7  ,'08/28/2015'


update t1
set dr_id = t2.rn
from #temp t1 inner join
(select  iden, dense_rank() over(order by id) as rn from #temp) t2
on t1.iden = t2.iden


update t1
set rownum_id = t2.rn
from #temp t1 inner join
(select  iden, row_number() over(partition by dr_id order by id) as rn from #temp) t2
on t1.iden = t2.iden




select *,row_number() over(order by iden)rn into #temp1 from
(             
                     select t2.* 
              from #temp t1 inner join #temp t2
              on (t1.dr_id = t2.dr_id or t2.dr_id = (t1.dr_id +1) ) and ( t1.rank<>t2.rank or t2.dr_id = (t1.dr_id +1) )
              and t2.iden = t1.iden  + 1
)a





declare @id int,@miniden int,@maxiden int,@maxid int
set @id = 1
select @maxid = max(iden) from #temp

while exists(select 1 from #temp1 where rn = @id)
begin
       Select @miniden = iden from #temp1
       where rn = @id

       Select @maxiden = iden from #temp1
       where rn = @id+1

       update #temp
       set grouprecord = @id +1
       where iden between @miniden and @maxiden

IF(@maxiden IS NULL)
BEGIN 
       Update #temp
       set grouprecord = @id +1
       where iden between @miniden and @maxid
END

       set @id = @id + 1
       SET @miniden =NULL
       SET @maxiden = NULL
end

UPDATE #TEMP
SET GROUPRECORD = 1
WHERE GROUPRECORD IS NULL
select * from #temp

答案 1 :(得分:3)