如何解决这个问题的角度?

时间:2019-05-08 14:32:55

标签: javascript angular angular-routing

我正在从用户帖子中设置路由,但是由于帖子和用户网址不同,因此我无法对其进行正确设置以路由具有帖子的userid的正确网址。

  constructor(private http: HttpClient, private activatedRoute: ActivatedRoute) { }

  pathPosts: string = "https://jsonplaceholder.typicode.com/posts"
  pathUsers: string = "https://jsonplaceholder.typicode.com/users"
  posts: Post[];
  users: User[];

  ngOnInit() {
    this.getUsers();
    this.activatedRoute.params.subscribe(params => {
      this.getPosts(params[userid]);
    })
  }

  getPosts(userid: string) {
    if (userid) {
      this.http.get<Post[]>(this.pathPosts + "posts?userid=" + userid).subscribe(response => {
        this.posts = response;
      });
    } else {
      this.http.get<Post[]>(this.pathPosts).subscribe(response => {
        this.posts = response;
      });
    }

  }

  getUsers() {
    this.http.get<User[]>(this.pathUsers).subscribe(response => {
      this.users = response;
    })
  }

src / app / post / post.component.ts(24,28)中的错误:错误TS2304:找不到名称“用户ID”。

1 个答案:

答案 0 :(得分:1)

您可以使用params['userid']params[userid]