[英]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.