ng g service services/xxx 创建services目录下的xxx服务
创建
app.module.ts
引入 服务 申明服务
import { StorageService } from './services/storage.service';
providers: [StorageService],
在需要服务的组件 xx.component.ts文件中引入服务 构造器中通过参数传递实例化
import { StorageService } from '../../services/storage.service';
//注意文件的位置
constructor(public storage: StorageService) { this.storage.test();//storage.service.ts文件中定义的方法 }