현재 문서는 실제 경로 세그먼트가 아닌 경로 매개 변수를 얻는 것에 대해서만 이야기합니다.
예를 들어 현재 경로의 부모를 찾으려면 어떻게 가능합니까?
답변
새로운 V3 라우터에는 url 속성이 있습니다.
this.router.url === '/login'
답변
각도 RC4 :
Router
에서 가져올 수 있습니다@angular/router
그런 다음 주입하십시오.
constructor(private router: Router ) {
}
그런 다음 URL 매개 변수를 호출하십시오.
console.log(this.router.url); // /routename
답변
Location
컴포넌트에 주입 하고 읽습니다 . Angular가 해결할 수 있도록 어딘가에 location.path();
추가해야합니다 . ROUTER_DIRECTIVES
Location
import: [RouterModule]
모듈 에 추가 해야합니다.
최신 정보
V3 (RC.3) 라우터에서는 ActivatedRoute
해당 snapshot
특성을 사용하여 자세한 정보를 주입 하고 액세스 할 수 있습니다 .
constructor(private route:ActivatedRoute) {
console.log(route);
}
또는
constructor(private router:Router) {
router.events.subscribe(...);
}
답변
새 라우터의 경우> = RC.3
이 작업을 수행하는 가장 좋고 간단한 방법입니다!
import { Router } from '@angular/router';
constructor(router: Router) {
router.events.subscribe((url:any) => console.log(url));
console.log(router.url); // to print only path eg:"/login"
}
답변
이것을 사용하십시오
import { Router, NavigationEnd } from '@angular/router';
constructor(private router: Router) {
router.events.filter(event => event instanceof NavigationEnd)
.subscribe(event => {
console.log(event);
});
}
그리고 main.ts
수입
import 'rxjs/add/operator/filter';
편집하다
현대적인 방법
import {filter} from 'rxjs/operators';
router.events.pipe(
filter(event => event instanceof NavigationEnd)
)
.subscribe(event => {
console.log(event);
});
답변
아직도 이것을 찾는 사람들을 위해. Angular 2.x에는 몇 가지 방법이 있습니다.
constructor(private router: Router, private activatedRoute: ActivatedRoute){
// string path from root to current route. i.e /Root/CurrentRoute
router.url
// just the fragment of the current route. i.e. CurrentRoute
activatedRoute.url.value[0].path
// same as above with urlSegment[]
activatedRoute.url.subscribe((url: urlSegment[])=> console.log(url[0].path))
// same as above
activatedRoute.snapshot.url[0].path
// the url fragment from the parent route i.e. Root
// since the parent is an ActivatedRoute object, you can get the same using
activatedRoute.parent.url.value[0].path
}
참고 문헌 :
답변
경로 세그먼트를 얻으려면
import { ActivatedRoute, UrlSegment } from '@angular/router';
constructor( route: ActivatedRoute) {}
getRoutes() { const segments: UrlSegment[] = this.route.snapshot.url; }
