2017-07-25 27 views
0

我有一個類似於http://example.com/login/?redirect="/user#account"的URL,因此我在此存儲redirect值,並使用它在成功登錄後導航到該頁面。如果redirect查詢中沒有任何# (hash)值,那麼這可以正常工作,但如果它具有該值然後將該值重定向到該路由,即使用戶未登錄,它也應該重定向到http://example.com/login/#/login,如果沒有登錄。Angular:查詢參數值包含路由值

+0

angularjs或angular2? –

+0

@NikhilRadadiya它在Angular2/4 –

+0

你使用'ActivatedRoute'嗎? –

回答

0

的方式角4 URL創建和路線是這樣的:

import { Component, OnInit } from '@angular/core'; 
import { Router } from '@angular/router'; 

@Component({ 
    selector: 'app-home', 
    templateUrl: './home.component.html', 
    styleUrls: ['./home.component.css'] 
}) 
export class HomeComponent implements OnInit { 

    constructor(private router: Router) { } 

    ngOnInit() { 
    } 

    onLoadServers(id: number){ 
    this.router.navigate(['/servers', id, 'edit'],{queryParams: {allowEdit: '1'}, fragment: 'loading'}); 
    } 

} 

由於命令onLoadServers()您將收到的路由以下觀點導航

http://localhost:4200/servers/1/edit?allowEdit=1#loading

正如你所提到沒有 「」。

在通過這條路線零件載你可以收到的查詢參數和片段(裝載 - 在這種情況下)有:

constructor(private route: ActivatedRoute) { } 

ngOnInit() { 
    console.log(this.route.snapshot.queryParams);//{allowEdit:"1"} 
    console.log(this.route.snapshot.fragment);//loading 
}