Angular: 配置 proxy 解决跨域

配置 proxy.config.json

{
  "/api": {
    "target": "http://localhost:3000",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true,
    "pathRewrite": {
      "^/api": ""
    }
  }
}

在 angular.json 中配置 proxy

"serve": {
     "builder": "@angular-devkit/build-angular:dev-server",
     "options": {
       "browserTarget": "min-wyy:build",
       "proxyConfig": "proxy.config.json"
     },
	...
}

service.module.ts

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 { }


你可能感兴趣的:(angular.js,前端,javascript)