Utku Çam
Utku Çam

Reputation: 29

How to fix this route problem in angular?

I'm setting up routes from my userposts but because posts and users url's are diffrent i can't set it correct up to route correct url of userid who has the post.

  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;
    })
  }

ERROR in src/app/post/post.component.ts(24,28): error TS2304: Cannot find name 'userid'.

Upvotes: 0

Views: 41

Answers (1)

ahmeticat
ahmeticat

Reputation: 1939

You can use params['userid'] in order to params[userid]

Upvotes: 1

Related Questions