【发布时间】:2021-11-09 09:36:54
【问题描述】:
我总是尝试获取'/' 它显示static-root-component(我的主页的组件),
但是当它是'/welcome' 时,页面会立即重定向到'/' 并加载static-root-component 而不是welcome-component
最初我想将未经授权的用户重定向到欢迎页面,但登录状态只能在 JavaScript 中检查。在 JS 获得有关登录状态的信息后,它决定使用 location.replace("/welcome") 重定向,但是...... Angular 再次转到 '/'
“有趣”的事实:在使用
ng serve进行调试期间没有任何路由问题,但在使用ng build时总是会发生这种情况
不知道出了什么问题还有app.module.ts:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { StaticRootComponent } from './static-root/static-root.component';
import { WelcomeComponent } from './welcome/welcome.component';
import { HttpClientModule } from '@angular/common/http';
import { HttpService } from './http.service';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
const appRoute: Routes = [
{ path: '', component: StaticRootComponent, pathMatch: 'full' },
{ path: 'welcome', component: WelcomeComponent }
];
@NgModule({
declarations: [
AppComponent,
StaticRootComponent,
WelcomeComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
RouterModule.forRoot(appRoute),
HttpClientModule
],
providers: [HttpService],
bootstrap: [AppComponent]
})
export class AppModule { }
如果需要,我可以删除任何其他 Angular 文件
【问题讨论】:
-
你试过了吗:
const appRoute: Routes = [ { path: '/', component: StaticRootComponent, pathMatch: 'full' }, { path: '/welcome', component: WelcomeComponent } ]; -
分享完整代码。
-
@milan,显然我试过了,但编译器说:
Invalid configuration of route '/welcome': path cannot start with a slash -
您是否尝试先添加
welcome路由? -
@Shakthifuture,你想看什么文件?我用
ng new生成了项目并且没有编辑其他文件
标签: javascript node.js angular angular-ui-router