当手动导航到浏览器时,Angular应用程序没有加载我的组件路径
我的应用程序从登录
页面上开始,该页面上有一个按钮create-account
页面。
如果我在create-account
页面上,并且刷新了浏览器,则返回到登录
页面。
这是为什么?我尝试阅读文档以找到解决方案,但并不完全清楚。我可以从浏览器中看到,如果我手动导航到http:// localhost/create-account
,它将返回301,然后返回登录
page。为了允许创建
击中该路由的路线必须采取什么措施?
这是我的app-routing.module.ts
:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CreateAccountComponent } from './create-account/create-account.component';
import { LoginComponent } from './login/login.component';
import { PasswordResetComponent } from './password-reset/password-reset.component';
const routes: Routes = [
{ path: "login", component: LoginComponent },
{ path: "create-account", component: CreateAccountComponent },
{ path: "password-reset", component: PasswordResetComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
My application starts on a login
page which has a button to a create-account
page.
If I am on the create-account
page and I refresh my browser, it goes back to the login
page.
Why is that? I tried reading through the documentation to find a solution, but it wasn't entirely clear. I can see from the browser that if I manually navigate to http://localhost/create-account
, it returns a 301 and goes back to the login
page. What must be done to the route to fix this in order to allow create-account
to be hit?
Here is my app-routing.module.ts
:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CreateAccountComponent } from './create-account/create-account.component';
import { LoginComponent } from './login/login.component';
import { PasswordResetComponent } from './password-reset/password-reset.component';
const routes: Routes = [
{ path: "login", component: LoginComponent },
{ path: "create-account", component: CreateAccountComponent },
{ path: "password-reset", component: PasswordResetComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀,那是因为我的
app.component.ts
在其中有一个,并且在应用程序初始化应用程序(首先浏览到)时都会被。
调用 如果您查看<<
> logincomponent
> >,则添加了一个没有映射的URL路由将转到登录
页面,如果您查看<<<< login 页面代码>路由在这里:Whoops, it was because my
app.component.ts
had this in it, and it gets called for every route when the application is initialized (first browsed to):I have refactored this to:
I have also added a wildcard route resolver to the
LoginComponent
instead, so that URL routes that have no mapping will go to thelogin
page, if you look at the last element added in theRoutes
here: