The page remains static even after selecting a child route using routerLink in Angular 2

Hello, I am facing an issue that I need help with. I am using a routerLink to navigate to a child route in my Angular application. Although the URL changes as expected, the view does not update to display the component associated with the active child route.

Here is the code snippet from app.component.ts:

<app-header></app-header>
<router-outlet></router-outlet>
<app-footer></app-footer>

This is the HTML code:

<div class="wide component">
    <div class="page-wrap">
        <img class="logo" src="../../assets/img/our-work-component-logo.png"><br>
        <h2>Component Elite</h2>
        <h3>Web portal and promotions</h3>

        <p class="short-description">Lorem Ipsum</p>
        <a routerLink="component" class="button orange-button">View More</a>
    </div>
</div>

And this is the configuration in app.module.ts:

import { OurWorkComponent } from './our-work/our-work.component';
import { ComponentComponent } from './our-work-content/component/component.component'

export const ROUTES: Routes = [
  { path: 'our-work', component: OurWorkComponent,
      children: [
         { path: 'component', component: ComponentComponent }
      ]
  }
];

@ngModule({
  declarations: [
     OurWorkComponent,
     ComponentComponent
  ],
  imports: [
     BrowserModule,
     RouterModule.forRoot(ROUTES)
  ],
  providers: [],
  bootstrap: [AppComponent]
})

Answer №1

One reason the changes are not taking effect is due to the presence of children within OurWorkComponent

children: [
    { path: 'component', component: ComponentComponent }
]

This indicates that there should be a <router-outlet> in the OurWorkComponent, which may be missing. Simply add it to the html of OurWorkComponent and you should see the desired results.


For more detailed information, please refer to: https://angular.io/guide/router#nesting-routes

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

Working with Django Rest Framework and Angular 2 to handle the simultaneous upload of multiple files

I'm currently utilizing Django Rest Framework for the backend of my application, with Angular 2 serving as the frontend. In my Angular page, I have a form creation function: createResourcesForm() { this.resourcesForm = this.formBuilder.group({ ...

Using ion-icon inside a clickable ion-card while applying float: right does not render properly

I am facing an issue with a list of ion-cards that have clickable icons appearing when you hover over them. The problem is that due to the floating nature of the icons, they are not positioned correctly (as shown in the image below) and end up getting cove ...

Select from a variety of backgrounds using the dropdown menu on the site

I seem to be struggling with javascript and php, but I know that in order to make this function properly, I need to utilize both languages. My goal is to set up a "select box" featuring 4 different images. There will be a default image displayed, but user ...

Integrating custom non-NPM JavaScript files into Angular 2

Currently, the application I am working on is running in an environment with angular 2.0.0 final using typescript and also utilizes angular-cli 1.0.0-beta.15. Since the development of the application involves multiple developers who all use typescript, I ...

Sending information from PHP to JavaScript

I'm attempting to send a PHP variable, which is a string, using JavaScript. However, when I view the final result, the string appears as a comment in the HTML code. Here's the code snippet: PHP: $txtVar = "My text"; JavaScript: var txt = &ap ...

Ways to display a specific HTML element or tag depending on the given prop in VueJs

When working with Vue.js, it's important to remember that a <template> can only have one root element. But what should be done if you need to render different html elements based on a prop? For instance, let's say we have a <heading> ...

I'm looking to make my dropdown in AngularJS more dynamic by integrating it with Firebase

Currently, I have an app developed using Ionic, TypeScript, AngularJS, and Phonegap. One of the functions in this app involves collecting information on whether the user is male or female. Initially, I created a static form for this purpose. <ion-item& ...

Enter your own text into the input fields to create a personalized message

Is there a way to eliminate duplicates when checking the checkboxes? <label><input type="checkbox" data-bValue="100" data-sValue="sV1" data-nValue="nV1" name="layers"> 100</label><label><input type="checkbox" data-bValue="200" d ...

ways to verify ng-if post modification occurrence

Currently, my project is utilizing Angular 6. In the code, there is a div element with *ng-if="edited" as shown below: <div *ngIf="edited"> {{langText}} </div> Initially, when the page loads, edited is set to false and the div is not vis ...

Preventing Cross-Site Scripting (XSS) vulnerabilities during the

When my site stores user-generated HTML to be rendered on a web page, what are the best practices to avoid XSS attacks? Is simply stripping <script> and <iframe> tags sufficient protection? Will this safeguard work across all browsers? I have h ...

Obtaining the cell element which spans another cell element within a table with JavaScript

We are presenting a table: <table> <tbody> <tr> <td>First Column</td> <td colspan="3">Second Column</td> <td>Third Column</td> ...

The mat-select choices are experiencing rendering issues

This is the HTML code I am using to display select locations and a map below it: Here is the HTML code for the above view, <mat-form-field class="locationSelector"> <mat-select placeholder="Choose location" (ngModel)="ServiceLocations"> ...

Utilize the Spotify API to discover tracks by including the album title and artist's name in the search

Currently working on a project that involves searching for a music track on Spotify. The idea is to input the track name in the text area and generate a list of matching Track IDs along with Artist Names, Album Names, and Artwork. I have made some progress ...

Retrieving the hidden field value from a list view with JavaScript

Custom Script: $(document).ready(function() { //Customize these values to personalize your modal popup var align = 'center'; var top = 100; var width = 500; ...

Module 'mongodb' not found in basic Node.js application

TL;DR: Here are the detailed steps I followed to encounter an error. To get to the main question, scroll to the bottom. I'm currently working through a book called addyosmani - backbone-fundamentals, which guides me in creating a basic backbone.js ap ...

Exploring the contrasts in employing functions within a react-native environment

Imagine having a component called "wo" that receives a function as a parameter. export default class SomeComp extends Component { constructor(props) { super(props); } _someFunc = () => { return ... } render() { ...

Using ReactJS and Redux to dispatch notifications

I have a query regarding displaying notifications for successful or failed user registration actions. Utilizing Redux, I've implemented the following action page: export function registerUserFail(error){ return { type:REGISTER_USER_FAIL, ...

The file 'angular2 src/app/app.routing' does not contain a exportable element called 'routing'

Shown below is the content of my packages.json file: { "name": "ng2", "version": "0.0.0", "license": "MIT", "angular-cli": {}, "scripts": { "start": "ng serve", "postinstall": "typings install", "lint": "tslint \"src/**/*.ts&bs ...

Using JavaScript/JQuery to Access an External SQLite Database

Is there a way to incorporate my own SQLite database into a website using JavaScript and JQuery? I've searched for examples but have yet to find any helpful articles on the topic! ...

Generating intervals from a given set of dates in real-time

When dealing with lists of dates fetched through ajax, how can you dynamically generate date ranges from them? For instance, consider the following list: var arrdates=['01/02/2014','06/05/2014','24/09/2014','05/06/2018&a ...