配置 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 { }
文章来源地址https://www.toymoban.com/news/detail-817632.html
文章来源:https://www.toymoban.com/news/detail-817632.html
到了这里,关于Angular: 配置 proxy 解决跨域的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!