升级Angular9 报错 Uncaught TypeError: Cannot set property ɵfac of function XXX which has only a getter,具体如下:
ng-zorro-antd-mobile.js:10807 Uncaught TypeError: Cannot set property ɵfac of function ToastService(_appRef, _cfr, _zone) {
this._appRef = _appRef;
this._cfr = _cfr;
...<omitted>... } which has only a getter
at ng-zorro-antd-mobile.js:10807
at Module../node_modules/ng-zorro-antd-mobile/__ivy_ngcc__/fesm5/ng-zorro-antd-mobile.js (ng-zorro-antd-mobile.js:10818)
at __webpack_require__ (bootstrap:84)
at Module../src/app/login/login.component.ts (login.component.ts:1)
at __webpack_require__ (bootstrap:84)
at Module../src/app/app-routing.module.ts (app-routing.module.ts:1)
at __webpack_require__ (bootstrap:84)
at Module../src/app/app.module.ts (app.module.ts:1)
at __webpack_require__ (bootstrap:84)
at Module../src/main.ts (main.ts:1)
原因
升级Angular 9 后默认开启Ivy enableIvy=true,
在这个情况下使用es5是会报错的。我们需要把es5换成es2015
在tsconfig.json将 "target": "es5"
换为"target": "es2015"
,然后重新编译,运行成功
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}