router.navigateByUrl
각각을 클래스의 함수로 묶으려고하고 해당 위치에서 해당 함수를 호출하려고합니다. 하지만 'Supplied parameters'를 던지면 호출 목표의 서명과 일치하지 않습니다. 나는 SO에서 몇 가지 다른 링크를 따라했지만 아무도 내 경우에 도움이 될제공된 매개 변수가 호출 대상의 서명과 일치하지 않습니다. 클래스 인스턴스화시 오류가 throw됩니다.
commonRouter.ts
// have wrapped navigation to home in homePage
// so wherever is needed this homePage will be called instead of
//this.router.navigateByUrl('/home');
import {Router} from '@angular/router';
export class RouterComponent{
router:any;
constructor(private rt:Router){
this.router=rt;
}
homePage(){
this.router.navigateByUrl('/home');
}
}
someComponent.ts 것 같다
// Importing the newly created typescript file
import {RouterComponent} from './../../app-routing-component';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.less']
})
export class LoginComponent implements OnInit {
private ms:MainService= new MainService();
//Instantiating RouterComponent
private rt:RouterComponent = new RouterComponent(); // this line throwing error
constructor(private fb:FormBuilder) {}
someMethod(){
rt.homePage() // Calling homePage
}
//... rest of code
}
앱 routing.module.ts
// module where all the paths and component are declared
import {NgModule} from "@angular/core";
import {RouterModule, Routes} from "@angular/router";
import {HomeComponent} from "./home/home/home.component";
const routes: Routes = [
{
path: 'login', component: LoginComponent,
}, {
path: 'home', component: HomeComponent,
children: [{
path: "account",
component: AccountsComponent
},{
path: '**',
component: PageNotFoundComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}
감사합니다. 문제를 해결했습니다. angular2에 대해 배울 것입니다. @ 주입 가능 – brk
단지 하나의 업데이트 RouterComponent를 공급자에 추가해야합니다. – brk
그건 사실이지만, 답변에 포함되어 있고 왜냐하면 당신이 그것을 당신의 모든 구성 요소에서 사용하기 위해서 당신의'NgModule'에 그것을 등록 할 수 있기 때문입니다. 그러나 Angular가 주사 가능한 매개 변수를 해결하기 위해서는 이러한 모듈을 동일한 모듈에 등록해야합니다. –