Issue with Angular 4 Routing: Links are opening in new window instead of within router-outlet

I am currently facing an issue while trying to display the SuburbDataComponent HTML on the DASHBOARD-SIDE-PANEL-COMPONENT.HTML. When I click on Dashboard, it opens a new window but only displays the SuburbDataComponent.html without showing the side panel. The link is not showing within the router-outlet. Can someone please review the code and identify what may be wrong? Thank you.

*************************
    app.Module.ts 
*************************

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

        import { DashboardSidePanelComponent } from './user-dashboard/dashboard-side-panel/dashboard-side-panel.component';
        import { SuburbsDataComponent } from './user-dashboard/dashboard-side-panel/suburbs-data/suburbs-data.component';

        const appRoutes: Routes = [
          { path: '', component: HomepageComponent },
          { path: 'userDashboard', component: DashboardSidePanelComponent },
          { path: 'testData', component: SuburbsDataComponent }
        ];

        @NgModule({
          declarations: [
            AppComponent,
            DashboardSidePanelComponent,
            SuburbsDataComponent
          ],
          imports: [
            FormsModule,
            ReactiveFormsModule,
            BrowserModule,
            HttpModule,
            RouterModule.forRoot(appRoutes)
          ],
          providers: [SuburbsService, AutoCompleteSuburbs, AuthService, AuthGuard],
          bootstrap: [AppComponent]
        })
        export class AppModule { }

*************************
DASHBOARD-SIDE-PANEL-COMPONENT.HTML
*************************

<div class="sidebar">
          <ul class="nav nav-sidebar">
            <li>
              <h3>My Services</h3>
            </li>
            <li>
              <a routerLink="/testData"><span>Dashboard</span></a>
            </li>

            <li>
              <a routerLink="/userDashboard/sending"><i class="fa fa-paper-plane-o fa-lg userDashIcons" aria-hidden="true"></i> <span>Send</span></a>
            </li>

            <li>
              <a href="#"><i class="fa fa-arrow-circle-o-left fa-lg userDashIcons" aria-hidden="true"></i> <span>Receive</span></a>
            </li>

            <li>
              <a href="#"><i class="fa fa-cart-arrow-down fa-lg userDashIcons" aria-hidden="true"></i> <span>Ecommerce</span></a>
            </li>
          </ul>
    </div>

<div class="container-sending">
    <router-outlet></router-outlet>
</div>
*************************
DASHBOARD-SIDE-PANEL-COMPONENT.ts
*************************

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

@Component({
  selector: 'app-dashboard-side-panel',
  templateUrl: './dashboard-side-panel.component.html',
  styleUrls: ['./dashboard-side-panel.component.css']
})
export class DashboardSidePanelComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

******************************
SuburbDataComponent.html
******************************

<div class="continerred">
  <h1> I AM A TEST WITH RED CONTAINER</h1>
</div>
******************************
SuburbDataComponent.ts
******************************

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

@Component({
  selector: 'app-suburbs-data',
  templateUrl: './suburbs-data.component.html',
  styleUrls: ['./suburbs-data.component.css'],
})
export class SuburbsDataComponent  {

  constructor () { }
}

Answer №1

routerLink is a directive that accepts an array of parameters and should be implemented in the following manner:

<li>
    <a [routerLink]="['/testData']"><span>Dashboard</span></a>
</li>

If you have a route such as /testData/:id, then a link for id=3 could be written in two ways:

<a [routerLink]="['/testData/3']">test data #3</a>

OR

<a [routerLink]="['/testData', 3]">test data #3</a>

The second method is recommended for usage.

PLNKR: https://plnkr.co/edit/g2vHik92YWMy5yWJa1et

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

How do I add a new module to an existing one using Angular-CLI?

After generating modules: $ ng generate module myTestModule installing module create src/app/my-test-module/my-test-module.module.ts $ ng generate module myTestModule2 installing module create src/app/my-test-module2/my-test-module2.module.ts I ha ...

A guide on validating dates in Angular Ionic5 with the help of TypeScript

I have tried multiple solutions, but none seem to work when validating the current date with the date entered by the user. The date is passed from the user into the function parameters, but how do I perform validation? How can I validate the date? isToday( ...

Encountering a Lint No Nested Ternary Error while utilizing the ternary operator

Is there a way to prevent the occurrence of the "no nested ternary" error in TypeScript? disablePortal options={ // eslint-disable-next-line no-nested-ternary units=== "mm&quo ...

Waiting for variable to become false using Angular 7 Observable

The observable below highlights the authentication process: import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { CookieService } from 'ngx-cookie-service'; import { Observabl ...

Error 404 encountered while trying to create a new route in an Express tutorial on MDN

Attempting to tackle the challenge section of part 2 in the MDN Express tutorial, I found myself faced with a task after setting up the Express project: The objective is to add a new route in /routes/users.js that will output the message "You're so c ...

The CanDeactivate feature appears to be malfunctioning

I'm currently working on detecting router deactivation. Below is the definition of the router: export const AppRoutes: Routes = [ { path: '', component: HelloComponent, canDeactivate: [ConfirmDeactivateGuard] }, { pat ...

Troubleshooting a pair of distinct software programs: Angular and a web API

I am currently working on two separate applications, one developed in Angular and the other in web API. There is a function in my Angular application that calls a method in .NET Core. I would like to know if it is possible to debug the Angular function and ...

Unable to simulate a returned value from an import in Jest

Within my module, I have a function called shuffle<T>(a: T[]): T[] that is exported by the random module. While testing two methods in another class that rely on this function, I need to mock it. Here's how I attempted to do so: jest.mock(' ...

Disabling lint in a PhpStorm or WebStorm project with angular-cli

I am currently using PhpStorm version 2017.2, and I have created an angular-cli project within a directory that already contains several modules and components built with angular-cli. The issue I am facing is that I am inundated with countless linting err ...

"Unexpected Alignment Issue with NZ Zorro's Dynamic Columns Feature in the Ant Design NZ-Table Component

I am facing an issue with a table that receives columns dynamically from the server. The headers and data columns are not aligned properly. How can I ensure that they align correctly? <nz-table *ngIf="queryResults" #headerTable [nzData]="queryResults" ...

Changing the positions of objects on a resized HTML Canvas

I am currently developing a tool that allows users to draw bounding boxes on images using the Canvas and Angular. Everything was working smoothly until I encountered an issue with zooming in on the canvas causing complications when trying to draw bounding ...

A data type that exclusively accepts values from an enumerated list without mandating the inclusion of every possible value within the enum

Here's a code snippet I'm working with: enum Foo { a, b, c } type Bar = { [key in keyof typeof Foo]: string; } const test: Bar = { a: 'a', b: 'b' }; I'm encountering an issue where the code is complaining ...

Functions have been successfully deployed, but they are not appearing on the Azure Portal

I am experiencing difficulties deploying basic Typescript functions to Azure. Despite a successful deployment using VS code and confirmation in the Output window, I cannot see any functions listed in the Azure Portal under the Function App: https://i.stac ...

Utilizing AngularJS and RequireJS for intricate routing within web applications

I have encountered an issue with nested routings that I am struggling to resolve. The technologies I am using include: AngularJS, RequireJS; AngularAMD, Angular Route. To start off, here is my main routing setup: app.config(function($routeProvider, $loc ...

Circular Dependencies in Angular (only the file name)

Previously, I used to keep interfaces and services in separate files but later combined them into one file since they were always requested together. For example, instead of having user.interface.ts and user.service.ts as separate files, I now have all the ...

The Angular 2+ page fails to load properly on Internet Explorer or SharePoint

I created an Angular 5 solution that functions well in both Chrome and Firefox within SharePoint 2013. However, I have run into issues when using IE11. The index.html file was inserted into the SharePoint page using a Content Editor web part. Even after di ...

The $routeChangeSuccess event is failing to trigger in ngNewRouter in AngularJS version 1.4

Transitioning from AngularJS 1.3 to AngularJS 1.4 has brought about some changes, including the use of AngularJS's new route method known as ngNewRouter, specifically introduced in AngularJS 1.4. Below is a snippet of my code: var versionModule = ng ...

"An error in the signature index results in the failure of the

I'm encountering a coding issue that's puzzling me. The TypeScript index signature I included in my code is as follows: const ships: { [index: string]: Ship } = {}; This snippet of code contains the problematic line: recieveAttack(e: any) { ...

Adding files to an Angular ViewModel using DropzoneJS

I am facing a challenge in extracting file content and inserting it into a specific FileViewModel. This is necessary because I need to bundle all files with MainViewModel which contains a list of FileViewModel before sending it from the client (angular) to ...

Having trouble implementing a CSS style for a Kendo-checkbox within a Kendo-treeview component

I am facing a The issue I am encountering is that while the CSS for k-treeview is being applied from the scss file, it is not being applied for the kendo-checkbox I attempted to resolve the problem by using the following code: <kendo-treeview ...