angular 2构造函数内部服务工作另一个函数内部服务不工作
我的代码 component.ts页面:angular 2构造函数内部服务工作另一个函数内部服务不工作,angular,typescript,constructor,angular-services,Angular,Typescript,Constructor,Angular Services,我的代码 component.ts页面: import {Component, OnInit, NgModule, VERSION} from '@angular/core'; import {BrowserModule} from '@angular/platform-browser'; import { UserService } from "../services/user.service"; import {Router, ActivatedRoute, Params } from '
import {Component, OnInit, NgModule, VERSION} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import { UserService } from "../services/user.service";
import {Router, ActivatedRoute, Params } from '@angular/router';
@Component({
selector: 'app-all-users',
templateUrl: './all-users.component.html',
styleUrls: ['./all-users.component.css'],
providers: [ UserService ]
})
export class AllUsersComponent{
users; usersnew; limit; limitnew;
constructor(public userService: UserService, public router: Router, public route: ActivatedRoute) {
this.limit = "0";
this.limitnew = "0";
this.userService.getTestUsers(this.limit).subscribe( users => this.users = users);
}
LoadMore(){
this.limit = "12";
this.limitnew = +this.limitnew + +this.limit;
this.userService.getTestUsers(this.limitnew).subscribe( usersnew => this.usersnew = usersnew);
this.users = [...this.users , ...this.usersnew];
console.log(this.users);
}
}
html页面:
<div class="row">
<div class="col-sm-3 *ngFor="let user of users ;let i = index ">
<img *ngIf="user.image == ''" src="http://localhost/assets/images/user.png" class="user_img">
</div>
</div>
<button (click)="LoadMore()"> Load More </button>
首先,订阅只能进行一次。如果订阅一次,该函数将在每次都被激发,直到组件被销毁。因此,请从LoadMore函数中删除订阅
首先,您必须从以下角度/核心导入OnInit、OnDestroy lifcycle挂钩
import { Component, OnInit, OnDestroy } from '@angular/core';
在该内部组件之后,进行订阅
ngOnInit() {
this.subscription = this.userService.getTestUsers(this.limit).subscribe( users => this.users = users);
}
当组件被破坏时,不要忘记取消订阅
ngOnDestroy(){
this.subscription.unsubscribe();
}
希望这会有所帮助。尝试以下方法:
export class AllUsersComponent {
users: Array<any> = [];
usersnew: Array<any> = [];
limit; limitnew;
constructor(public userService: UserService, public router: Router, public route: ActivatedRoute) {
this.limit = "0";
this.limitnew = "0";
this.userService.getTestUsers(this.limit).subscribe(users => this.users = users);
}
LoadMore() {
this.limit = "12";
this.limitnew = +this.limitnew + +this.limit;
this.userService.getTestUsers(this.limitnew)
.subscribe((usersnew) => {
this.usersnew = usersnew
}, (error) => {
console.log('error', error);
}, () => {
this.done()
});
}
done() {
this.users = [...this.users, ...this.usersnew];
console.log(this.users);
}
}
导出类AllUsersComponent{
用户:数组=[];
usersnew:Array=[];
限制;限制新的;
构造函数(公共用户服务:用户服务,公共路由器:路由器,公共路由:ActivatedRoute){
此值为0;
此参数为new=“0”;
this.userService.getTestUsers(this.limit).subscribe(users=>this.users=users);
}
LoadMore(){
此值为0.limit=“12”;
this.limitnew=+this.limitnew++this.limit;
this.userService.getTestUsers(this.new)
.订阅((usersnew)=>{
this.usersnew=usersnew
},(错误)=>{
console.log('error',error);
}, () => {
这个
});
}
完成(){
this.users=[…this.users,…this.usersnew];
console.log(this.users);
}
}
请给出一个答案-“不工作”的确切含义是什么?在subscribe in loadMore方法中打印日志this.usersnew。如果您在subscribe方法中获得值,那么您的代码是正确的。错误是什么?subscribe inside的console.log,我获得值subscribe的替代值是什么订阅的替代值no alternates for subscribes。。!
export class AllUsersComponent {
users: Array<any> = [];
usersnew: Array<any> = [];
limit; limitnew;
constructor(public userService: UserService, public router: Router, public route: ActivatedRoute) {
this.limit = "0";
this.limitnew = "0";
this.userService.getTestUsers(this.limit).subscribe(users => this.users = users);
}
LoadMore() {
this.limit = "12";
this.limitnew = +this.limitnew + +this.limit;
this.userService.getTestUsers(this.limitnew)
.subscribe((usersnew) => {
this.usersnew = usersnew
}, (error) => {
console.log('error', error);
}, () => {
this.done()
});
}
done() {
this.users = [...this.users, ...this.usersnew];
console.log(this.users);
}
}