【问题标题】:Error: Invalid configuration of route ''. One of the following must be provided: component, redirectTo, children or loadChildren错误:路由“”的配置无效。必须提供以下之一:component、redirectTo、children 或 loadChildren
【发布时间】:2019-01-24 05:44:24
【问题描述】:

我正在根据运行良好的条件设置“”路径并设置到“”路径的路径。我在我的开发服务器上工作正常。但是当我构建项目并在生产服务器上运行它时,它给了我这个错误。

未捕获的错误:路由“ ”的配置无效。必须提供以下之一:component、redirectTo、children 或 loadChildren

这是我的 app-routing.module.ts

const fallback: Route = {
  path: '**',
  component: NotFoundComponent
}

var   home: Route = {};

const hostName = window.location.host;
console.log('hostName: ', hostName);
var workspaceName = hostName.split('.')[0];
console.log('workspaceName: ', workspaceName);

if (workspaceName === 'peprix' || workspaceName === 'www') {
  console.log("if");
  let homeRoute: Route = {
    path: '', component: MainComponent
  }
  home = homeRoute;
} else {
  console.log("else");
  let homeRoute: Route = {
    path: '', canActivate: [SessionGuard], data: { workspaceName: workspaceName }, resolve: { workspaceId: SigninService }, component: SigninNextComponent
  }
  home = homeRoute;
}

const routes: Routes = [
  home,
  { path: 'signin', canActivate: [SessionGuard], component: SigninComponent, data: { workspaceName: workspaceName } },
  { path: 'create-workspace', canActivate: [SessionGuard], component: CreateWorkspaceComponent },
  { path: 'projects', canActivate: [ProjectGuard], component: ProjectComponent },
  { path: 'password-reset', component: PasswordResetComponent },
  { path: 'new-password', component: NewPasswordComponent },
  fallback
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
  providers: [
    ProjectDetailsService,
    SigninService
  ]
})
export class AppRoutingModule { }

P.S:我已经导入了所有组件和依赖项,所以这里没有问题。

【问题讨论】:

  • var home: Route = {};home 不能为空[此评论可能对将来的某人有所帮助]

标签: angular angular-routing angular-router


【解决方案1】:

我认为这是Angular的生产模式下包装优化的结果。

请先看看这个测试。

当我尝试以下代码时,结果是一样的:

- 错误Uncaught Error: Invalid configuration of route ''. One of the following must be provided: component, redirectTo, children or loadChildren

var home: Route = {};

// No matter what you do later, it is invalid.
// The variable home assigned to routes is unchanged.
Home.path = '';
Home.component = HomeComponent;

// result: home = {}
// so error
const routes: Routes = [home];

-

const home: Route = {
    path: '',
    component: HomeComponent
}

最后,对于您的问题解决方案,您可以这样做:

// Define them first
// MainComponent
const home: Route = {
    path: '',
    component: MainComponent
};

// SigninNextComponent
const home2: Route = {
    path: '',
    component: SigninNextComponent
}

// Your judgment parameters
const hostName = window.location.host;
var workspaceName = hostName.split('.')[0];

// Judge written here
const routes: Routes = [
    workspaceName === 'peprix' || workspaceName === 'www' ? home : home2,
    Fallback
];

【讨论】:

    猜你喜欢
    • 2021-07-02
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 2018-08-04
    • 2017-03-11
    • 1970-01-01
    • 2020-09-03
    • 2019-12-10
    相关资源
    最近更新 更多