Service服务
1、ng g s testService
2、服务必须有提供商,才可以外部使用
@Injectable({
providedIn: 'root'
})
如果只写了@Injectable(),则外部是不可以使用的
3、注册提供商的三种方式
通过@Injectable 的providedIn:'root'注册为根级提供商(全部组件皆可使用)
通过@NgModule的providers:[] 注册为模块内可用的提供商(在当前组件和引入了当前module的组件中使用)
通过@Component的providers:[]注册为组件的提供商(只有在当前组件和当前组件的子组件中使用)

浙公网安备 33010602011771号