的方式角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
}
angularjs或angular2? –
@NikhilRadadiya它在Angular2/4 –
你使用'ActivatedRoute'嗎? –