公共服务
ng g service services/stroage
import { StorageService } from './services/storage.service'
providers: [
StorageService
],
公共方法
- 创建公共方法
在 storage.service.ts
下
export class StorageService {
func() {
console.log('this is service');
}
}
import { StorageService } from '../../services/storage.service'
constructor(public storage: StorageService) {
this.storage.func()
}
公共变量
- 创建公共方法
在 storage.service.ts
下
export class StorageService {
str: string
set (val) {
this.str = val
}
get () {
return this.str
}
}
import { StorageService } from '../../services/storage.service'
constructor(public storage: StorageService) {
this.storage.set('str')
console.log(this.storage.get())
}