I'm setting up ' ' path based on a condition which is working fine and setting up the route to ' ' path. I works fine on my development server. But when i build the project and run it on production server it gives me this error.
Uncaught Error: Invalid configuration of route ' '. One of the following must be provided: component, redirectTo, children or loadChildren
This is my 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: I've imported all the components and dependencies so it ain't a problem here.
I think this is the result of packaging optimization under the production mode of Angular.
Please take a look at this test first.
When I try the following code, the result is the same:
- error: 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];
- right
const home: Route = {
path: '',
component: HomeComponent
}
Finally, for your problem solution, you can do something like this:
// 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
];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With