页面使用角度路由器在浏览器中显示两次? [英] page is showing twice in the browser using angular router?

查看:20
本文介绍了页面使用角度路由器在浏览器中显示两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular 在默认端口 localhost:4200 上加载页面在浏览器中,知道这里实现了什么错误吗?

app-routing.module.ts

import { NgModule } from '@angular/core';从'@angular/router' 导入 { Routes, RouterModule };从 './app.component' 导入 { AppComponent };const 路由:路由 = [{路径:'专业问题',组件:AppComponent},{小路: '',重定向到:'专业问题',路径匹配:'完整'}];@NgModule({进口:[RouterModule.forRoot(routes)],出口:[路由器模块]})导出类 AppRoutingModule { }

app.component.html

解决方案

Remove Router-Outlet因为当您最后添加该标签时,它会再次运行该部分.所以从最后删除那部分并检查

<div class="container-fluid"><div class="row"><div class="col-sm-5"><app-create-questions></app-create-questions>

<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">开始格式化</button>

<div class="col-sm-5"><app-format-questions></app-format-questions>

当你想从一个页面路由到另一个页面时需要路由部分前任.127.0.0.1:4000/学生

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

解决方案

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

这篇关于页面使用角度路由器在浏览器中显示两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆