@ 专家- mx/ngx- services
通用的Angular服务,跨越了Expeteam的应用.
独立设置 (Angular v20+)
使用 provideNgxServices(environment) 在你身边 ApplicationConfig.providers。 。 。 .
import { provideHttpClient, withInterceptors } from '@angular/common/http'
import type { ApplicationConfig } from '@angular/core'
import { provideRouter } from '@angular/router'
import {
apiHeadersInterceptor,
apiTokenInterceptor,
httpCachingInterceptor,
provideNgxServices,
type Environment
} from '@experteam-mx/ngx-services'
import { environment } from '../environments/environment'
import { routes } from './app.routes'
const envs = {
...environment,
authCookie: 'token'
// cacheRoutes is opt-in; omit or leave empty until routes are registered:
// cacheRoutes: [
// { pattern: '/api/catalogs', ttl: 60_000 },
// { pattern: '/api/companies/.+/branches', ttl: 30_000 }
// ]
} as Environment
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideNgxServices(envs),
provideHttpClient(withInterceptors([
apiHeadersInterceptor,
apiTokenInterceptor,
httpCachingInterceptor
]))
]
}
provideNgxServices 登记册 ENVIRONMENT_TOKEN,这是 API 的服务和一些截取器在这个包中的要求.
httpCachingInterceptor 缓存只获取其 URL 匹配条目的请求 cacheRoutes (第一次匹配的 RegExp 模式获胜). 没有配置路径, 没有缓存 .
NgModule 兼容性
NgxServicesModule.forRoot(environment) 仍然可用于兼容性,但是它为新的独立应用程序而贬值,将在 20.2.0。 。 。 .
import { importProvidersFrom } from '@angular/core'
import { NgxServicesModule } from '@experteam-mx/ngx-services'
providers: [
importProvidersFrom([NgxServicesModule.forRoot(envs)])
]
偏爱 provideNgxServices(envs) 所有的新应用程序.
构建
ng build @experteam-mx/ngx-services
构建输出 :
dist/experteam-mx/ngx-services
发布
cd dist/experteam-mx/ngx-services
npm publish
测试
ng test @experteam-mx/ngx-services