cuốn sách gpt4 ai đã làm

(Essence) 05/08/2020 Việc sử dụng luồng dữ liệu không đồng bộ Angular RxJS

In lại Tác giả: Người biết Thời gian cập nhật: 2024-03-13 05:33:21 31 4
mua khóa gpt4 Nike

nhập { Thành phần, OnInit } từ '@angular/core'; nhập { CommonService } từ '../../services/common.service'; nhập { Có thể quan sát } từ 'rxjs'; rxjs/operators'; @Component({ selector: 'app-home', templateUrl: './home.comComponent.html', styleUrls: ['./home.comComponent.less'] }) lớp xuất HomeComponent triển khai OnInit { constructor(public common: CommonService) { } //Đăng ký dịch vụ ngOnInit(): void { //1. Gọi lại không đồng bộ this.common.getDatacb( (res) => { console.log(res) }); //2. Promise nhận được this.common.getPromise().then((res) => { console.log(res); }) //3. rxjs lấy dữ liệu không đồng bộ var rxData = this.common.geRxjstData(); var p1 = rxData.subscribe(res => { console.log(res); }) // 4. rxjs lấy dữ liệu không đồng bộ và thực thi this.common.getRxIntervalData().subscribe(res => { console.log(res); }) // 5. Hoàn tác hành động sau một giây setTimeout(() => { p1.unsubscribe(); }, 1000) //6. pipe var stream = this.common.streamFun(); val % 2 == 0), //Filter map(value => { return value * value; // Xử lý dữ liệu}) ) .subscribe(res => { console.log(res); }) } }
nhập { Tiêm } từ '@angular/core'; nhập { Có thể quan sát } từ "rxjs";//js thư viện @Injectable({ProvideIn: 'root' }) xuất lớp CommonService { constructor() { } ///Hàm gọi lại getDatacb(cb) { setTimeout(() => { cb('getCBdata') }, 1000) } //promise gets getPromise() { return new Promise((resolve, từ chối) => { setTimeout(() => { Resolve('Zhang San----promise'); // giải quyết('Zhang San----promise1'); // Resolve('Zhang San----promise2'); }, 1000) }) } //3. rxjs lấy dữ liệu không đồng bộ geRxjstData() { return new Observable(observer => { setTimeout(()) => { var username = 'Xiao Ming-- rxjs' Observer.next(username); }, 3000) }) } //4. rxjs thực thi getRxIntervalData() nhiều lần { return new Observable(observer => { var count = 0 ; setInterval(() => { count++; var username = 'Xiao Ming--rxjs-Interval' Observer.next(username + count); }, 1000) }) } // hứa: không có lệnh thu hồi và thực thi nhiều lần, nhưng Rxjs có streamFun() { return new Observable(observer => { let count = 0; setInterval(()) => { người quan sát.next(đếm++ }, 1000);
31 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress