配置 proxy.config.json
typescript
{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"logLevel": "debug",
"changeOrigin": true,
"pathRewrite": {
"^/api": ""
}
}
}
在 angular.json 中配置 proxy
typescript
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "min-wyy:build",
"proxyConfig": "proxy.config.json"
},
...
}
service.module.ts
typescript
import { isPlatformBrowser } from '@angular/common';
import { InjectionToken, NgModule, PLATFORM_ID } from '@angular/core';
import { httpInterceptorProvides } from './http-interceptors';
export const API_CONFIG = new InjectionToken('ApiConfigToken');
export const WINDOW = new InjectionToken('WindowToken');
@NgModule({
declarations: [],
imports: [
],
providers: [
{
provide: API_CONFIG,
useValue: '/api/' // useValue: 'http://localhost:3000/'
},
...
]
})
export class ServicesModule { }