Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to inject ActivatedRouteSnapshot

Injecting ActivatedRouteSnapshot into a component is not working (and neither is injecting ActivatedRoute). Here the stack trace:

"Error: Can't resolve all parameters for ActivatedRouteSnapshot: (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?). at SyntaxError.ZoneAwareError (http://localhost:4200/polyfills.bundle.js:7042:33) at SyntaxError.BaseError [as constructor] (http://localhost:4200/vendor.bundle.js:73735:16) at new SyntaxError (http://localhost:4200/vendor.bundle.js:6140:16) at CompileMetadataResolver._getDependenciesMetadata (http://localhost:4200/vendor.bundle.js:19345:31) at CompileMetadataResolver._getTypeMetadata (http://localhost:4200/vendor.bundle.js:19220:26) at CompileMetadataResolver._getInjectableMetadata (http://localhost:4200/vendor.bundle.js:19208:21) at CompileMetadataResolver.getProviderMetadata (http://localhost:4200/vendor.bundle.js:19450:40) at http://localhost:4200/vendor.bundle.js:19408:49 at Array.forEach (native) at CompileMetadataResolver._getProvidersMetadata (http://localhost:4200/vendor.bundle.js:19375:19) at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (http://localhost:4200/vendor.bundle.js:18848:30) at CompileMetadataResolver._loadDirectiveMetadata (http://localhost:4200/vendor.bundle.js:18736:23) at http://localhost:4200/vendor.bundle.js:18937:54 at Array.forEach (native) at CompileMetadataResolver.loadNgModuleDirectiveAndPipeMetadata (http://localhost:4200/vendor.bundle.js:18936:41)" 

In the component, ActivatedRouteSnapshot is injected as follows:

constructor([...], private router: Router,           private route: ActivatedRouteSnapshot) {     [...] } 

ActivatedRouteSnapshot is provided in app.component.ts:

@Component({     selector: 'app-root',     templateUrl: './app.component.html',     styleUrls: ['./app.component.css'],     providers: [[...], ActivatedRouteSnapshot] }) 

I'm trying to access the query params similarly to how it is done here: https://stackoverflow.com/a/38997116/3433306

According to the docs it should be as simple as adding it to the constructor parameters: https://angular.io/docs/ts/latest/api/router/index/ActivatedRouteSnapshot-interface.html

What am I missing to successfully inject ActivatedRouteSnapshot?

like image 972
studersi Avatar asked May 26 '17 15:05

studersi


People also ask

What is the difference between ActivatedRoute and ActivatedRouteSnapshot?

Since ActivatedRoute can be reused, ActivatedRouteSnapshot is an immutable object representing a particular version of ActivatedRoute . It exposes all the same properties as ActivatedRoute as plain values, while ActivatedRoute exposes them as observables.

What is ActivatedRouteSnapshot in angular?

ActivatedRouteSnapshotlinkContains the information about a route associated with a component loaded in an outlet at a particular moment in time. ActivatedRouteSnapshot can also be used to traverse the router state tree.

What is the difference between ActivatedRoute and router?

Router does redirecting, don't call APIs about "getting the route information". If you needed to subscribe to changes in route, use this. router . ActivatedRoute does all "get the query params, get current URL, etc".

What is Routerstatesnapshot in angular?

This is a tree of activated route snapshots. Every node in this tree knows about the "consumed" URL segments, the extracted parameters, and the resolved data. The following example shows how a component is initialized with information from the snapshot of the root node's state at the time of creation.


1 Answers

Use ActivatedRoute instead of ActivatedRouteSnapshot. Then you can use the snapshot like this:

constructor(activatedRoute: ActivatedRoute) {    var snapshot = activatedRoute.snapshot; } 
like image 67
Markus Kollers Avatar answered Sep 22 '22 20:09

Markus Kollers