I have created a new module in my app so I can separate parts that do not have to communicate and created an new.module.ts with its own routing module and components :
new-routing.module.ts
:
const exportRoutes: Routes = [
{
path: 'export',
component: ExportsComponent
}
]
@NgModule({
imports: [RouterModule.forRoot(exportRoutes)],
exports: [RouterModule]
})
export class ExportRoutingModule {}
new.module.ts
:
import { Router } from '@angular/router'
import { BrowserModule } from '@angular/platform-browser'
// Routing Module
import { NewRoutingModule } from './new-routing.module'
@NgModule({
imports: [..., ExportRoutingModule, ...],
declarations: [ExportsComponent],
bootstrap: [ExportsComponent]
})
I have a simple index.html
:
<body class="app">
<router-outlet></router-outlet> // outlet is here because I read that I needed it to be able to use activated route, I actually just want the query params
</body>
and finally, where the problem lies, my component :
export class MyComponent implements OnInit {
constructor(private activatedRoute: ActivatedRoute) {}
ngOnInit() {this.activatedRoute.queryParamMap.subscribe(
(params: ParamMap) => console.log(params)
// Do stuff with params => error)}
When I navigate to http://localhost:4200/export?firstParam=1.00,2.00,3.00
in my console, the params
are logued twice, once empty, once populated as such :
ParamsAsMap {params: {…}}
keys:(...) // empty
params:{} // empty
core.js:3565 Angular is running in the development mode. Call enableProdMode() to enable the production mode.
ParamsAsMap {params: {…}}
keys:Array(3)
params:{firstParam: "1.00,2.00,3.00", secondParam: "bla"}
This cause my component to throw error since I need those params to display my component and the first time they are logued they are empty so :
- Why are they loggued twice ?
- Why is my code executed before my params observable has a value ?
- Could I get rid of the router outlet (which I don't really need since I have no routing involved with this module, I just used it because I read that I couldn't use
activatedRoute
without it; I just want the query params from my url
Thanks for your help