简体   繁体   English

当在angular 4中有一个外部路由文件时,为什么我的应用程序重新加载整个页面而不是组件?

[英]Why is my application reloading the full page instead of the component when having an external routing file in angular 4?

I just started migrating an app to Angular + Flex-Layout + Angular Material. 我刚刚开始将应用程序迁移到Angular + Flex-Layout + Angular Material。

I decided to have my routing in an external file called "app-routing.module.ts". 我决定将路由保存在一个名为“ app-routing.module.ts”的外部文件中。 I export my module in in the app.module.ts within "imports". 我将模块导入到“导入”中的app.module.ts中。 This is my routing file: 这是我的路由文件:

import { NgModule }             from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { HomeComponent }        from './home/home.component'
import { CreateMatchComponent } from './match/create-match.component'

const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent },
  { path: 'match/new', component: CreateMatchComponent}
];

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

And here is the HTML from the app.component that renders my router outlet. 这是来自app.component的HTML,它呈现了我的路由器出口。

<div class="containerX">
    <div fxLayout="row wrap">
        <mat-toolbar color="primary" class="mat-elevation-z4">
            <span>Amazing Football Stats App</span>
            <span class="example-spacer"></span>
            <mat-icon class="example-icon">favorite</mat-icon>
            <mat-icon class="example-icon">delete</mat-icon>
        </mat-toolbar>
    </div>          
    <div fxLayout="row wrap" style="padding-top: 8px">
        <router-outlet></router-outlet>
    </div> 
</div>

As you can see, my App Component has a div with the navigation bar and then another div with my <router-outlet> . 如您所见,我的应用程序组件的导航栏上有一个div,而我的<router-outlet>又有一个div。

If I go to localhost:4200 it loads <app-root> which contains the <nav-bar> and the <router-outlet> and since the "route" is empty it redirects me to "/home". 如果我转到localhost:4200它将加载<app-root> ,其中包含<nav-bar><router-outlet>并且由于“ route”为空,因此会将我重定向到“ / home”。

Now my problem is this: If I change the URL to: localhost:4200/match/new (In the browser, in the URL bar) hit enter, I would expect to leave the <nav-bar> and only update the <router-outlet> and the same goes backwards. 现在我的问题是这样的:如果将URL更改为: localhost:4200/match/new (在浏览器中的URL栏中),请按Enter,我希望离开<nav-bar>而仅更新<router-outlet> ,并且相同。

If I am on a different page and I change the URL to "/home" (or even leaving it empty) it should keep the nav bar and only update the router outlet. 如果我在其他页面上,并且将URL更改为“ / home”(甚至将其留空),则应保留导航栏,仅更新路由器插座。

Sorry if this is a stupid question I just started with angular routing. 抱歉,如果这是一个愚蠢的问题,我只是从角度路由开始。 What am I doing wrong? 我究竟做错了什么?

When you change the browser location, the browser is handling that change and will send a new HTTP request to your server. 当您更改浏览器位置时,浏览器正在处理该更改,并将向您的服务器发送新的HTTP请求。 That's why it reloads the whole page. 这就是为什么它会重新加载整个页面。

In order to only change the component loaded in the <router-outlet> , you need Angular's router to handle the change, which is done by using the routerLink directive: 为了只更改<router-outlet>加载的组件,您需要Angular的路由器来处理更改,这是通过使用routerLink指令完成的:

<a routerLink="match/new" routerLinkActive="active">Create Match</a>

or programmatically with a call to router.navigate : 或通过编程方式调用router.navigate

constructor(private router: Router) {}
...
goToCreateMatch() {
    this.router.navigate(['/match/new']);
}

Here's a link to angular's documentation, for more info: 这是指向angular的文档的链接,以获取更多信息:

https://angular.io/guide/router#router-links https://angular.io/guide/router#router-links

https://angular.io/api/router/Router#navigate https://angular.io/api/router/Router#navigate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 上载文件并重新加载div而不是重新加载页面 - Upload a file and reloading a div instead reloading a page 当我将CSS放在外部文件而不是内部文件中时,为什么CSS停止工作 <header> 标签? - Why does my CSS stop working when I put it in an external file instead of inside the <header> tag? 为什么我的Ajax脚本重新加载我的页面 - why is my ajax script reloading my page 重新加载组件Angular的问题 - Problems on reloading component Angular 重新加载iframe onclick按钮,而不是重新加载页面Angular2 - Reload iframe onclick button instead reloading page Angular2 为什么 Firebase Auth 在登录时会重新加载页面? - Why is Firebase Auth reloading the page when signing in? 为什么我的角度组件会忽略我的CSS文件? - Why does my angular component ignore my css file? 当页面向下滚动时,从应用程序组件移动到 Angular 中的单独组件时的侧边栏没有页面的全高 - Sidebar when moved from app component to a separate component in Angular doesn't have full height of the page when the page scrolls down 当按下提交页面重新加载为 php 文件 - when press submit the page reloading as the php file 为什么 Angular Routing 在我的网站上不起作用? - Why is Angular Routing not working on my website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM