Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a' [duplicate]

I am creating a component then it's set to root component then i am getting error Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a'. i have using RouterLink as a directive in component anotation but it can't work.

app.routes.ts

import { ModuleWithProviders }  from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { dogRoutes }    from './Dogs/dog.routes';
import { catRoutes }    from './Cats/cat.routes';
import {userRoutes} from "./Users/user.routes";
// Route Configuration
export const routes: Routes = [
  {
    path: '',
    redirectTo: '/dogs',
    pathMatch: 'full'
  },
  ...catRoutes,
  ...dogRoutes,
  ...userRoutes
];

export const routing: ModuleWithProviders = RouterModule.forRoot(routes);

app.component.ts

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

@Component({
  selector: 'my-app',
  template: `
  <div class="demo-layout-transparent mdl-layout mdl-js-layout">
  <header class="mdl-layout__header mdl-layout__header--transparent">
    <div class="mdl-layout__header-row">
      <!-- Title -->
      <span class="mdl-layout-title">Scotch Pets</span>
      <!-- Add spacer, to align navigation to the right -->
      <div class="mdl-layout-spacer"></div>
      <!-- Navigation with router directives-->
      <nav class="mdl-navigation">
        <a class="mdl-navigation__link" [routerLink]="['/']">Home</a>
        <a class="mdl-navigation__link" [routerLink]="['/cats']">Cats</a>
        <a class="mdl-navigation__link" [routerLink]="['/dogs']">Dogs</a>
        <a class="mdl-navigation__link" [routerLink]="['/login']">Login</a>
      </nav>
    </div>
  </header>
  <main class="mdl-layout__content">
    <h1 class="header-text">We care about pets...</h1>
  </main>
</div>
<!-- Router Outlet -->
<div class="container">
  <router-outlet></router-outlet>
    </div>
  `,

})

export class AppComponent{}

app.module.ts

import { NgModule }       from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { FormsModule }    from '@angular/forms';
import { HttpModule, JsonpModule } from '@angular/http';

import { AppComponent }         from './app.component';
import { DogsModule } from './Dogs/dogs.module';
import { CatsModule } from './Cats/cats.module';
import { UserModule }          from './Users/users.module';
import {routing} from './app.routes';
import {RouterModule} from "@angular/router";


@NgModule({
  imports: [
    BrowserModule,
FormsModule,
 HttpModule,
RouterModule,
JsonpModule,
UserModule,
DogsModule,
CatsModule,
routing
 ],
 declarations: [
  AppComponent,
 ],
 providers: [   ],
  bootstrap: [ AppComponent ]
})

export class AppModule {}

dog-list.component.ts

 import {Component, OnInit} from '@angular/core';
import {PetService} from '../services/pet.service'
import {Observable} from 'rxjs/Observable';
import {Pet} from '../model/pet';
import {AuthenticationService} from "../../Users/services/authentication.service";


@Component({

  template: `
    <h2>Dogs</h2>
    <p>List of dogs</p>
    <ul class="demo-list-icon mdl-list">
      <li class="mdl-list__item" *ngFor="let dog of dogs | async">
        <span class="mdl-list__item-primary-content">
            <i class="material-icons mdl-list__item-icon">pets</i>
            <a [routerLink]="['/dogs', dog.id.$t]">{{dog.name.$t}}</a>
        </span>
      </li>
    </ul>
    `
})
// Component class implementing OnInit
export class DogListComponent implements OnInit {
  // Private property for binding
  dogs: Observable<Pet[]>;

  constructor(private petService: PetService, private _service: AuthenticationService) {

  }

  // Load data ones componet is ready
  ngOnInit() {
    this._service.checkCredentials();
    // Pass retreived pets to the property
    this.dogs = this.petService.findPets('dog');
    alert(JSON.stringify(this.dogs))
  }
}

Is there a better way for me to add fix this error?

like image 929
Prashant Jangid Avatar asked Dec 21 '16 17:12

Prashant Jangid


People also ask

Can't bind to routerLink since it isn't a known property of TD?

You need to add RouterModule to imports of every @NgModule() where components use any component or directive from (in this case routerLink and <router-outlet> . declarations: [] is to make components, directives, pipes, known inside the current module.

Can we pass object in routerLink?

RouterLink for dynamic dataDynamic data or user-defined objects can be passed from Angular version 7.2 using the state object stored in History API. The state value can be provided using the routerLink directive or navigateByURL.

Can't bind to routerLink since it isn't a known property of ion Col?

the reason is that you probably did not include RouterModule to your @NgModule in imports section. Important thing is this needs to be included in every module where you are using Router link or any other router feature.

Can not match any routes URL segment?

This generally occurs when there is a mismatch in the routes specified, or a component which is not present in our routing configuration, or if there is the use of multiple routes in our angular application which is not properly configured.


1 Answers

You need to add the RouterModule to imports: [] of every module where you use router directives like RouterOutlet or routerLink-

like image 174
Günter Zöchbauer Avatar answered Sep 23 '22 20:09

Günter Zöchbauer