更新的问题:
好吧,调试我注意到这个值只在这里更新:
this._homeService.getClientInfo(this.user.customer_uuid).subscribe(
(res) => {this._customerService.setCustomer(this.getCustomerFromResponse(res)).subscribe()},
(err) => {console.error(err)}
);
函数getCustomerFromResponse(response)返回一个客户对象,我也可以在那里做一个新客户或者我想要传递的值。但是在我的旧帖子(后面)的示例中调用函数setCustomer(customer),客户不会被传递给observable的值。为什么呢?
旧帖子: 我正在与可观察者一起工作,但我根本不会断言它们。我现在面临这个问题,但我经常遇到问题。我有这项服务:
import {Injectable} from "@angular/core";
import {Customer} from "./customer";
import {Observable} from "rxjs/Rx";
import "rxjs/add/operator/map";
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
@Injectable()
export class CustomerService {
private customer: Customer;
private customer$: Observable<Customer>;
constructor() {
this.customer = new Customer;
this.customer$ = Observable.of(this.customer);
}
setCustomer(customer: Customer) {
return this.customer$.do(val => this.customer = val);
}
getCustomer() {
return this.customer;
}
getObservable() {
return this.customer$;
}
}
我从类app.component调用getObservable()以便每次更新值:
this._customerService.getObservable().subscribe(
res => {this.customer = res})
我现在发布每个班级:
clients.component
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Response } from "@angular/http";
import { User } from '../../model/user';
import { Customer } from '../../model/customer';
import { UserService } from "../../model/user.service";
import { CustomerService } from "../../model/customer.service";
import { ClientsService } from './clients.service';
import { HttpWrapperService } from "../../shared/httpwrapper.service";
import { NotificationsService } from '../../shared/notifications.service';
import { Notification } from '../../shared/notifications.model';
方法:
customerSelect(customer: Customer) {
let user = this._userService.getUser();
user.customer_uuid = customer.uuid;
this._customerService.setCustomer(customer).subscribe();
this._userService.setUser(user).subscribe();
this.router.navigate(['/home']);
}
login.component
import {Component, Input, OnInit} from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
import { User } from '../../model/user';
import {UserService} from "../../model/user.service";
import { Customer } from '../../model/customer';
import {CustomerService} from "../../model/customer.service";
import {LoginService} from "./login.Service";
import {HttpWrapperService} from "../../shared/httpwrapper.service";
import {AuthService} from "../../shared/auth.service";
import { NotificationsService } from '../../shared/notifications.service';
import { Notification } from '../../shared/notifications.model';
方法:
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
if (params['flag'] == 0) {
let nullcustomer = new Customer();
nullcustomer.name = "";
this._customerService.setCustomer(nullcustomer).subscribe();
this._authService.logout();
}
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
仅供参考我正在使用angular core 2.0.0稳定版。因为我总是面对可观察的问题,如果有人能为我提供一个很好的链接教程,我只会为angular2的可观察对象提供帮助。我经常搜索,但找不到一个好的。
答案 0 :(得分:1)
我找到了解决方案,但我无法理解,如果有人能解释我为什么工作会更好。
首先,我必须得到当前客户,我无法创建新客户并清除所有属性。因此,在这两个地方(家庭和客户),功能将是:
formatCustomer(customerNew: Customer) {
let customer = this._customerService.getCustomer();
if (customer == null) {
customer = new Customer;
}
customer.prop1= customerNew.prop1;
customer.prop2= customerNew.prop2;
...
this._customerService.setCustomer(customer).subscribe();
}