How to fix Angular 2 `Uncaught (in promise): TypeError: Cannot read property 'query' of null`?

前端 未结 6 1283
甜味超标
甜味超标 2020-12-10 03:36

I\'ve been using the Heroes tutorial in the Angular 2 docs to experiment. However, I\'ve come to a point that I don\'t understand what\'s happening with this error:

6条回答
  •  天涯浪人
    2020-12-10 04:28

    In my case, I fix this problem using @Inject decorator. Try this:

    import {Inject} from '@angular/core'
    
    constructor(@Inject(HeroService) private _heroService: HeroService,
                @Inject(RouteParams) private _routeParams: RouteParams) {}
    

    My project is configured with JSPM and this is the only way to work with dependency injection, I do not why, anyone has an explanation?

    Edited: I have found the solution. In my case, I have to add this configuration into config.js file

    System.config({
     baseURL: "",
     defaultJSExtensions: true,
     transpiler: "typescript",
     typescriptOptions: {
        "target": "es5",
        "emitDecoratorMetadata": true
     },
     paths: {
        "npm:*": "jspm_packages/npm/*",
     ....
    

    Now I can remove @Inject decorator and this sentence works properly

    constructor(private _heroService: HeroService,
                private _routeParams: RouteParams) {}
    

    The key is emitDecoratorMetadata: true

提交回复
热议问题