2017-12-27 51 views
0

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 { 
} 

답변

1

RouterComponent에는 Router 인수가 필요합니다. 라우터는 주사 할 수 있으므로, Angular가 RouterComponent 클래스를 처리하는 방법을 알고 있으면 해결할 수 있습니다.

클래스를 Injectable으로 꾸미고 각도 값에 값을 삽입하는 것이 가장 좋습니다. 예 :

import { Injectable } from '@angular/core';  
import { Router } from '@angular/router'; 

@Injectable() 
export class RouterService { 
    constructor(private router: Router) { } 

    homePage(){ 
    this.router.navigateByUrl('/home'); 
    } 
}; 

는 모듈에 등록하거나 Component 장식에 제공 필드에 종속성으로 추가하고 구성 요소로 가져옵니다.

import { Component } from '@angular/core'; 
import { RouterService } from '...'; 

@Component({ ... }) 
export class LoginComponent { 
    constructor(private router: RouterService) { } 

    toHomePage() { 
    this.router.homePage(); 
    } 
}; 

왜냐하면 주사제이기 때문에 각도는 매개 변수를 해결하는 방법을 알고 있습니다.

RouterComponent 클래스의 명명 규칙을 선택하면 다른 사람들이 각도 component으로 꾸며 졌다고 생각할 수 있지만 service으로 사용하고 있습니다.

+0

감사합니다. 문제를 해결했습니다. angular2에 대해 배울 것입니다. @ 주입 가능 – brk

+0

단지 하나의 업데이트 RouterComponent를 공급자에 추가해야합니다. – brk

+0

그건 사실이지만, 답변에 포함되어 있고 왜냐하면 당신이 그것을 당신의 모든 구성 요소에서 사용하기 위해서 당신의'NgModule'에 그것을 등록 할 수 있기 때문입니다. 그러나 Angular가 주사 가능한 매개 변수를 해결하기 위해서는 이러한 모듈을 동일한 모듈에 등록해야합니다. –