page is showing twice in the browser using angular router?

我是研究僧i 提交于 2020-04-17 22:43:53

问题


Angular was loading page on default port localhost:4200 i wanted it to serve as localhost:4200/specialtyquestions when app build and that is working but its showing pages twice in the browser , Any idea what is implemented wrong here ?

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AppComponent } from './app.component';


const routes: Routes = [{
  path: 'specialtyquestions',
  component: AppComponent
},
{
  path: '',
  redirectTo: 'specialtyquestions',
  pathMatch: 'full'
}];


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

app.component.html

<app-navbar></app-navbar>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-5">
            <app-create-questions></app-create-questions>
        </div>
        <div class="col-sm-1">
            <div class="row">
                <button type="button" class="btn btn-primary" data-backdrop='static' (click)="openModal()" style="margin-top: 300px;" data-toggle="modal" data-target="#exampleModalLong">Start Formatting</button>
            </div>
        </div>
        <div class="col-sm-5">
            <app-format-questions></app-format-questions>
        </div>
    </div>
</div>
<h2>
    <router-outlet></router-outlet>
</h2>

image


回答1:


Remove Router-Outlet because when you Add that tag at the last it Run that part again. so remove that part from the end and Check

<app-navbar></app-navbar>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-5">
            <app-create-questions></app-create-questions>
        </div>
        <div class="col-sm-1">
            <div class="row">
                <button type="button" class="btn btn-primary" data-backdrop='static' (click)="openModal()" style="margin-top: 300px;" data-toggle="modal" data-target="#exampleModalLong">Start Formatting</button>
            </div>
        </div>
        <div class="col-sm-5">
            <app-format-questions></app-format-questions>
        </div>
    </div>
</div>

Router part is needed when you want to route from one page to another e.x. 127.0.0.1:4000/Student



来源:https://stackoverflow.com/questions/61126591/page-is-showing-twice-in-the-browser-using-angular-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!