angular 服务
1. 创建 ng g s service/list
2.在app.module.ts中引入
import { ListService } from './service/list.service'; @NgModule({ ... providers: [ListService], })
3.在组件中使用
import { ListService } from 'src/app/service/list.service'; list:Array<string> = []; constructor(private listService:ListService=new ListService() ) { } ngOnInit(): void { this.list = this.listService.getList(); } clickHandle(){ this.listService.addList('node.js') }