Search code examples
angularroutesangular-router

Angular Route ExtraOptions object


In configuring my Angular (ng version 8.2.2) routing module I would like to create an object to handle all the configs. The configuration object goes in as the 2nd parameter of the forRoot() function and needs to be of type ExtraOptions (https://angular.io/api/router/ExtraOptions)

It works if I create the object in-line:

RouterModule.forRoot(routes,{ onSameUrlNavigation: 'reload' })

But not as a predefined object.

const options = { onSameUrlNavigation: 'reload' }

RouterModule.forRoot(routes,options)

This gives me an error of:

ERROR in src/app/app-routing.module.ts(24,32): error TS2345: Argument of type '{ onSameUrlNavigation: string; }' is not assignable to parameter of type 'ExtraOptions'. Types of property 'onSameUrlNavigation' are incompatible. Type 'string' is not assignable to type '"reload" | "ignore"'.

I tried declaring the type of object as:

const options: ExtraOptions = {
    onSameUrlNavigation: 'reload'
}

But I get the error:

Cannot find name 'ExtraOptions'.

Does anyone know if it is possible to create an ExtraOptions object to configure a Router?


Solution

  • Your code seems to be working fine here make sure you have imported extraOptions from @angular/router

    import { Routes, RouterModule, ExtraOptions } from '@angular/router';