繁体   English   中英

错误:路线“”的配置无效。 必须提供以下之一:component,redirectTo,children或loadChildren

[英]Error: Invalid configuration of route ''. One of the following must be provided: component, redirectTo, children or loadChildren

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

未捕获的错误:路由''的无效配置。 必须提供以下之一: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 { }

PS:我已经导入了所有组件和依赖项,所以这里不是问题。

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

请先看一下这个测试。

当我尝试以下代码时,结果是相同的:

- 错误Uncaught Error: Invalid configuration of route ''. One of the following must be provided: component, redirectTo, children or loadChildren 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
];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM