I want my homepage to be replaced when I click on a link to another page, but instead, the other page is added to my home

mikeneumannd

I am trying to add a second page to my web app, but instead, the second page's HTML is added to my homepage. Why?

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { OtherPage } from './app.other-page';

const routes: Routes = [
  { path: 'otherpage', component: OtherPage },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.component.html

<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">

<h1 *ngIf="'test' === str">It's true!</h1>

<a [routerLink]="['/otherpage']">Go to Other Page</a>

<router-outlet></router-outlet>

app.component.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  title = 'angular-test';
}

app.other-page.html

 <h1>This is the other page</h1>

app.other-page.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.other-page.html',
})
export class OtherPage {
  title = 'angular-test';
}

In the example code: the "Go to other page" link is still on the page when clicked, while I'd rather the other page consist of only app.other-page.html.

Adrien PESSU

In your example, When you navigate to "/otherpage", the content of the component OtherPage will be displayed instead of router-outlet. That means that everything that is around router-outlet will be always displayed. It makes sense to put the header before router-outlet.

For more informations, you could read the angular router documentation. https://angular.io/guide/router

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I want to validate my flutter form with iconButton and go to the homepage. But i couldn't link the button with the page i want to display

I want my link to only change color when I click on it, not by just refreshing the page. How do I do this with CSS?

I have an image slideshow on my homepage but when I go to other page it throws an error

I want to go to another page when I click on the image in Flutter

I want to pass Props to my userdetails page using link

i want my dropdown to appear when the link is clicked instead of hover

I want to change the css of a page on a the click of a link

My css code in Word Press is working for every other page in the website when i only want it to work for 1 page

I want it to open automatically in my matmenu when the page is opened

How can i link my (html) login page and other pages to my index page

Why in my Angular-project does not replace home-page when I go to another route, and just adds the bottom of the page?

Apache redirects to another page of my site when i use http instead of https

In my php project when I clicked on logout, I am redirecting at home page, and if I click back button I am getting back in account page

How can I add home-page sections in any page, other than home-page in my Shopify Store?

When I click any button in my view page the form is submitted

when I click on logout my login page is not displayed

My Page is automatically Reloading when I click the dropdown button

link to another page not working for when i click on an img but works when i click on a text

I want the data in input fields to be removed when I click on the current page link

I want to add an image to home page of my flutter app.But am getting difficulties with its length and width

Can I add my HTML page as a browser home page?

My html website overflowing the right way when i click my login screen and back to my main page

I want to navigate to Another page after I click Login

I need to link to another HTML page inside my current HTML page using JavaScript or HTML

I want my form to be in the middle of my page not at the top center

How can I open my web application in localhost:8080 directly instead of tomcat home page

Reload another page when I click on button

i want the page to route to another page only when validation is successful

Why won't my link navigate to the page that I want upon clicking it?