gpt4 book ai didi

Angular 直接 url 路由总是重定向到/home

转载 作者:行者123 更新时间:2023-12-03 20:17:34 34 4
gpt4 key购买 nike

我的 Angular 路由行为有问题。刷新或输入的 url 像/user 总是将我重定向到/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 { }

最佳答案

可能的解决方案:-

const appRoutes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' }, <---add this
{ path: 'home', component: HomeComponent }
..........
]

关于Angular 直接 url 路由总是重定向到/home,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47146797/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com