简体   繁体   English

@Input() 变量说对象在 Angular 中可能未定义

[英]@Input() variable saying object is possibly undefined in Angular

I'm trying to print the object properties data in html.我正在尝试在 html 中打印对象属性数据。 Unfortunately I'm facing the issue saying as like "object is possibly undefined in Angular".不幸的是,我面临的问题是“对象可能在 Angular 中未定义”。 My component ts file :我的组件 ts 文件:

import { Component, Input, OnInit } from '@angular/core';
import { Bike } from '../bike';

@Component({
  selector: 'app-bikedetails',
  templateUrl: './bikedetails.component.html',
  styleUrls: ['./bikedetails.component.css']
})
export class BikedetailsComponent implements OnInit {

  constructor() {  }
  @Input() selected? : Bike;
  ngOnInit(): void {
  }

}

My component html code :我的组件 html 代码:

<div class="row">
    <div class="col-md-12">

        {{selected | json}}  // Able to see the Json data.

        <h2>Bike Name : {{selected.name}}</h2>   //object is possibly undefined in Angular
        <h3>Bike Company : {{selected.Company}}</h3>  ////object is possibly undefined in Angular
        <h4>Top Speed : {{selected.topspeed}}</h4> //object is possibly undefined in Angular
        <h4>Description : {{selected.name}}</h4>  //object is possibly undefined in Angular
    </div>
</div>

Not getting the reasons why I'm getting the JSON data when I use {{selected |不明白为什么我在使用 {{selected | 时获取 JSON 数据的原因json}} & facing issue when I use it's properties {{selected.name}}. json}} 并在我使用它的属性 {{selected.name}} 时遇到问题。

What I tried : I tried by making the "selected" property as like "undefined" and nullable(!) type and even by initializing in constructor...我尝试过的:我尝试将“selected”属性设置为“undefined”和 nullable(!) 类型,甚至通过在构造函数中初始化...

You specifically defined the input variable to be possibly undefined .专门将输入变量定义为可能是undefined

@Input() selected? : Bike;

The questionmark ( ? ) is just a shorthand for undefined , as in问号 ( ? ) 只是undefined的简写,如

@Input() selected: Bike | undefined;

Most likely that's also what you want, as possibly the input value may be undefined on the outside.很可能这也是您想要的,因为输入值可能在外部未定义。 If you are 100% sure the component is only initialized with an actual input value, you may change your signature to如果您 100% 确定组件使用实际输入值初始化,则可以将签名更改为

@Input() selected: Bike;

but for the sake of type-safety, you can keep it possibly undefined and instead catch the undefined case in your template但是为了类型安全,您可以保持它可能undefined ,而是在模板中捕获undefined情况

<div class="row" ngIf="selected">
    <div class="col-md-12">

        {{selected | json}}  // Able to see the Json data.

        <h2>Bike Name : {{selected.name}}</h2>   //object is possibly undefined in Angular
        <h3>Bike Company : {{selected.Company}}</h3>  ////object is possibly undefined in Angular
        <h4>Top Speed : {{selected.topspeed}}</h4> //object is possibly undefined in Angular
        <h4>Description : {{selected.name}}</h4>  //object is possibly undefined in Angular
    </div>
</div>

or use the elvis operator like so或者像这样使用 elvis 运算符

<h2>Bike Name : {{selected?.name}}</h2>

Try using a setter for the @Input variable.尝试对@Input变量使用 setter。

export class BikedetailsComponent implements OnInit {
  public selected: Bike;

  @Input() set selected(bike: Bike) {
    this.selected = bike;
  }

  constructor() {  }

  ngOnInit(): void {
  }
}

And use *ngIf in the template to use the selected only if it's defined并在模板中使用*ngIf以仅在定义时使用selected

<div *ngIf="!!selected" class="row">
  <div class="col-md-12">
    <h2>Bike Name : {{selected.name}}</h2>
    <h3>Bike Company : {{selected.Company}}</h3>
    <h4>Top Speed : {{selected.topspeed}}</h4>
    <h4>Description : {{selected.name}}</h4>
  </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM