我的角度有问题,这里有两个部分:
我有一个UserNow
部分的属性,MyApp
我想UserNow
通过组件设置属性的值LoginPage
。怎么做?
我已经尝试过(但没有产生任何影响)
Import {MyApp} from '../../app/app.component';
@Component({
selector: 'page-login',
templateUrl: 'login.html'
})
export class LoginPage {
public app: any;
login() {
...
this.app = MyApp;
this.app.userNow = MyValue;
...
}
}
有几种方法可以做到这一点。
1)使用服务:服务通常在应用程序中只有一个实例,可用于在组件之间轻松共享数据。例如,创建服务userService并将其注入到要使用它的组件中。
2)使用Emit:Emit用于在应用程序中发出事件,并且可以采取相应的措施。
this.eventInChild.emit(data);
事件发射可以采取两种措施。
<child-component (eventInChild)="parentFunction($event)"></child-component>
在服务中它是这样的:
getEmitStatus() {
return this.eventInService;
}
//In component or service - to listen to event
this.subscription = this.userService.getEmitStatus()
.subscribe(item => {
//thing to do here
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句