각도 2의 버튼을 사용하여 다른 페이지로 이동
버튼을 클릭하여 다른 페이지로 이동하려고하는데 작동하지 않습니다. 무엇이 문제일까요? 나는 이제 앵귤러 2를 배우고 있는데 지금은 조금 힘들다.
//Routes/Path in a folder call AdminBoard
export const AdminRoutes: Routes =[
{
path: 'dashboard',
component: AdminComponent,
children: [
{path: '', redirectTo: 'Home'},
{path: 'Home', component: HomeComponent},
{path: 'Service', component: ServiceComponent},
{path: 'Service/Sign_in', component:CustomerComponent}
]
}
];
//Button is also in a different folder. Click button to navigate to this page {path: 'Service/Sign_in', component:CustomerComponent}
<button class="btn btn-success pull-right" ><a routerLink="/Service/Sign_in"> Add Customer</a></button>
다음과 같이 사용하면 작동합니다.
<a routerLink="/Service/Sign_in"><button class="btn btn-success pull-right" > Add Customer</button></a>
다음 router.navigateByUrl('..')
과 같이 사용할 수도 있습니다 .
<button type="button" class="btn btn-primary-outline pull-right" (click)="btnClick();"><i class="fa fa-plus"></i> Add</button>
import { Router } from '@angular/router';
btnClick= function () {
this.router.navigateByUrl('/user');
};
최신 정보
다음 Router
과 같이 생성자 에 주입해야합니다 .
constructor(private router: Router) { }
그래야만 this.router
.
업데이트 2
이제 Angular v4 이후 routerLink
에는 아래와 같이 버튼 에 속성을 직접 추가 할 수 있습니다 (댓글 섹션의 @mark에서 언급했듯이).
<button routerLink="/url"> Button Label</button>
다음과 같은 방법으로 routerLink를 사용할 수 있습니다.
<input type="button" value="Add Bulk Enquiry" [routerLink]="['../addBulkEnquiry']" class="btn">
또는 <button [routerLink]="['./url']">
귀하의 경우에 사용 하십시오. 자세한 내용은 github https://github.com/angular/angular/issues/9471 에서 전체 스택 추적을 읽을 수 있습니다.
the other methods are also correct but they create a dependency on the component file.
Hope your concern is resolved.
<button type="button" class="btn btn-primary-outline pull-right" (click)="btnClick();"><i class="fa fa-plus"></i> Add</button>
import { Router } from '@angular/router';
btnClick= function () {
this.router.navigate(['/user']);
};
It is important that you decorate the router link and link with square brackets as follows:
<a [routerLink]="['/service']"> <button class="btn btn-info"> link to other page </button></a>
Where "/service" in this case is the path url specified in the routing component.
you can change
this.router.routeReuseStrategy.shouldReuseRoute = () => false;
at the component level in constructor like bellow
constructor(private router: Router) {
this.router.routeReuseStrategy.shouldReuseRoute = () => false;
}
Having the router link on the button seems to work fine for me:
<button class="nav-link" routerLink="/" (click)="hideMenu()">
<i class="fa fa-home"></i>
<span>Home</span>
</button>
참고URL : https://stackoverflow.com/questions/41427405/navigate-to-another-page-with-a-button-in-angular-2
'IT story' 카테고리의 다른 글
Sublime 2 업데이트 알림을 끄는 방법은 무엇입니까? (0) | 2020.09.08 |
---|---|
CG_CONTEXT_SHOW_BACKTRACE 환경 변수를 어떻게 설정할 수 있습니까? (0) | 2020.09.08 |
프로그램은 릴리스 빌드로만 충돌합니다. 디버그 방법은 무엇입니까? (0) | 2020.09.08 |
intellij 아이디어로 Maven 목표를 디버깅하는 방법은 무엇입니까? (0) | 2020.09.08 |
AppBarLayout으로 스크롤링보기 겹침 (0) | 2020.09.07 |