迁移后的模板错误 Angular 6 -> 9
Template errors after migration Angular 6 -> 9
我刚刚从 Angular 6 迁移到 Angular 9,最重要的依赖项之一无法在 Angular 6.
上使用
我按照 ng update
中的步骤操作:
ng update
ng update rxjs
ng update @angular/cli
ng update @angular/core
问题是我现在有很多模板错误,都涉及相同的问题。
No directive found with exportAs 'ngModel'
No directive found with exportAs 'ngForm'
我在网上找不到任何与 v9 上的这些错误相关的内容(仅在 v2 上)。
以下是与错误相关的一些部分:
- 关于
ngModel
的错误
<input type="text" class="form-control style-input-size" name="UserUpdatedUsername" [(ngModel)]="UserUpdated.username" #myKronozUserUpdatedUsername="ngModel" mdbActive required>
- 关于
ngForm
的错误
<form #editUserForm="ngForm" (ngSubmit)="editUserForm.valid && saveUpdateUser()" novalidate class="style-form-edit-user">
FormsModule 在 app.module.ts
中很好地导入(这是应用程序内的唯一模块)。
app.module.ts:
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [FormsModule]
})
export class AppModule {
}
package.json:
"dependencies": {
"@agm/core": "^1.1.0",
"@angular/animations": "^9.0.5",
"@angular/common": "^9.0.5",
"@angular/core": "^9.0.5",
"@angular/forms": "^9.0.5",
"@angular/platform-browser": "^9.0.5",
"@angular/platform-browser-dynamic": "^9.0.5",
"@angular/platform-server": "^9.0.5",
"@angular/router": "^9.0.5",
"@ngx-translate/core": "^10.0.2",
"@ngx-translate/http-loader": "^3.0.1",
"angular-csv-files": "^1.0.0",
"chart.js": "2.5.x",
"classlist.js": "1.1.x",
"core-js": "2.4.x",
"crypto-js": "^3.1.9-1",
"easy-pie-chart": "2.1.x",
"font-awesome": "4.7.x",
"hammerjs": "2.0.x",
"jarallax": "^1.10.3",
"moment-timezone": "^0.5.21",
"ng-html-util": "1.0.x",
"ng-recaptcha": "^4.3.0",
"ng-uikit-pro-standard": "/!\ sensible value /!\",
"ng2-file-upload": "^1.3.0",
"ngx-facebook": "^2.4.0",
"ngx-pagination": "^3.2.1",
"node-sass": "^4.13.1",
"rxjs": "^6.5.4",
"rxjs-compat": "^6.3.3",
"screenfull": "3.3.x",
"smoothscroll-polyfill": "0.3.x",
"tslib": "^1.10.0",
"web-animations-js": "^2.3.2",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular/cli": "^9.0.5",
"@angular/compiler-cli": "^9.0.5",
"@angular/language-service": "^9.0.5",
"@angular/compiler": "^9.0.5",
"@types/jasmine": "2.5.38",
"@types/node": "^12.11.1",
"codelyzer": "^5.1.2",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.0.0",
"karma-cli": "~1.0.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-coverage-istanbul-reporter": "^0.2.0",
"protractor": "~5.1.2",
"ts-node": "~3.2.0",
"tslint": "~5.7.0",
"typescript": "~3.7.5",
"webpack": "^4.3.0",
"@angular-devkit/build-angular": "~0.900.5"
}
我是否遗漏了一些关于迁移的信息?
谢谢。
你收到错误是因为在 Angular 9 中删除了仅模板变量。这样做是因为它们被键入为 any
并允许突变。通常,这是一种反模式。今后,您的元素将是强类型的。
在 Angular 版本 <= 8 中,以下代码有效。 #TranslateBtn
你可以引用 translate
属性 因为它被输入为 any
.
<button
#translateBtn
(click)="translateBtn.translate = !translateBtn.translate"
>
Translate
</button>
在Angular 9 中,#translateBtn
将是HTMLButtonElement
类型,它没有translate
属性。相反,您可以用在组件上调用函数的事件替换该功能。
HTML:
<button (click)="ontTranslate()">
Translate
</button>
TS:
export class PeopleListComponent {
translateToWookiee = false;
onTranslate(): void {
this.translateToWookiee = !this.translateToWookiee;
}
}
我的回答来自这个博客 article 来自 Brian Love,Google Angular 和 Web 技术的开发专家。
建议按照官方指导:https://update.angular.io/.
建议先更新到版本8,再更新到版本9。
遵循此模式,应使用 Angular 原理图工具自动处理所有不兼容性。
我刚刚从 Angular 6 迁移到 Angular 9,最重要的依赖项之一无法在 Angular 6.
上使用我按照 ng update
中的步骤操作:
ng update
ng update rxjs
ng update @angular/cli
ng update @angular/core
问题是我现在有很多模板错误,都涉及相同的问题。
No directive found with exportAs 'ngModel'
No directive found with exportAs 'ngForm'
我在网上找不到任何与 v9 上的这些错误相关的内容(仅在 v2 上)。
以下是与错误相关的一些部分:
- 关于
ngModel
的错误
<input type="text" class="form-control style-input-size" name="UserUpdatedUsername" [(ngModel)]="UserUpdated.username" #myKronozUserUpdatedUsername="ngModel" mdbActive required>
- 关于
ngForm
的错误
<form #editUserForm="ngForm" (ngSubmit)="editUserForm.valid && saveUpdateUser()" novalidate class="style-form-edit-user">
FormsModule 在 app.module.ts
中很好地导入(这是应用程序内的唯一模块)。
app.module.ts:
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [FormsModule]
})
export class AppModule {
}
package.json:
"dependencies": {
"@agm/core": "^1.1.0",
"@angular/animations": "^9.0.5",
"@angular/common": "^9.0.5",
"@angular/core": "^9.0.5",
"@angular/forms": "^9.0.5",
"@angular/platform-browser": "^9.0.5",
"@angular/platform-browser-dynamic": "^9.0.5",
"@angular/platform-server": "^9.0.5",
"@angular/router": "^9.0.5",
"@ngx-translate/core": "^10.0.2",
"@ngx-translate/http-loader": "^3.0.1",
"angular-csv-files": "^1.0.0",
"chart.js": "2.5.x",
"classlist.js": "1.1.x",
"core-js": "2.4.x",
"crypto-js": "^3.1.9-1",
"easy-pie-chart": "2.1.x",
"font-awesome": "4.7.x",
"hammerjs": "2.0.x",
"jarallax": "^1.10.3",
"moment-timezone": "^0.5.21",
"ng-html-util": "1.0.x",
"ng-recaptcha": "^4.3.0",
"ng-uikit-pro-standard": "/!\ sensible value /!\",
"ng2-file-upload": "^1.3.0",
"ngx-facebook": "^2.4.0",
"ngx-pagination": "^3.2.1",
"node-sass": "^4.13.1",
"rxjs": "^6.5.4",
"rxjs-compat": "^6.3.3",
"screenfull": "3.3.x",
"smoothscroll-polyfill": "0.3.x",
"tslib": "^1.10.0",
"web-animations-js": "^2.3.2",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular/cli": "^9.0.5",
"@angular/compiler-cli": "^9.0.5",
"@angular/language-service": "^9.0.5",
"@angular/compiler": "^9.0.5",
"@types/jasmine": "2.5.38",
"@types/node": "^12.11.1",
"codelyzer": "^5.1.2",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.0.0",
"karma-cli": "~1.0.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-coverage-istanbul-reporter": "^0.2.0",
"protractor": "~5.1.2",
"ts-node": "~3.2.0",
"tslint": "~5.7.0",
"typescript": "~3.7.5",
"webpack": "^4.3.0",
"@angular-devkit/build-angular": "~0.900.5"
}
我是否遗漏了一些关于迁移的信息? 谢谢。
你收到错误是因为在 Angular 9 中删除了仅模板变量。这样做是因为它们被键入为 any
并允许突变。通常,这是一种反模式。今后,您的元素将是强类型的。
在 Angular 版本 <= 8 中,以下代码有效。 #TranslateBtn
你可以引用 translate
属性 因为它被输入为 any
.
<button
#translateBtn
(click)="translateBtn.translate = !translateBtn.translate"
>
Translate
</button>
在Angular 9 中,#translateBtn
将是HTMLButtonElement
类型,它没有translate
属性。相反,您可以用在组件上调用函数的事件替换该功能。
HTML:
<button (click)="ontTranslate()">
Translate
</button>
TS:
export class PeopleListComponent {
translateToWookiee = false;
onTranslate(): void {
this.translateToWookiee = !this.translateToWookiee;
}
}
我的回答来自这个博客 article 来自 Brian Love,Google Angular 和 Web 技术的开发专家。
建议按照官方指导:https://update.angular.io/.
建议先更新到版本8,再更新到版本9。
遵循此模式,应使用 Angular 原理图工具自动处理所有不兼容性。