【问题标题】:this._nativeError is undefined when I bundle my angular 2 application当我捆绑我的 Angular 2 应用程序时 this._nativeError 未定义
【发布时间】:2017-01-17 12:42:47
【问题描述】:

我正在使用gulpbrowserifytsify 来捆绑我的应用程序,并且我将browserify 指向main.ts 文件。这就是我捆绑应用程序所做的一切。但是,当我尝试运行已导入捆绑 js 文件的 index.html 文件时,我收到错误:this._nativeError is undefined 出现空白屏幕。

我的tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc-e2e",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ]
  }
}

我的吞咽任务:

gulp task

【问题讨论】:

  • 你使用的是哪个版本的zonejs
  • 区域版本为 0.7.5
  • 降级到 0.7.4 7.5 中有一个 bug github.com/angular/zone.js/issues/595
  • 我会检查的。但是,该应用程序在开发模式下运行良好,我使用 angular-cli 提供服务。
  • 不看真实代码就很难判断

标签: angular gulp browserify tsify


【解决方案1】:

关于tsify 配置,您定义:

defaultOptions = {
  ...
  tsifyOptions: {},
  ...
}

但你使用:

.plugin(tsify, defaultOptions.tsifydefaultOptions)

目前尚不清楚它是否与您的问题有关,但是如果您的tsconfig.json 与您的package.json 不在同一目录中并且不在父目录中,则不会找到它。由于您的配置包含../node_modules/@types,因此它似乎不是。

要让tsify 知道tsconfig.json 在哪里,您可以指定project 选项:

.plugin(tsify, { project: './some-directory/tsconfig.json' })

【讨论】:

  • 我不明白你想用../node_modukles/@types 提出什么建议。你能告诉我更多的细节吗?
  • 只是如果你的tsconfig.json 里面有那个路径,我猜它和你的package.json 不在同一个目录中 - 这是tsify 开始查找的目录一个。这没有问题,但是需要告诉tsify 它在哪里,仅此而已。 project 选项应设置为您的tsconfig.json 的路径相对 到您的package.json
  • 同样,这可能无法解决您的问题,但您需要解决的第一件事是experimentalDecorators 错误,因为这意味着tsify 找不到您的tsconfig.json
  • `导入'./polyfills.ts'; ^ ParseError: 'import' 和 'export' 可能只出现在 'sourceType: module' `这是我在将 tsify 指向 tsconfig 文件后得到的结果。
  • 我认为以这种方式调试构建是不可能的。有太多的未知数。但是,如果您从node_modules 导入.ts 文件,您还应该为tsify 指定global: true 选项。您应该认真考虑使用angular-cli 工具来构建捆绑包。 ...
【解决方案2】:

我遇到了同样的问题,并按照此处的建议将 console.log 放入 compiler.umd.js 中找到了我的解决方案: https://stackoverflow.com/a/41644735/7434393

在 compiler.umd.js 的第 1604 行附近,我添加了一个 console.log 来显示 我的消息。

它是 set 函数的一部分 Object.defineProperty(BaseError.prototype, "message" 定义。

setter 将消息分配给 _nativeError.message,但是 _nativeError 是未定义的,因此,它被炸毁了,隐藏了 REAL 错误消息。

所以,我只是放了一个 console.log 来吐出正在发生的消息 传入,它向我显示了我遇到的实际错误(模板错误)。

因此,这似乎是他们代码中的一个错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多