Angular component property with type DataBindingDirective has no initializer and is not definitely assigned in the constructor

huangapple go评论63阅读模式
英文:

Angular component property with type DataBindingDirective has no initializer and is not definitely assigned in the constructor

问题

我遇到了编译错误:

类型为DataBindingDirective的属性没有初始化程序,并且在构造函数中没有明确分配。

@ViewChild(DataBindingDirective) dataBinding: DataBindingDirective;
~~~~~~~~~~~

我怀疑这可能与包版本有关,因为在StackBlitz上一切正常(工作示例:StackBlitz)。

更大的代码块:

export class NPEComponent implements OnInit {
  rowData$: Observable<Array<NPE>> = of([]);
  public filter$: Observable<CompositeFilterDescriptor | null>;
  @ViewChild(DataBindingDirective) dataBinding: DataBindingDirective;  // 编译错误在这里

  constructor(private activatedRoute: ActivatedRoute) {

    this.filter$ = this.activatedRoute.queryParams.pipe(
      map(params => params['name']),
      distinctUntilChanged(),
      map(searchTerm => {
        if (!searchTerm) return null;

        return (<CompositeFilterDescriptor>{
          logic: 'or',
          filters: [buildFilterItem('organizationName', searchTerm)]
        })
      });
  }

  ngOnInit(): void {
    this.rowData$ = this.NPEService.apiNPEGet();
  }

packages.json中的版本信息:

"dependencies": {
  "@angular/animations": "~13.3.5",
  "@angular/common": "~13.3.5",
  "@angular/compiler": "~13.3.5",
  "@angular/core": "~13.3.5",
  "@angular/forms": "~13.3.5",
  "@angular/localize": "~13.3.5",
  "@angular/platform-browser": "~13.3.5",
  "@angular/platform-browser-dynamic": "~13.3.5",
  "@angular/platform-server": "~13.3.5",
  "@angular/router": "~13.3.5",
  // 其他依赖项...
},
"devDependencies": {
  "@angular-devkit/build-angular": "~13.3.4",
  "@angular/cli": "~13.3.4",
  "@angular/compiler-cli": "~13.3.5",
  // 其他开发依赖项...
}

PowerShell中的版本信息:

> ng --version
Angular CLI: 13.3.10
Node: 18.13.0 (不受支持)
Package Manager: npm 8.19.3
OS: win32 x64
Angular: 13.3.12
Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1303.10
@angular-devkit/build-angular   13.3.10
@angular-devkit/core            13.3.10
@angular-devkit/schematics      13.3.10
@angular/cli                    13.3.10
@schematics/angular             13.3.10
rxjs                            7.5.7
typescript                      4.6.4
英文:

I'm getting compilation error:

Property with type DataBindingDirective has no initializer and is not definitely assigned in the constructor.

@ViewChild(DataBindingDirective) dataBinding: DataBindingDirective;
                                  ~~~~~~~~~~~

I suspect that it maybe related to the packages versions, because at stackblitz everything working fine (working example: StackBlitz ).

Bigger chunk of code:

export class NPEComponent implements OnInit {
  rowData$: Observable&lt;Array&lt;NPE&gt;&gt; = of([]);
  public filter$: Observable&lt;CompositeFilterDescriptor | null&gt;;
  @ViewChild(DataBindingDirective) dataBinding: DataBindingDirective;  // compilation error here

  constructor(private activatedRoute: ActivatedRoute) {

    this.filter$ = this.activatedRoute.queryParams.pipe(
      map(params =&gt; params[&#39;name&#39;]),
      distinctUntilChanged(),
      map(searchTerm =&gt; {
        if (!searchTerm) return null;

        return (&lt;CompositeFilterDescriptor&gt;{
          logic: &#39;or&#39;,
          filters: [ buildFilterItem(&#39;organizationName&#39;, searchTerm) ]
        })
      }),
      //tap(() =&gt; console.log(&#39;filter been assigned&#39;)),
      //tap(() =&gt; this.dataBinding.skip = 0),
    );
  }

  ngOnInit(): void {
    this.rowData$ = this.NPEService.apiNPEGet();
  }

versions from packages.json :

  &quot;dependencies&quot;: {
    &quot;@angular/animations&quot;: &quot;~13.3.5&quot;,
    &quot;@angular/common&quot;: &quot;~13.3.5&quot;,
    &quot;@angular/compiler&quot;: &quot;~13.3.5&quot;,
    &quot;@angular/core&quot;: &quot;~13.3.5&quot;,
    &quot;@angular/forms&quot;: &quot;~13.3.5&quot;,
    &quot;@angular/localize&quot;: &quot;~13.3.5&quot;,
    &quot;@angular/platform-browser&quot;: &quot;~13.3.5&quot;,
    &quot;@angular/platform-browser-dynamic&quot;: &quot;~13.3.5&quot;,
    &quot;@angular/platform-server&quot;: &quot;~13.3.5&quot;,
    &quot;@angular/router&quot;: &quot;~13.3.5&quot;,
    &quot;@ng-idle/core&quot;: &quot;^11.1.0&quot;,
    &quot;@ng-idle/keepalive&quot;: &quot;^11.0.3&quot;,
    &quot;@ngrx/effects&quot;: &quot;^13.2.0&quot;,
    &quot;@ngrx/router-store&quot;: &quot;^13.2.0&quot;,
    &quot;@ngrx/store&quot;: &quot;^13.2.0&quot;,
    &quot;@ngrx/store-devtools&quot;: &quot;^13.2.0&quot;,
    &quot;@ngx-translate/core&quot;: &quot;^14.0.0&quot;,
    &quot;@ngx-translate/http-loader&quot;: &quot;^7.0.0&quot;,
    &quot;@progress/kendo-angular-buttons&quot;: &quot;^8.1.0&quot;,
    &quot;@progress/kendo-angular-common&quot;: &quot;^3.2.1&quot;,
    &quot;@progress/kendo-angular-dateinputs&quot;: &quot;^7.1.2&quot;,
    &quot;@progress/kendo-angular-dialog&quot;: &quot;^7.0.0&quot;,
    &quot;@progress/kendo-angular-dropdowns&quot;: &quot;^7.2.1&quot;,
    &quot;@progress/kendo-angular-excel-export&quot;: &quot;^5.0.1&quot;,
    &quot;@progress/kendo-angular-grid&quot;: &quot;^7.3.3&quot;,
    &quot;@progress/kendo-angular-indicators&quot;: &quot;^3.0.2&quot;,
    &quot;@progress/kendo-angular-inputs&quot;: &quot;^10.1.0&quot;,
    &quot;@progress/kendo-angular-intl&quot;: &quot;^4.1.0&quot;,
    &quot;@progress/kendo-angular-l10n&quot;: &quot;^4.0.0&quot;,
    &quot;@progress/kendo-angular-label&quot;: &quot;^4.0.1&quot;,
    &quot;@progress/kendo-angular-layout&quot;: &quot;^7.2.0&quot;,
    &quot;@progress/kendo-angular-notification&quot;: &quot;^4.0.1&quot;,
    &quot;@progress/kendo-angular-pdf-export&quot;: &quot;^4.0.0&quot;,
    &quot;@progress/kendo-angular-popup&quot;: &quot;^5.0.0&quot;,
    &quot;@progress/kendo-angular-tooltip&quot;: &quot;^4.1.1&quot;,
    &quot;@progress/kendo-angular-treeview&quot;: &quot;^7.1.3&quot;,
    &quot;@progress/kendo-data-query&quot;: &quot;^1.6.0&quot;,
    &quot;@progress/kendo-drawing&quot;: &quot;^1.17.1&quot;,
    &quot;@progress/kendo-licensing&quot;: &quot;^1.3.0&quot;,
    &quot;@progress/kendo-theme-default&quot;: &quot;^5.0.0&quot;,
    &quot;bootstrap&quot;: &quot;^5.1.3&quot;,
    &quot;jquery&quot;: &quot;^3.6.0&quot;,
    &quot;oidc-client&quot;: &quot;^1.11.5&quot;,
    &quot;popper.js&quot;: &quot;^1.16.0&quot;,
    &quot;run-script-os&quot;: &quot;^1.1.6&quot;,
    &quot;rxjs&quot;: &quot;~7.5.5&quot;,
    &quot;tslib&quot;: &quot;^2.4.0&quot;,
    &quot;zone.js&quot;: &quot;~0.11.5&quot;
  },
  &quot;devDependencies&quot;: {
    &quot;@angular-devkit/build-angular&quot;: &quot;~13.3.4&quot;,
    &quot;@angular/cli&quot;: &quot;~13.3.4&quot;,
    &quot;@angular/compiler-cli&quot;: &quot;~13.3.5&quot;,
    &quot;@ngrx/schematics&quot;: &quot;^13.2.0&quot;,
    &quot;@types/jasmine&quot;: &quot;~4.0.3&quot;,
    &quot;@types/jasminewd2&quot;: &quot;~2.0.10&quot;,
    &quot;@types/node&quot;: &quot;^17.0.29&quot;,
    &quot;jasmine-core&quot;: &quot;~4.1.0&quot;,
    &quot;karma&quot;: &quot;~6.3.19&quot;,
    &quot;karma-chrome-launcher&quot;: &quot;~3.1.1&quot;,
    &quot;karma-coverage&quot;: &quot;~2.2.0&quot;,
    &quot;karma-jasmine&quot;: &quot;~5.0.0&quot;,
    &quot;karma-jasmine-html-reporter&quot;: &quot;^1.7.0&quot;,
    &quot;schematics-scss-migrate&quot;: &quot;^1.3.15&quot;,
    &quot;typescript&quot;: &quot;~4.6.3&quot;
  }
}

In PowerShell:

&gt;ng --version
Angular CLI: 13.3.10
Node: 18.13.0 (Unsupported)
Package Manager: npm 8.19.3
OS: win32 x64
Angular: 13.3.12
Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1303.10
@angular-devkit/build-angular   13.3.10
@angular-devkit/core            13.3.10
@angular-devkit/schematics      13.3.10
@angular/cli                    13.3.10
@schematics/angular             13.3.10
rxjs                            7.5.7
typescript                      4.6.4

答案1

得分: 1

描述的行为与TypeScript的严格配置相关。避免此错误的一种简单方法是使用非空断言运算符 "!":

@ViewChild(DataBindingDirective) dataBinding!: DataBindingDirective;

在StackBlitz中不存在此错误,因为项目在tsconfig.json文件中缺少严格类型检查配置。

英文:

The described behavior is related to a TypeScript strict configuration.One simple way to avoid it will be to place an non-null assertion operator "!":

@ViewChild(DataBindingDirective) dataBinding!: DataBindingDirective;

The error isn't present in StackBlitz as the project is missing the strict type check configurations in the tsconfig.json file.

huangapple
  • 本文由 发表于 2023年2月18日 14:08:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定