【问题标题】:Angular modules href not working角度模块href不起作用
【发布时间】:2018-09-11 00:41:34
【问题描述】:

我有一个应用程序,该应用程序与 ng 服务器 (localhost:4200) 一起工作,但是当我使用命令 ng build --env prod --base-href / dashboard2 / 部署它时,根目录确实可以工作,但模块显示 404 错误。

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

//Importar componentes
import { LoginComponent } from './login/login.component';
import { HomeComponent } from './home/home.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { UserProfileComponent } from './user-profile/user-profile.component';
import { CreateUserComponent } from './create-user/create-user.component';


const appRoutes: Routes = [
    {path:'', component: LoginComponent},
    {path:'dashboard', component: DashboardComponent},
    {path:'login', component: LoginComponent},
    {path:'user-profile',   component: UserProfileComponent },
    {path:'create-user', component:CreateUserComponent},
    {path:'home', component: HomeComponent},
    {path:'**', component: LoginComponent}
];

export const appRoutingProviders: any[] = [];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

【问题讨论】:

  • 为什么dashboard2周围有空格?
  • 一行写错了

标签: angular angular-ui-router http-status-code-404 href


【解决方案1】:

如果您使用PathLocationSTrategy(默认),则需要为令牌APP_BASE_HREF 指定基本标记和提供程序。您是否已将此行添加到 @NgModule 装饰器中?

providers: [{provide: APP_BASE_HREF, useValue: '/dashboard2'}]

APP_BASE_HREF 会影响路由器在应用内解析routerLinkrouter.navigate() 调用的方式。



<base href="..."> 标签会影响浏览器在加载 <link><script><img> 等静态资源时解析 URL 的方式。

【讨论】:

  • 错误是由于在调用 this.router.navigate 到 window.location.reload 之前造成的。非常感谢
【解决方案2】:

试试下面的命令:

ng build --prod --base-href /PROJECT_NAME/

【讨论】:

  • www.domain.com/dashboard/user-profile ----- Not Found 在此服务器上未找到请求的 URL /dashboard/user-profile。
猜你喜欢
  • 2018-06-07
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多