创建服务命令:
ng g service my-new-service
创建到指定目录下面:
ng g service services/storage
可以在其他组件中使用服务。
//在service引入并配置服务
import { StorageService } from './services/storage.service';
//声明
providers: [StorageService],
//在其他组件 引入服务
import { StorageService } from '../../services/storage.service';
//初始化
constructor(public storage:StorageService) {
console.log(this.storage.get());
}
服务之间可以相互调用,