Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编译时正常,将typescript中命名空间中的类与webpack一起使用时出现运行时错误_Typescript_Webpack_Swagger Codegen - Fatal编程技术网

编译时正常,将typescript中命名空间中的类与webpack一起使用时出现运行时错误

编译时正常,将typescript中命名空间中的类与webpack一起使用时出现运行时错误,typescript,webpack,swagger-codegen,Typescript,Webpack,Swagger Codegen,我使用-l typescript选项生成REST消费者服务库。生成的代码如下所示(DefaultApi.ts): tsconfig.json: { "compilerOptions": { "baseUrl": "src/app", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false,

我使用
-l typescript
选项生成REST消费者服务库。生成的代码如下所示(
DefaultApi.ts
):

tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": "src/app",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "module": "commonjs"
  },
  "compileOnSave": false,
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "!typings/**",
    "!node_modules/**"
  ]
}

当前版本的swagger codegen TypeScript Angular generator未将DefaultApi封装在命名空间中


更新和重新生成。如果您有任何问题,请告诉我

您有两种选择来解决此问题,一种是轻松,另一种是复杂:

  • 更改生成的ts文件
  • 在生成的代码末尾添加以下代码:

    export = API.Client;
    
    现在,您可以在模块中使用
    import
    ,而不会出现问题,例如:

    import {DefaultApi} from './generated-code';
    
  • 但如果更改生成的文件不是一个选项,请求助于 想法:

    使用不同的tsconfig拆分模块代码和非模块代码。将模块化代码和非模块化代码与Salsa、webpack解析别名和javascript支持混合使用

    教程:

    TL;这里有一位博士正在应用这个解决方案

  • 创建一个新的tsconfig.generate.json来生成代码句柄,只需生成红色代码,例如:
  • 在初始tsconfig.json中,必须排除生成的ts文件。这将确保没有不必要的代码,例如:
  • 现在,洞里的王牌!您将添加一个
    api.js
    文件,并在
    tsconfig.generate.json
    中引用它。是的,这是一个js文件,萨尔萨就是在这里开始行动的。要做到这一点,您必须在tsconfig中启用
    allowJs
    功能
  • 这些文件基本上是通过commonjs导出生成的代码,而无需触摸它

    /// <reference path="./namespacing-code.ts" />
    // typescript compiler don't warning this because is Salsa!
    module.exports = API.Client;
    
    现在,您可以使用您的生成代码而无需触摸它:
    从“api”导入api

    如果有任何疑问,这里使用这种方法进行回购。
    我希望我已经帮了你

    你能分享你的网页配置和tsconfig吗?@JaganathanBantheswaran补充道。谢谢你看!在我看来,您使用的是typescript 2.x。您对Salsa和webpack resolve.alias解决方案的看法如下所述?嗯。。。我刚刚检查过,我使用的是2.2.1版,它似乎是最新版本()。好的。我错了。我现在看到这是angular 1.x typescript生成器,而不是angular 2。我建议与swagger codegen团队讨论一个问题,建议使用命令行选项来禁止添加名称空间错误。谢谢你的调查。
    import {DefaultApi} from './generated-code';
    
    {
       "compilerOptions": {
          "outFile": "module-generated-code.js"
       },
       "files": ["generated-code.ts"]
    }
    
    {
       "compilerOptions": {
    
       },
       "exclude": ["generated-code.ts"]
    }
    
    { 
       "compilerOptions": {
          "outFile": "module-generate-code.js", "allowJs": true
       },
       "files": ["generated-code.ts", "api.js"]
    }
    
    /// <reference path="./namespacing-code.ts" />
    // typescript compiler don't warning this because is Salsa!
    module.exports = API.Client;
    
    { //webpack.config
        resolve: {
          extensions: ['', '.webpack.js', '.web.js', '.ts', '.js'],
          alias:{ 'api':'./module-generated-code.js'
        }
    }
    
    { //tsconfig.json
        "compilerOptions": {
            "allowJs": true, //remember of enabling Salsa here too
            "baseUrl": ".",
            "paths": {
                "api":["api.js"] //it is just get type definitions from generated files
            }
         },