programing tip

알려진 속성이 아니기 때문에 'routerLink'에 바인딩 할 수 없습니다.

itbloger 2020. 10. 29. 07:52
반응형

알려진 속성이 아니기 때문에 'routerLink'에 바인딩 할 수 없습니다.


최근에 저는 앵귤러 2로 플레이하기 시작했습니다. 지금까지 굉장합니다. 그래서 저는 .NET을 사용하여 배우기 위해 데모 개인 프로젝트를 시작했습니다 angular-cli.

기본 라우팅 설정을 사용하여 이제 헤더에서 일부 경로로 이동하고 싶지만 내 헤더가의 상위이므로이 router-outlet오류가 발생합니다.

app.component.html

<app-header></app-header> // Trying to navigate from this component
    <router-outlet></router-outlet>
<app-footer></app-footer>

header.component.html

  <a [routerLink]="['/signin']">Sign in</a>

이제 나는 그 구성 요소가 래퍼 router-outlet이기 때문에이 방법으로 액세스 할 수 없다는 것을 부분적으로 이해 합니다 router. 그렇다면 이와 같은 시나리오에서 외부에서 내비게이션에 액세스 할 수있는 가능성이 있습니까?

필요한 경우 더 많은 정보를 추가해 드리겠습니다. 미리 감사드립니다.

최신 정보

1- package.json이미 안정적인 @angular/router 3.3.1버전이 있습니다. 2- 내 메인 app모듈에서 routing-module. 아래를 봐주세요.

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AlertModule  } from 'ng2-bootstrap';
import { LayoutModule } from './layout/layout.module';
import { UsersModule } from './users/users.module';
import { AppRoutingModule } from  './app-routing.module';
import { AppComponent } from './app.component';
import { PageNotFoundComponent } from './shared/components/not-found.component';

@NgModule({
  declarations: [
    AppComponent,
    PageNotFoundComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    AlertModule.forRoot(),
    LayoutModule,
    UsersModule,
    AppRoutingModule  --> This is the routing module. 
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { SigninComponent } from './users/signin/signin.component';
import { PageNotFoundComponent } from './shared/components/not-found.component';

const routes: Routes = [
{ path: '**', component: PageNotFoundComponent }
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})

export class AppRoutingModule {}

액세스하려는 경로가 다른 경로에서 위임되었습니다 module.UsersModule

user-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { SigninComponent } from './signin/signin.component';

const usersRoutes: Routes = [
  { path: 'signin',  component: SigninComponent }
];
@NgModule({
  imports: [
    RouterModule.forChild(usersRoutes)
  ],
  exports: [
    RouterModule
  ]
})

export class UsersRoutingModule { }

Layout모듈의 일부인 구성 요소에서 탐색하려고 하지만 라우터 모듈에 대한 개념이 없습니다. 그것이 오류의 원인입니다.

Layout.module.ts

import { NgModule } from '@angular/core';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';

@NgModule({
  declarations: [HeaderComponent, FooterComponent],
  exports: [HeaderComponent, FooterComponent]
})
export class LayoutModule{}

에서 탐색하려고합니다 HeaderComponent. 필요한 경우 더 많은 정보를 제공해 드리겠습니다.


당신은 추가 할 필요가 RouterModuleimports모든의 @NgModule()구성 요소 (이 경우에서 모든 구성 요소 또는 지시어를 사용하는 경우 routerLink<router-outlet>.

declarations: [] 현재 모듈 내부에 알려진 구성 요소, 지시문, 파이프를 만드는 것입니다.

exports: [] is to make components, directives, pipes, available to importing modules. What is added to declarations only is private to the module. exports makes them public.


You are missing either the inclusion of the route package, or including the router module in your main app module.

Make sure your package.config has this:

"@angular/router": "^3.3.1"

Then in your app.module import the router and configure the routes:

import { RouterModule } from '@angular/router';

imports: [
        RouterModule.forRoot([
            {path: '', component: DashboardComponent},
            {path: 'dashboard', component: DashboardComponent}
        ])
    ],

Update:

Move the AppRoutingModule to be first in the imports:

imports: [
    AppRoutingModule.
    BrowserModule,
    FormsModule,
    HttpModule,
    AlertModule.forRoot(), // What is this?
    LayoutModule,
    UsersModule
  ],

I'll add another case where I was getting the same error but just being a dummy. I had added [routerLinkActiveOptions]="{exact: true}" without yet adding routerLinkActive="active".

My incorrect code was

<a class="nav-link active" routerLink="/dashboard" [routerLinkActiveOptions]="{exact: true}">
  Home
</a>

when it should have been

<a class="nav-link active" routerLink="/dashboard" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
  Home
</a>

Without having routerLinkActive, you can't have routerLinkActiveOptions.

참고URL : https://stackoverflow.com/questions/42035387/cant-bind-to-routerlink-since-it-isnt-a-known-property

반응형