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

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

问题描述

标题几乎总结了它.我有一个在 ng serve 上被命中的 proxy.conf.js 文件,我只想从环境 ts 文件或 json 文件中获取目标 url,或者我真的不在这一点上,我只想在一个地方存储分别提供给其他人的 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

出于某种原因,我显然不明白(特别是因为智能感知为文件和属性填充了路径)我在 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

ADDENDUM :因此 environment.*.ts 文件位于 /src/environments 中,就像任何默认安装一样.proxy.conf.js 与其他默认文件(如 package.json 或 angular.json)位于项目根目录中.路径都是正确的,intellisense 甚至会填写值,但无论出于何种原因,proxy.conf.js 在从环境中导入 const 对象时都不会很好地发挥作用(在构建时,尽管就像我说的,intellisense 发现一切都很好).所以就像标题所传达的那样,我遇到的唯一问题是从环境 ts 文件中将 url 读入代理 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.

推荐答案

所以这里的问题原来是 typescript 的版本和缺少 import vs require 支持.如果您使用的是 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 serve 设置 proxy.config.js 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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