我的角度路由行为有问题。像/ user这样刷新或输入的URL总是将我重定向到/ home。我还在index.html文件中设置了它。通过单击导航菜单按钮一切正常。但是一旦我尝试刷新页面(例如/ register),我就会被重定向到/ home。
我在app.module.ts
上做错了什么?
看起来像这样:
import { LanguageService } from './_services/language.service';
import { JwtStorageAdapter } from './_helpers/JwtStorageAdapter';
import { TruncatePipe } from './_helpers/truncatePipe';
import { RegisterService } from './_services/register.service';
import { UserService, AlertService, AuthenticationService, EventService, MemberService, ParticipationService } from './_services/index';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { AboutComponent } from './about/about.component';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { CreateMemberComponent } from './user/members/create-member/create-member.component';
import { EditMemberComponent } from './user/members/edit-member/edit-member.component';
import { MaterializeModule } from 'angular2-materialize';
import { NavbarPublicComponent } from './navbarPublic/navbarPublic.component';
import { EventsComponent } from './user/events/events.component';
import { AuthGuard } from './_guards/index';
import { MembersComponent } from './user/members/members.component';
import { RegisterComponent } from './register/register.component';
import { AlertComponent } from './_directives/index';
import { NavbarUserComponent } from './user/navbarUser/navbarUser.component';
import { TeamsComponent } from './user/teams/teams.component';
import { AdminComponent } from './user/admin/admin.component';
import { CreateEventComponent } from './user/events/create-event/create-event.component';
import { DownloadComponent } from './download/download.component';
import { EventDetailComponent } from './user/events/event-detail/event-detail.component';
import { FooterComponent } from './footer/footer.component';
import { SocialmediconsComponent } from './footer/socialmedicons/socialmedicons.component';
import { AgbComponent } from './agb/agb.component';
import { EventsPastComponent } from './user/events/events-past/events-past.component';
const appRoutes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'download', component: DownloadComponent },
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'agb', component: AgbComponent },
{ path: 'user/events', component: EventsComponent, canActivate: [AuthGuard] },
{ path: 'user/members', component: MembersComponent, canActivate: [AuthGuard] },
{ path: 'user/teams', component: TeamsComponent, canActivate: [AuthGuard] },
{ path: 'user/admin', component: AdminComponent, canActivate: [AuthGuard] },
{ path: 'user/events/create', component: CreateEventComponent, canActivate: [AuthGuard] },
{ path: 'user/events/detail', component: EventDetailComponent, canActivate: [AuthGuard] },
{ path: 'user/members/create', component: CreateMemberComponent, canActivate: [AuthGuard] },
{ path: 'user/members/edit', component: EditMemberComponent, canActivate: [AuthGuard] },
{ path: 'user/events/events-past', component: EventsPastComponent, canActivate: [AuthGuard] }
//{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
declarations: [
AppComponent,
AlertComponent,
AboutComponent,
HomeComponent,
LoginComponent,
NavbarPublicComponent,
EventsComponent,
MembersComponent,
RegisterComponent,
NavbarUserComponent,
TeamsComponent,
AdminComponent,
CreateEventComponent,
TruncatePipe,
CreateMemberComponent,
EditMemberComponent,
DownloadComponent,
EventDetailComponent,
FooterComponent,
SocialmediconsComponent,
AgbComponent,
EventsPastComponent
],
imports: [
RouterModule.forRoot(appRoutes),
BrowserModule,
FormsModule,
HttpModule,
MaterializeModule
],
providers: [
AuthGuard,
AlertService,
AuthenticationService,
UserService,
EventService,
MemberService,
RegisterService,
ParticipationService,
JwtStorageAdapter,
LanguageService
],
bootstrap: [AppComponent]
})
export class AppModule { }
答案 0 :(得分:2)
const appRoutes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' }, <---add this
{ path: 'home', component: HomeComponent }
..........
]
答案 1 :(得分:1)
添加pathMatch: 'full'
。改变你的第一条路线:
{ path: '', component: HomeComponent }
到:
{ path: '', component: HomeComponent, pathMatch: 'full' }
答案 2 :(得分:1)
对不起噪音。 解决问题我必须删除app.component.ts中的导航:
ngOnInit() {
--> this.router.navigate(['home']);
...