
上QQ阅读APP看书,第一时间看更新
Service without dependencies
A service without dependencies is a service whose constructor is empty:
export Service {
constructor(){}
getData() {}
}
To use it, you simply type:
import { Service } from './service'
let service = new Service();
service.getData();
Any module that consumes this service will get their own copy of the code, with this kind of code. If you, however, want consumers to share a common instance, you change the service module definition slightly to this:
class Service {
constructor() {}
getData() {}
}
const service = new Service();
export default service;
Here, we export an instance of the service rather than the service declaration.