我有一个组件,其中有flagPerfil
变量,我需要将此变量发送到我的模块。
我的服务
export class ListUserService {
public flagPerfil = boolean;
changeFlagPerfil() {
console.log('EEE' + this.flagPerfil);
this.flagPerfil ? this.flagPerfil = false : this.flagPerfil = true;
}
组件类:
@Component({
selector: 'app-list-user',
templateUrl: './list-user.component.html',
styleUrls: ['./list-user.component.scss'],
providers: [ListUserService]
})
export class ListUserComponent implements OnInit {
private flagPerfil = false;
getFlag() {
return this.flagPerfil;
}
这是视图 list-user.component.html
<span *ngIf="!flagPerfil">
<input type="button" id="usuarios" name="more" (click)="changeFlagPerfil()"
value="Amigos">
</span>
我有一个模块,我想在模块中注入服务,因为
我需要FlagProfile
变量
@NgModule({
imports: [
IonicModule,
CommonModule,
FormsModule,
RouterModule.forChild([{path: '', component: Tab1Page}])
],
declarations: [Tab1Page, ListUserComponent, FriendsComponent]
})
export class Tab1PageModule {
public flagPerfil: boolean;
constructor(private listUserService: ListUserService) {
this.flagPerfil = listUserService.flagPerfil;
console.log('FLAG', listUserService.flagPerfil);
}
}
如何使用事件发射?
我需要模块自动听到该变量的变化
答案 0 :(得分:2)
export class ListUserService {
public flag$ = new Subject< boolean >();
}
export class Tab1PageModule {
public flagPerfil: boolean;
constructor(private listUserService: ListUserService) {
listUserService.$flags.subscribe((flag)=>{
console.log('FLAG', flag;
this.flagPerfil = flag;
});
}
}
export class ListUserComponent implements OnInit {
private flagPerfil = false;
getFlag() {
return this.flagPerfil;
}
constructor(private listUserService: ListUserService) {
}
functionThatGetsCalledWhenTheFlagIsChanged(){
this.listUserService.flags$.next(this.flagPerfil);
}
}
答案 1 :(得分:1)