반응형
구성 요소 Angular 2 내에서 리디렉션
마지막에 다른 구성 요소로 리디렉션하려는 간단한 방법이 있습니다.
export class AddDisplay{
display: any;
addPairTo(name: string, pairTo: string){
this.display = {};
this.display.name = name;
this.display.pairTo = pairTo;
}
}
내가 원하는 방법의 끝에 다른 구성 요소로 리디렉션하는 것입니다.
export class AddDisplay{
display: any;
addPairTo(name: string, pairTo: string){
this.display = {};
this.display.name = name;
this.display.pairTo = pairTo;
this.redirectTo('foo');
}
}
Angular 2에서 어떻게 수령 할 수 있습니까?
먼저 라우팅 구성
import {RouteConfig, Router, ROUTER_DIRECTIVES} from 'angular2/router';
과
@RouteConfig([
{ path: '/addDisplay', component: AddDisplay, as: 'addDisplay' },
{ path: '/<secondComponent>', component: '<secondComponentName>', as: 'secondComponentAs' },
])
그런 다음 구성 요소 가져 오기에서 라우터를 삽입하십시오.
import {Router} from 'angular2/router'
export class AddDisplay {
constructor(private router: Router)
}
마지막으로해야 할 일
this.router.navigateByUrl('<pathDefinedInRouteConfig>');
또는
this.router.navigate(['<aliasInRouteConfig>']);
@kit의 대답은 괜찮지 만 ROUTER_PROVIDERS
구성 요소의 공급자에 추가 하는 것을 잊지 않고 . 다음 그런 ngOnInit
메서드 내에서 다른 페이지로 리디렉션 할 수 있습니다 .
import {Component, OnInit} from 'angular2/core';
import {Router, ROUTER_PROVIDERS} from 'angular2/router'
@Component({
selector: 'loginForm',
templateUrl: 'login.html',
providers: [ROUTER_PROVIDERS]
})
export class LoginComponent implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
this.router.navigate(['./SomewhereElse']);
}
}
이 Angular cli 6.x를 위해 일했습니다.
import {Router} from '@angular/router';
constructor(private artistService: ArtistService, private router: Router) { }
selectRow(id: number): void{
this.router.navigate([`./artist-detail/${id}`]);
}
참고 URL : https://stackoverflow.com/questions/32896407/redirect-within-component-angular-2
반응형
'ProgramingTip' 카테고리의 다른 글
일로 만 작업 할 때 "datetime.timedelta"와 "dateutil.relativedelta.relativedelta"의 차이점은 무엇입니까? (0) | 2020.11.05 |
---|---|
console.log () 또는 동기화? (0) | 2020.11.05 |
NoSQL의 트랜잭션? (0) | 2020.11.05 |
MVC 3 : HtmlHelpers를 사용하여 추가 된 특성을 조건부로 추가 (0) | 2020.11.05 |
Hibernate 기준 : 매핑 된 연관없이 테이블 결합 (0) | 2020.11.04 |