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

14,730

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
];
Share:
14,730
Saad Ashfaq
Author by

Saad Ashfaq

Hello, I am Saad and I am a self-taught software developer with a degree in Computer System Engineering. I have been doing Web and Cross-Platform Mobile Application Development for more than 2 years, mostly with JavaScript. In my career, both as a freelancer and as an employee, I have had opportunities to work on a wide range of projects, ranging from automation scripts to SaaS web applications. My ability to learn and understand things quickly and adapt to changes and environments has helped me grow enormously in my career. I am open to opportunities where I can further polish and improve my skills. I am looking for a team that believes in processes, writing clean and testable code, and making something big.

Updated on June 05, 2022

Comments

  • Saad Ashfaq
    Saad Ashfaq almost 2 years

    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.

    • Harkal
      Harkal over 3 years
      var home: Route = {}; home must not be empty [this comment might help someone in future]