我正在尝试使用 gulp 任务从 JSON 配置创建一个 typescript 类。
input
{
"ApiEndpoint": "http://localhost:5000/api"
}
希望得到像
这样的输出 public static get ApiEndpoint(): string {
return "http://localhost:5000/api";
}
尝试使用 gulp-ts-config 但它在 template.ts 中抛出错误。不知何故,我无法让它在我的系统中工作。是否有任何其他 gulp 插件可以通过我们实现相同的目的?
最佳答案
我的包.json
{
"devDependencies": {
"gulp": "3.9.1",
"gulp-ts-config": "1.0.15"
}
}
我的 gulpfile.js
var gulp = require('gulp');
var gulpTsConfig = require('gulp-ts-config');
gulp.task('test', function () {
gulp.src('appsettings.json')
.pipe(gulpTsConfig('AppSettings'))
.pipe(gulp.dest('Scripts'))
});
appSettings.json
{
"ApiEndpoint": "http://localhost:5000/api"
}
运行任务后创建的 typescript
export class AppSettings {
public static get ApiEndpoint(): string {
return "http://localhost:5000/api";
}
}
引用文献:https://www.npmjs.com/package/gulp-ts-config
如果我更改我的 appSettings.json 以包含多个对象以转换为 typescript 函数
{
"ApiEndpoint": "http://localhost:5000/api",
"HelloWorld": "HelloWorld"
}
结果ts文件
export class AppSettings {
public static get ApiEndpoint(): string {
return "http://localhost:5000/api";
}
public static get HelloWorld(): string {
return "HelloWorld";
}
}
您从 gulp-ts-config 收到的具体错误是什么?我正在使用带有 npm 版本 5.5.1 和节点版本 v8.9.3 的 Visual Studio 2017
关于typescript - 使用 Gulp 将 JSON 配置到 TypeScript 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47984798/