In Angular 2, the routerLink feature appears as regular text on the screen

Recently, I encountered an issue with routerLink in my Angular 2 project. I am currently using Visual Studio 2015 to develop a Single Page Application with routing functionality. However, I noticed that when I click on an anchor tag with [routerLink], it does not redirect me to the intended page - it simply appears as plain text.

MasterPage.html-

Left menu headers footers
<a [routerLink]="['Supplier']">Supplier</a> 
<a [routerLink]="['Customer']">Customer</a><br />
<div>
    <router-outlet></router-outlet>
</div>

Routing.ts -

import {Component} from '@angular/core';
import {CustomerComponent} from '../Binder/CustomerComponent';
import {SupplierComponent} from "../Binder/SupplierComponent";

export const ApplicationRoutes = [
{ path: 'Customer', component: CustomerComponent },
{ path: 'Supplier', component: SupplierComponent }
];

app.module-

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CustomerComponent }   from './CustomerComponent';
import {FormsModule} from "@angular/forms"
import {GridComponent} from "./GridComponent"
import { MasterPageComponent }   from './MasterPageComponent';
import { SupplierComponent }   from './SupplierComponent';
import { RouterModule } from '@angular/router';
import { ApplicationRoutes } from './Routing';

@NgModule({
imports: [
RouterModule.forRoot(ApplicationRoutes),
BrowserModule,
FormsModule],
declarations: [
CustomerComponent,
SupplierComponent,                  
MasterPageComponent],
bootstrap: [MasterPageComponent]
})
export class MainModuleLibrary { }

If you require any further information, feel free to reach out. Thank you in advance.

Answer №1

To create a link, add a forward slash at the beginning:

<a [routerLink]="['/Home']">Home</a>
<a [routerLink]="['/About']">About</a><br />
<div>
    <router-outlet></router-outlet>
</div>

Answer №2

Give this a shot:

  <a routerLink="/Supplier" routerLinkActive="active">Supplier</a>
  <a routerLink="/Customer" routerLinkActive="active">Customer</a>

<router-outlet></router-outlet>

UPDATE: Make sure you also have a similar line in your routing.ts file, like this:

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

and remember to import that into your @ngModule as well. If you're just starting your project and don't have much content yet, these links may show up on your page without being clickable or generating any errors.

Answer №3

masterPage.html

<a [routerLink]="['./supplier']">Supplier</a> 
<a [routerLink]="['./customer']">Customer</a><br />
<div>
    <router-outlet></router-outlet>
</div>

routing.ts

import { Routes, RouterModule } from '@angular/router';
import { CustomerComponent } from '../binder/customerComponent';
import { SupplierComponent } from '../binder/supplierComponent';

const appRoutes: Routes = [
   { path: 'customer', component: CustomerComponent },
   { path: 'supplier', component: SupplierComponent }
];

export const routing = RouterModule.forRoot(appRoutes);

app.module

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CustomerComponent }   from './customerComponent';
import { FormsModule } from "@angular/forms"
import { GridComponent } from "./gridComponent"
import { MasterPageComponent }   from './masterPageComponent';
import { SupplierComponent }   from './supplierComponent';
import { RouterModule } from '@angular/router';
import { routing } from './routing';

@NgModule({
  imports: [
    routing,
    BrowserModule,
    FormsModule],
  declarations: [
    CustomerComponent,
    SupplierComponent,                  
    MasterPageComponent],
  bootstrap: [MasterPageComponent]
})
export class MainModuleLibrary { }

And remember, it's best to avoid capitalizing file names and routes.

Answer №4

For me, the issue stemmed from forgetting to include the RouterModule import in the module where I was utilizing the routerLink directive.

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

@NgModule({
  declarations: [
    //...
  ],
  imports: [
    //...
    RouterModule,
  ],
  exports: [
    //...
  ]
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue encountered: TypeScript compiler (tsc) failed to compile a dependent library within a project, specifically one that depends on a JavaScript library and @types typing package

Imagine a scenario where a library, let's name it LibraryA, relies on another library called js-yaml without type definitions. To make this work, LibraryA has a devDependency called @types/js-yam in its package.json. LibraryA itself compiles smoothly. ...

Tips for adding a new property to an array object in TypeScript using a condition

Here is an array object that I have: arr = [ { Name: "ABC", Age: 20}, { Name: "XXX", Age: 15} ]; In Typescript, I am looking to dynamically add a new property called "Flag" with a value of 1 only if the Age is greater than 15. Can someone suggest ...

Challenges with exporting dynamically generated divs using jspdf in an Angular 2 project

I have been utilizing the jspdf library to print div elements in my current project. But I recently discovered an issue where dynamic content within a div is not being printed correctly. Specifically, when incorporating simple Angular if statements, jspdf ...

formik connects props that are lacking certain properties

Trying to figure out a way to simplify the process of declaring all the properties of formik in my Props when using connect(). The error message I keep encountering is: Type '{ entry: Entry; }' is missing the following properties from type &apos ...

Tips for efficiently utilizing Hooks with React Context:

I am currently working on my application and utilizing React Context with the setState function. const userContext = React.createContext([{ user: {} }, () => {}]); const userHook = useState({ user: {} }); <userContext.Provider value={userHook}> / ...

Special scenarios requiring OnPush Change Detection

I'm currently building an Angular 2 application using ngrx, and I've been intrigued by the concept of OnPush change detection for optimizing performance. After reading multiple articles on the topic, it seems that the general consensus is: "If a ...

Preventing data binding for a specific variable in Angular 2: Tips and tricks

How can I prevent data binding for a specific variable? Here's my current approach: // In my case, data is mostly an object. // I would prefer a global solution function(data) { d = data; // This variable changes based on user input oldD = da ...

Having trouble with the Ng multiselect dropdown displaying empty options?

I'm currently facing a challenge in adding a multiselect dropdown feature to my project. Below is the code I have been working on: HTML <ng-multiselect-dropdown [settings]="searchSettings" [data]="dummyList" multiple> </n ...

A guide to calculating the sum of columns using TypeScript and integrating it with Angular 8

Currently, I am attempting to calculate the average of all columns and display it at the footer of my table. The table data is fetched from an API resulting in a structure like this: <tr *ngFor="let item of items"> <td>{{item.num1 ...

Clerk Bug: The UserResource type returned by useUser() does not match the @clerk/types

When attempting to pass the user obtained from useUser(), an error occurred: The 'UserResource' type is lacking the required properties 'passkeys' and 'createPasskey' from the 'UserResource' type Upon investigating ...

Default functionality of Typescript paths imports fails to operate properly

Can anyone help me figure out how to set up default imports in my .ts files using the paths specified in my tsconfig.base.json? I have this file defined as default in the File Type > Typescript Config. https://i.sstatic.net/LvBGV.png The import statem ...

Warning: React has detected that a non-boolean value of `true` was received for the attribute `my-optional-property`

source code import React from "react"; import { Button, ButtonProps } from "@material-ui/core"; interface MyButtonProps extends ButtonProps { "aria-label": string; "my-optional-property"?: boolean; } function MyCustomButton(props: MyButtonProps) { ...

Discovering how to specify the type of a dynamically created object using 'for await' in TypeScript

for await (user of users) { ... } Encountered an issue: "error TS2552: Cannot find name 'user'. Did you mean 'users'?" Appreciate your assistance. ...

When a MatFormFieldControl both implements ControlValueAccessor and Validator, it can potentially lead to a cyclic

I am attempting to develop a custom form control by implementing MatFormFieldControl, ControlValueAccessor, and Validator interfaces. However, I encounter issues when including NG_VALUE_ACCESSOR or NG_VALIDATORS. @Component({ selector: 'fe-phone-n ...

Restrictive discriminated union via function argument

I am in possession of a shop that organizes a variety of types based on their IDs interface Dog { type: "dog"; woofs: string; } interface Cat { type: "cat"; meows: string; } type Pet = Dog | Cat; type AnimalState = Record<string, Pet ...

What is the best way to modify a constant array in Angular?

Hello team, I'm fresh to working with angular and I have a TypeScript file that contains a list of heroes: export const HEROES: Hero[] = [ { id: 11, name: 'Dr Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: 'Bombas ...

Developing a loader feature in React

I've been working on incorporating a loader that displays when the component has not yet received data from an API. Here's my code: import React, {Component} from 'react' import './news-cards-pool.css' import NewsService fro ...

Why is my Angular2 *ngFor not showing the contents of my array?

Currently delving into Angular 5 and immersing myself in a crash course. I've managed to progress past just using an ngFor directive, but as I tackle part of a practice assignment, I'm encountering an issue where the data won't loop and disp ...

Emulating Data in Angular 2 Using Configuration Similar to Ember Mirage

Is there a way to mock data through configuration in Angular 2 similar to how it's done in Ember Mirage? I'm aware that I can create my own solution using Dependency Injection and MockBackend to intercept HTTP calls and provide dummy data. Howeve ...

Merge two attributes into a single entity using RxJS

Currently, I am working on handling HTTP responses in the Angular framework and I have a requirement to merge two properties using rxjs. Let's consider a sample response from an API where I need to combine the age with birthday. Since the HTTP respon ...