Angular observable sub和get对象不起作用

时间:2017-05-09 14:59:14

标签: angular

我试图获取对象的值而不是多次查询服务器。

但我错过了什么或做错了什么?

我在服务中有以下内容:

SELECT p.*
FROM products p
LEFT JOIN product_category pc_w1 ON pc_w1.product_id = p.prod_id AND pc_w1.category_id = 3
LEFT JOIN product_category pc_pt2 ON pc_pt2.product_id = p.prod_id AND pc_pt2.category_id = 1
LEFT JOIN product_category pc_rb ON pc_rb.product_id = p.prod_id AND pc_rb.category_id IN (11)
LEFT JOIN product_category pc_rb1 ON pc_rb1.product_id = p.prod_id AND pc_rb1.category_id IN (1,12)
WHERE
  pc_w1.product_id IS NOT NULL AND pc_pt2.product_id IS NOT NULL AND 
  (
    pc_rb.product_id IS NOT NULL AND
    pc_rb1.product_id IS NOT NULL 
  )
GROUP BY p.prod_id

有没有办法可以在另一个组件中像这样调用它?

  user$: Observable<any>;

  private userChangeSet = new Subject<any>();


  constructor(private http: Http, private router: Router) {
    this.user$ = this.userChangeSet.asObservable();
  }

 reloadUserData() {
    let    url = this.baseapi + '/getuser';
    return this.http.get( url)
        .map(t=>  {
          this.updateUser(t.json().user);
          return t.json();
        });
  }
 updateUser(object) {
    this.userChangeSet.next(object);
  }

当t.json()。user的结果为

if (this.customservice.user.cash < 3000) { } else { }

另外,为什么这不起作用?

{username: username, cash: 1}

因为这里说 this.customservice.user$.subscribe((value) => { this.xxx= value; }); 未定义

要信息。我在登录时调用reloagUserData。我不打算再打电话。我只是在调用用户变量。

1 个答案:

答案 0 :(得分:0)

我要指出的第一件事是你没有声明 user$: Observable<any>; private userChangeSet = new Subject<any>(); // You were missing this! :D constructor(private http: Http, private router: Router) { this.user$ = this.userChangeSet.asObservable(); }

private userData = this.reloadUserData(); // This returns {username: username, cash: 1} right?

if (this.userData.cash < 3000) {
    // Do stuff   
} else {
   // Do other stuff
}
  

有没有办法可以在另一个组件中调用它?

你很近:

this.reloadUserData().subscribe((data) => {
   this.userData = data; // Here we retrieve the data of the user and store in in our variable userData
   this.sharedService.updateUser(this.userData);  // Here we call the method of the shared service passing our user's data
   }
);

更新1:

如果您想将数据传递给正在收听的组件,请使用共享服务中创建的方法:

Observable

我们做的是:

  1. 我们称之为提供用户数据的方法
  2. 当我们收到数据时,我们称之为共享服务的方法,并将数据作为参数传递
  3. 当我们调用该方法时,我们触发updateUser(data)意味着订阅该方法的所有组件将触发并接收作为参数发送的数据。因此,如果我们调用user$,则会触发订阅@Injectable() export class CustomService { user$: Observable<any>; private userChangeSet = new Subject<any>(); constructor(private http: Http, private router: Router) { this.user$ = this.userChangeSet.asObservable(); } reloadUserData() { let url = this.baseapi + '/getuser'; return this.http.get( url) .map(t=> { this.updateUser(t.json().user); return t.json(); }); } updateUser(object) { this.userChangeSet.next(object); } } 的所有组件,并且他们将收到数据。
  4. 更新2:

    快速举例。

    共享服务:

    this.reloadUserData().subscribe((data) => {
       this.userData = data; // Receives the data
       this.sharedService.updateUser(this.userData);  // Sends the data to the shared service
       }
    );
    

    <强>的Component1:

    this.customservice.user$.subscribe((data) => {
          this.userData = data; // Is subscribed and listening, as soon as someone calls the method updateUser() from the shared service will get the data passed as parameter and will updated the value of userData
     });
    

    <强> COMPONENT2:

    async/await