0,把service中的数据传给组件:
![](https://img.haomeiwen.com/i8736531/df70a76e69cd01c2.png)
![](https://img.haomeiwen.com/i8736531/331106c1ee662d34.png)
1, 创建Service.ts file
--所有需要维护的数据
--一个数据对应一个EventEmitter
--对数据的set,get或其他操作
![](https://img.haomeiwen.com/i8736531/aff5d5ca2609c45f.png)
2,创建trigger component 该文件用于关联html和ts中的emit,html中绑定了onSetToActive方法,在onSetToActive方法中调用service中的event。
![](https://img.haomeiwen.com/i8736531/07eeb0dc55167a42.png)
![](https://img.haomeiwen.com/i8736531/267432bb0974a4dd.png)
3,trigger事件被创建, 接下来创建listen方法监听。
![](https://img.haomeiwen.com/i8736531/dd1678a94058af90.png)
用对应EventEmitter的subscribe方法监听,在subscribe方法中写监听到后,具体对service数据的操作,一般这个操作是写在service.ts中。
备注:
有关service的dependecy inject
1,创建service
2,引入module(import),引入provider,在construc中引入(别忘加private)
3,引入module(import),在construc中需要每用到一次都引入,但providers只要在最顶层引入即可(子组件都可以用)
![](https://img.haomeiwen.com/i8736531/5e2b53537e5cd5d0.png)
![](https://img.haomeiwen.com/i8736531/254f1ec84acc687b.png)
网友评论