在组件中使用isDefined()方法判断变量是否定义,如果没有定义,则给变量一个默认值。例如:
import { Component, Input } from '@angular/core';
@Component({
selector: 'my-component',
template:
})
export class MyComponent {
@Input() myObject;
isDefined(value) { return value !== undefined && value !== null; } }
在上述示例中,使用了isDefined()方法来检查myObject变量是否定义。如果是,则展示myObject.property的值。如果不是,则在模板中不会展示任何内容。