1. 設(shè)置proxy.config.json文件
{`
"/api"`: {//這里是前臺(tái)調(diào)用后端接口時(shí)做的代理標(biāo)識(shí)`
"target":"localhost:3100",
"logLevel":` `"debug",
"secure":false,
"changeOrigin":true,
"pathRewrite": {
"^/api":""
}
}
}
|
注意:pathRewrite 部分的配置,"pathRewrite": {"^/api": ""} 如果沒有這部分的配置,那在發(fā)送請(qǐng)求的時(shí)候,實(shí)際請(qǐng)求的地址將會(huì)是http://localhost:3100/api/actionapi/。相較于真實(shí)url,會(huì)多出/api這一部分。
2. 設(shè)置service的url
//這里的api表示代理標(biāo)識(shí)
//實(shí)際的訪問url應(yīng)該是:[http://localhost:3100/actionapi/](http://localhost:3100/actionapi/)
const wcfPath = '/api/actionapi/'``;
3 設(shè)置package.json文件
"scripts"``: {
"ng"``: "ng"``,
"build"``: "ng build --prod --aot --build-optimizer"``,
"start:dev"``: "ng serve --proxy-config proxy.conf.json --open"``,
"start:aot"``: "ng serve --prod --aot --proxy-config proxy.conf.json --open"``,
"test"``: "ng test"``,
"lint"``: "ng lint"``,
"e2e"``: "ng e2e"
}
再重新npm run start:dev啟動(dòng)一下項(xiàng)目,應(yīng)該就能啟用angular反向代理了。