升级Angular9 报错 Uncaught TypeError: Cannot set property ɵfac of function XXX which has only a getter,具体如下:

  1. ng-zorro-antd-mobile.js:10807 Uncaught TypeError: Cannot set property ɵfac of function ToastService(_appRef, _cfr, _zone) {
  2. this._appRef = _appRef;
  3. this._cfr = _cfr;
  4. ...<omitted>... } which has only a getter
  5. at ng-zorro-antd-mobile.js:10807
  6. at Module../node_modules/ng-zorro-antd-mobile/__ivy_ngcc__/fesm5/ng-zorro-antd-mobile.js (ng-zorro-antd-mobile.js:10818)
  7. at __webpack_require__ (bootstrap:84)
  8. at Module../src/app/login/login.component.ts (login.component.ts:1)
  9. at __webpack_require__ (bootstrap:84)
  10. at Module../src/app/app-routing.module.ts (app-routing.module.ts:1)
  11. at __webpack_require__ (bootstrap:84)
  12. at Module../src/app/app.module.ts (app.module.ts:1)
  13. at __webpack_require__ (bootstrap:84)
  14. at Module../src/main.ts (main.ts:1)

原因

升级Angular 9 后默认开启Ivy enableIvy=true,
在这个情况下使用es5是会报错的。我们需要把es5换成es2015
在tsconfig.json将 "target": "es5"换为"target": "es2015",然后重新编译,运行成功

  1. {
  2. "compileOnSave": false,
  3. "compilerOptions": {
  4. "baseUrl": "./",
  5. "outDir": "./dist/out-tsc",
  6. "sourceMap": true,
  7. "declaration": false,
  8. "downlevelIteration": true,
  9. "experimentalDecorators": true,
  10. "module": "esnext",
  11. "moduleResolution": "node",
  12. "importHelpers": true,
  13. "target": "es2015",
  14. "typeRoots": [
  15. "node_modules/@types"
  16. ],
  17. "lib": [
  18. "es2018",
  19. "dom"
  20. ]
  21. },
  22. "angularCompilerOptions": {
  23. "fullTemplateTypeCheck": true,
  24. "strictInjectionParameters": true
  25. }
  26. }