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

时间:2019-01-24 05:44:25

标签: angular angular-routing angular-router

我正在根据可以正常工作的条件设置“”路径,并设置到“”路径的路线。我在开发服务器上工作正常。但是,当我构建项目并在生产服务器上运行它时,会出现此错误。

未捕获的错误:路由''的无效配置。必须提供以下之一: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:我已经导入了所有组件和依赖项,所以在这里这不是问题。

1 个答案:

答案 0 :(得分:0)

我认为这是在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
];