使用环境为ng服务设置proxy.config.js属性 [英] Use environment to set proxy.config.js property for ng serve

查看:767
本文介绍了使用环境为ng服务设置proxy.config.js属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题几乎总结了一下.我有一个ng serve命中的proxy.conf.js文件,我只想从环境ts文件或json文件中获取目标网址,或者我现在不在乎只希望一个地方存储分别为其他地方提供信息的URL ...

Title pretty much sums it up. I've got a proxy.conf.js file that gets hit on ng serve and I just want to grab the target url from the environment ts file, or a json file, or I don't really care at this point, I just want one place to store the URL that feeds the others respectively...

所以environment.congif.ts(任何配置中都不包含,只是一个文件,用于存储当前提供给environment.dev.ts且无法提供给proxy.conf.js的设置对象);

So environment.congif.ts (which is not included in any configurations, just a file to store the settings object that currently feeds environment.dev.ts and is failing to feed proxy.conf.js);

export const environment = {
    production: false,
    apiUrl: 'http://localhost:12345'
};

然后proxy.conf.js;

var settings = require('./src/environments/environment.config');

const PROXY_CONFIG = [{
    "/restapi/*": {
      "target": settings.environment.apiUrl,
      "secure": false,
      "changeOrigin": true,
      "logLevel": "debug"
    }
}]

module.exports = PROXY_CONFIG

出于某种原因,我显然不理解(特别是因为intellisense填写的路径对于文件和属性都很好),我在ng serve上收到错误;

Which for some reason I obviously don't understand (especially since intellisense fills in the path just fine for both the file and the property) I get error on ng serve;

Cannot find module './src/environments/environment.config'
Error: Cannot find module './src/environments/environment.config'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
........

所以我的问题是,为什么它不能以这种方式解决?我只希望一个文件在其上设置目标URL,从而从environment.config.ts

So my question is, why can it not resolve this way? I just want one file to set the target URL on that feeds the places it's needed from environment.config.ts

附加:因此,*.ts文件位于/src/environments中,与任何默认安装一样. proxy.conf.js与其他默认文件(如package.json或angular.json)位于项目根目录中.路径都是正确的,intellisense甚至会填充值,但是出于任何原因,从环境中导入const对象时proxy.conf.js都不能很好地工作(在构建时,尽管我说过intellisense会发现一切都很好).因此,就像标题所传达的那样,我唯一的问题是将URL从环境ts文件读取到代理js文件中,这样我只有一个文件可以维护apiurl.

ADDENDUM : So the environment.*.ts files sit in /src/environments like with any default installation. The proxy.conf.js sits in the project root with other default files like package.json or angular.json. The paths are all correct, intellisense will even fill in the values but for whatever reason proxy.conf.js will not play nice when importing the const object from the environment (at build, though like I said intellisense finds everything just fine). So just like the title conveys, the only issue I'm having is reading the url into the proxy js file from the environment ts file so that I have only one file to maintain the apiurl in.

推荐答案

因此,问题出在这里是打字稿的版本,缺少导入功能,需要支持.如果您使用的是类似v3的产品,则不会遇到相同的问题.感谢您的光临!

So the problem here turned out to be the version of typescript and its lack of import vs require support. If you're using anything like v3 you won't encounter the same issue. Thanks for looking!

这篇关于使用环境为ng服务设置proxy.config.js属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆