Having trouble with Angular 2 Routing and loading components?

I am facing an issue with Angular 2 where it is searching for my component in app/app/aboutus.component, but I cannot pinpoint the source of the problem.

Here is my app.component.ts code:

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

@Component({
  selector: 'my-app',
  templateUrl: 'app/app.component.html',
  directives:[ROUTER_DIRECTIVES]
})
export class AppComponent { }

Next, let's take a look at aboutus.component.ts:

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

@Component({
    selector: 'about-us',
    templateUrl: 'app/aboutus.html',
    directives: [ROUTER_DIRECTIVES]
})

export class AboutUsComponent {
    teammembers: String[];
    constructor() {

    }
    ngOnInit() {
        this.teammembers = ['raja', 'steve', 'trevor'];
    }

}

Now, examining app.routes.ts:

import { provideRouter, RouterConfig } from '@angular/router';

import { AboutUsComponent } from 'aboutus.component';

export const routes: RouterConfig = [
  {path: 'aboutus', component: AboutUsComponent}
];

export const APP_ROUTER_PROVIDERS = [
  provideRouter(routes)
];

In main.ts, our main entry point into the app:

//main entry point into app
import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import { APP_ROUTER_PROVIDERS } from './app.routes';

bootstrap(AppComponent, [
  APP_ROUTER_PROVIDERS
]);

Lastly, here is app.component.html:

<div class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="javascript:void(0)">Angular 2 Routing</a>
    </div>
    <div class="navbar-collapse collapse navbar-responsive-collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a>Pricing</a></li>
        <li><a [routerLink]="['AboutUs']">About Us</a></li>
      </ul>
    </div>
  </div>
</div>
<router-outlet></router-outlet>

The desired template to route to is aboutus.html, which simply contains an <h1> tag. I am relatively new to Angular 2 routing and have been struggling to get Angular to recognize my components. Currently, I am encountering an XHR 404 error on the aboutus.component.js file even after running npm install.

Answer №1

If you encounter a different error, consider the following solution:

export const routes: RouterConfig = [
  {path: '', component: AboutUsComponent}
 {path: 'aboutus', component: AboutUsComponent}
];

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

Deploy the dist folder generated by ng build with the help of msdeploy

Has anyone successfully used msdeploy to install the output of ng build --prod (the dist folder) into IIS? I attempted to do so with this command: msdeploy.exe -verb:sync -source:package=c:\Workspace\MyProject\dist.zip -dest:auto -setPara ...

TypeScript: By providing a variable CLASS as an argument to a function, automatically determine the return type to be an object of the specified class without any additional information

I am looking to create a function that accepts actual class objects themselves as arguments (within an object containing multiple arguments), with the return type of the function being inferred to be an instance of the class provided as the argument. In t ...

How to associate an object with a component in Angular2/TypeScript using HTTP

I am currently working on displaying a list of item names retrieved from a REST API. By utilizing the map function on the response observable and subscribing to it, I was able to obtain the parsed items object. Now, my challenge is how to bind this object ...

Programmatically close a modal dialog using a $http promise

Utilizing AngularJS with Bootstrap UI, I have implemented a modal dialog to display while an HTTP request is processing. This serves as a wait dialog and can be easily inserted into different parts of the code like so: $scope.foobar = function() { var wa ...

What exactly is the purpose of the colon in JavaScript's import statement?

Looking at the following example. import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database' I am puzzled by the use of colons and I am unsure about where the imported files are being referenced from. ...

select items using a dropdown menu in an Angular application

Let me describe a scenario where I am facing an issue. I have created an HTML table with certain elements and a drop-down list Click here for image illustration When the user selects in, only records with type in should be displayed Another image refere ...

Creating an uncomplicated search bar that dynamically adjusts values based on its function

Summing it up, there is a div situated within my app.component.html: <div class="col-lg-6 search-div"> <div class="input-group"> <input type="text" class="form-control" placeholder="Search for..."> <span class="input-group-b ...

Guide on incorporating Kendo UI typings into a TypeScript module

Currently, I am working with Kendo UI for React and TypeScript. My goal is to import the Kendo UI typings for TypeScript using a "typeof import". Following the guidance provided at https://docs.telerik.com/kendo-ui/third-party/typescript, I successfully i ...

Obtaining radio button values from user input forms in Angular

I need help with retrieving values from a user input form. The first option is a select dropdown, the third one is a time picker which are both working fine. However, I'm struggling with the second option, which is a radio button group with two button ...

Utilizing Font Awesome in the header of an Ag-Grid table

Having trouble adding an icon to my header using fa-icon. In my app.component file, I have imported: import { faImages } from '@fortawesome/free-solid-svg-icons'; and added: faImages = faImages; Within my columnDefs, I am using: { field : & ...

Incorporate matter-js into your TypeScript project

Upon discovering this file: https://www.npmjs.com/package/@types/matter-js I ran the following line of code: npm install --save @types/matter-js When I tried to use it in the main ts file, an error message appeared: 'Matter' refers to a U ...

Creating personalized breakpoints in Material UI using TypeScript

When using the createMuiTheme() function, you have the ability to update breakpoint values like this. const theme = createMuiTheme({ breakpoints: { values: { xs: 0, sm: 600, md: 960, lg: 1280, xl: 1920, }, }, }) ...

I prefer to conceal the permission-wrapper component tag in the DOM when using Angular

Looking to hide the permission-wrapper component tag from appearing in the DOM. CRUCIAL: The component itself should conceal the component's tag (such as ), and instead display the template's content when necessary. For example, let's crea ...

In order for the expansion parameter to be successfully used, it must be either of tuple type or passed to the

const myFunction = function(arg1: number, arg2: number, arg3: number) {} const myFunction1 = function() {} const obj = { myFunction, myFunction1 }; type FuncType = typeof obj; function executeFunction<T extends keyof FuncType>(key: ...

Building a High-Performance Angular 2 Application: A Comprehensive Guide from Development to

Recently, I began developing an Angular2 project using the quickstart template. My main concern now is determining which files are essential for deployment on my live server. I am unsure about the specific requirements and unnecessary files within the qu ...

Check the functionality of a React functional component by conducting unit tests on its functions

Is there a way to properly unit test a function within a Functional component in React? Since wrapper.instance() will return null for functional components, what is the best approach to ensure this function is included in testing to achieve maximum coverag ...

Error: The script "build:universal" is required but not found in the

I encountered errors while attempting to run my Angular application on the server side: npm ERR! missing script: build:universal npm ERR! A complete log of this run can be found in: npm ERR! /home/training/.npm/_logs/2018-10-03T11_50_40_593Z-debug.lo ...

The Vue instance seems to be unable to recognize the shims-vue.d.ts file

I encountered an issue with my Vue file. Here is the code snippet: import Vue from 'vue'; import VueRouter from 'vue-router'; export default Vue.extend({ name: 'MyComponentsName', methods: { doRedirect() { this. ...

Update the CSS styling of a parent div based on the active state of specific child divs

I have a class with 4 distinct columns. div class="mainContent"> <div class="left-Col-1" *ngIf="data-1"> </div> <div class="left-Col-2" *ngIf="!data-1"> ...

Angular: Extracting the default value of a FormControl within a Child Component

Is there a way to retrieve the initial value set in a FormControl for a custom component within its ngOnInit method? Suppose you have created a form control like this: testControl = new FormControl("abc123"); And then linked it to your custom component ...