首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带参数的Angular 2+ http-get

带参数的Angular 2+ http-get
EN

Stack Overflow用户
提问于 2020-05-08 16:08:15
回答 1查看 17关注 0票数 0

我想创建一个get方法,它有多个参数。在我的示例中,它只需要一个参数,但我需要再插入一个参数。在后端代码段中,你可以看到,我想要传递第二个参数,但我不知道该怎么做。我试着做辅助治疗,但不起作用。

我想要传递creater_group (它可以工作)和第二个参数作为created_by。

组件:

代码语言:javascript
复制
ngOnInit(): void {

    const creater_group = localStorage.getItem('usergroup');
    const created_by = localStorage.getItem('user')
        this.getMessageNotifications(creater_group, created_by)
  }

getMessageNotifications(creater_group) {
    console.log(creater_group)
    this.notificationService.getMessageNotifications(creater_group).subscribe(response => {
      this.notifications = response.notification;
      console.log(this.notifications);
    }) 
  }

服务:

代码语言:javascript
复制
 getMessageNotifications(creater_group){
    return this.http.get(BASE_URL + 'messagenotifications/' + creater_group).pipe(
          map(res => res.json()))
}

后台: Nodejs。

代码语言:javascript
复制
function getMessageNotifications(req, res, next) {
    var creater_group = String(req.params.creater_group); 
    db.any(
      `SELECT 
          message_wall.message, 
          message_wall.created_at,
          message_wall.id,
          message_wall.seen,
          message_wall.creater_group,
          message_wall.created_by,
          users.logout_date
      FROM 
          message_wall, 
          users,  
      WHERE  
          message_wall.creater_group != $1 
      AND 
          user.username = $2

          `, creater_group
      )  
    .then(function (notification) {
        res.status(200)
          .json({
            status: 'success',
            notification: notification,
            message: 'Retrieved notifications'
          });
      })
      .catch(function (err) {
        return next(err);
      });
  }
EN

回答 1

Stack Overflow用户

发布于 2020-05-08 16:19:37

你可以试试这个

组件

代码语言:javascript
复制
    ngOnInit(): void {
    const creater_group = localStorage.getItem('usergroup');
    const created_by = localStorage.getItem('user')
        this.getMessageNotifications(creater_group, created_by)}

getMessageNotifications(creater_group,created_by) {
    this.notificationService.getMessageNotifications(creater_group,created_by).subscribe(response => {
      this.notifications = response.notification;
      console.log(this.notifications);
    }) 
  }

服务

代码语言:javascript
复制
getMessageNotifications(creater_group,created_by){
return this.http.get(BASE_URL + 'messagenotifications/' + creater_group +'/'+ created_by).pipe(
      map(res => res.json()))}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61674767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档