You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
910 B
39 lines
910 B
3 days ago
|
import { Injectable } from '@angular/core'
|
||
|
import { HttpClient } from '@angular/common/http'
|
||
|
import { Observable, of, throwError } from 'rxjs'
|
||
|
import { tap, catchError } from 'rxjs/operators'
|
||
|
import { WebSiteConfig } from '@cdk/public-api'
|
||
|
|
||
|
@Injectable({
|
||
|
providedIn: 'root',
|
||
|
})
|
||
|
export class ConfigService {
|
||
|
private configUrl = 'assets/config.json'
|
||
|
private appConfig: WebSiteConfig | null = null
|
||
|
|
||
|
constructor(private http: HttpClient) {}
|
||
|
|
||
|
loadConfig(): Observable<WebSiteConfig> {
|
||
|
if (this.appConfig) {
|
||
|
return of(this.appConfig)
|
||
|
}
|
||
|
|
||
|
console.log(123)
|
||
|
|
||
|
return this.http.get<WebSiteConfig>(this.configUrl).pipe(
|
||
|
tap((config) => {
|
||
|
this.appConfig = config
|
||
|
}),
|
||
|
catchError((error) => {
|
||
|
console.error('加载 config.json 失败:', error)
|
||
|
|
||
|
return throwError(() => new Error('无法加载应用配置'))
|
||
|
}),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
getConfig(): WebSiteConfig | null {
|
||
|
return this.appConfig
|
||
|
}
|
||
|
}
|