创建服务命令:

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());
  }

服务之间可以相互调用,

posted on 2021-09-14 15:05  鬼灯  阅读(67)  评论(0编辑  收藏  举报