aspnetcore-angular-universal icon indicating copy to clipboard operation
aspnetcore-angular-universal copied to clipboard

Routing Resolver not working

Open rphilp opened this issue 9 years ago • 9 comments

Trying to wire a resolve statement on a child route and I was getting these zone/injection errors: I tried simplifying it and putting it on the counter path built in and I was getting the same error:

Uncaught (in promise): Error↵Error↵ at Error.ZoneAwareError (http://localhost:5000/dist/vendor.js:83135:33)↵ at ZoneAwareError (http://localhost:5000/dist/vendor.js:83132:35)↵ at injectionError (http://localhost:5000/dist/vendor.js:2233:86)↵ at noProviderError (http://localhost:5000/dist/vendor.js:2271:12)↵ at ReflectiveInjector_.throwOrNull (http://localhost:5000/dist/vendor.js:3772:19)↵ at ReflectiveInjector.getByKeyDefault (http://localhost:5000/dist/vendor.js:3811:25)↵ at ReflectiveInjector.getByKey (http://localhost:5000/dist/vendor.js:3743:25)↵ at ReflectiveInjector.get (http://localhost:5000/dist/vendor.js:3612:21)↵ at AppBrowserModuleInjector.NgModuleInjector.get (http://localhost:5000/dist/vendor.js:4559:52)↵ at PreActivation.getToken (http://localhost:5000/dist/vendor.js:74898:25)↵ at http://localhost:5000/dist/vendor.js:74885:51↵ at http://localhost:5000/dist/vendor.js:70873:86↵ at forEach (http://localhost:5000/dist/vendor.js:70850:13)↵ at waitForMap (http://localhost:5000/dist/vendor.js:70871:5)↵ at PreActivation.resolveNode (http://localhost:5000/dist/vendor.js:74884:16) <

To keep it simple, they are in the same file, so I do not think injection is the issue. I also have another example more complicated that has the same error and it works in other projects.

@Injectable()
export class IdResolver implements Resolve<number> {
  constructor(private router: Router) {}
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<number> {
    let id = route.params['id'];
    return id;
  }
}

RouterModule.forRoot([...
{
path: 'counter', component: CounterComponent,
data: {
    title: 'Counter',
    meta: [{ name: 'description', content: 'This is an Counter page Description!' }],
    links: [
         { rel: 'canonical', href: 'http://blogs.example.com/counter/something' },
         { rel: 'alternate', hreflang: 'es', href: 'http://es.example.com/counter' }
    ]},
    resolve: { id: IdResolver }
},

rphilp avatar Apr 07 '17 17:04 rphilp

Is it possible that it's because it doesn't have @Injectable() on top of the Resolver?

Is this only happening if it's a Child Route?

cc/ @vikerman Do we have test cases for childroute resolvers working as well?

MarkPieszak avatar Apr 07 '17 17:04 MarkPieszak

My apologies, it was there I just forgot to put it on a new line (after the ticks). I've updated the first post with it, but here it is again.

@Injectable()
export class IdResolver implements Resolve<number> {
  constructor(private router: Router) {}

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<number> {
    let id = route.params['id'];

    return id;
  }
}

rphilp avatar Apr 07 '17 18:04 rphilp

You said this was working in other projects, were those angular2-universal or platform-server projects with Node, or regular CLI / angular apps?

MarkPieszak avatar Apr 07 '17 18:04 MarkPieszak

They were not Angular2-Universal apps. It was a regular Angular2 app with a dotnet backend.

I'll see if I can test it elsewhere and report back.

rphilp avatar Apr 07 '17 19:04 rphilp

It may be something we hadn't considered / tested with platform-server, this could potentially be a bug.

Could you try it here real quick: https://github.com/FrozenPandaz/ng-universal-demo That's a regular Node angular app with platform-server (as opposed to .NET)

Wondering if it'll work fine there. If not, think we found a bug! @rphilp Thank you again 🥂

MarkPieszak avatar Apr 07 '17 19:04 MarkPieszak

Appears to work on the base route. I apparently missed adding it as a provider in the example I tested it on earlier. Running into child view issues so still testing that.

rphilp avatar Apr 07 '17 20:04 rphilp

I will definitely have to add some child lazy-loaded stuff here so people follow best practices (as well as test to make sure it works without a hitch). If you find anything out let me know!

MarkPieszak avatar Apr 07 '17 20:04 MarkPieszak

I used Angular Router CrisisCenterModule as a test and I think I have everything working now.

Thanks for everything with this project.

rphilp avatar Apr 07 '17 22:04 rphilp

Glad you're enjoying it so far! 👍 If you ever have any performance / tweaks / improvements feel free to send a PR, we're always trying to improve the repo!

MarkPieszak avatar Apr 14 '17 12:04 MarkPieszak