Angular 5 Path param

时间:2018-03-25 17:07:33

标签: angular url parameters path observable

我希望在状态上获得Path param并使用此参数从我的API获取用户。

我通过查看这个角度指南尝试了一些东西:Route parameters in the ActivatedRoute service

但这不起作用。

路由页面的链接:

<a [routerLink]="['/admin/users', user.id]" > Person </a>

这是我想要识别的组件:

export class UserManagementViewComponent implements OnInit {

  user$: Observable<User>;


  constructor(private userManagementService:UserManagementService,private route:ActivatedRoute) { }

  ngOnInit() {
    this.get();
  }

  get(){

    this.route.paramMap
        .switchMap((params: ParamMap) => {
          return this.userManagementService.get(+params.get("id"))

          }
      )

  }

这是我服务上的get函数:

public get(id:number):Observable<User>{
    let url = this.apiUrl+ "/admin/user/" + id;
    return this.http.get<User>(url);
  }

这是显示传入用户的模板:( UserManagementViewComponent的模板):

<div class="card-content grey-text">
        <span class="card-title">{{(user$  | async)?.name}}'s Profile</span>
        <hr class="seperate">

    <ul>
      <li>name : {{(user$ | async)?.name}}</li>
      <li>surname : {{(user$  | async)?.surname}}</li>
      <li>username : {{(user$ | async)?.username}}</li>
      <li>email : {{(user$  | async)?.email}}</li>
    </ul>

  </div>

我得到的错误是:

ERROR in node_modules/rxjs/Subject.d.ts(3,28): error TS6143: Module './Observable' was resolved to '/home/sam/git/jwt-spring-security-and-angular/web-ui/node_modules/rxjs/Observable.js', but '--allowJs' is not set.
node_modules/rxjs/observable/IfObservable.d.ts(1,51): error TS6143: Module '../Observable' was resolved to '/home/sam/git/jwt-spring-security-and-angular/web-ui/node_modules/rxjs/Observable.js', but '--allowJs' is not set.
node_modules/rxjs/operator/do.d.ts(1,28): error TS6143: Module '../Observable' was resolved to '/home/sam/git/jwt-spring-security-and-angular/web-ui/node_modules/rxjs/Observable.js', but '--allowJs' is not set.
node_modules/rxjs/operator/map.d.ts(1,28): error TS6143: Module '../Observable' was resolved to '/home/sam/git/jwt-spring-security-and-angular/web-ui/node_modules/rxjs/Observable.js', but '--allowJs' is not set.
node_modules/rxjs/operator/switchMap.d.ts(1,45): error TS6143: Module '../Observable' was resolved to '/home/sam/git/jwt-spring-security-and-angular/web-ui/node_modules/rxjs/Observable.js', but '--allowJs' is not set.

问题是什么,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

The error message is telling you that 'Observable' is resolving to a .js file instead of a .ts file. Check your imports.