简体   繁体   English

TypeScript 无法读取未定义的属性 - Angular 7

[英]TypeScript Cannot read property of undefined - Angular 7

i have this email model:我有这个 email model:

export class Email{
    to: Array<
        {
            email: string
            name: string
        }
    >
    from: {
        email: string
        name: string
    }
    cc: Array<
        {
            email: string
            name: string
        }
    >
    bcc: Array<
        {
            email: string
            name: string
        }
    >
    subject: string
    body: string
    type: string
}

Then I import the email model and declare it in the class like this:然后我导入 email model 并像这样在 class 中声明它:

import { Email } from '@/models/email';

export class BuilderJobComponent implements OnInit {
    emailJobData: Email

Later on, inside a class method I try to set a value but I get undefined.稍后,在 class 方法中,我尝试设置一个值,但未定义。 What am I not understanding about this?我对此有什么不明白的?

Cannot read property 'from' of undefined无法读取未定义的属性“来自”

this.emailJobData.from.email = email
this.emailJobData.from.name  = name

// set the To address
if( Recipient ){
    this.emailJobData.to.push({ email: Recipient, name: '' })
}

You declared variable emailJobData , but did not assign any value to it.您声明了变量emailJobData ,但没有为其分配任何值。 Hence, it's value is undefined .因此,它的值是undefined And undefined doesn't have any properties.并且 undefined 没有任何属性。

The type after colon only defines the type that can be assigned to the variable, but does not assign any value.冒号后面的类型只定义了可以赋值给变量的类型,不赋值。

In your code, replace在您的代码中,替换

emailJobData: Email;

with

emailJobData: Email = new Email();

And you should be good.你应该很好。

EDIT: To expand on fixing the issue, it's a good idea to have a constructor in your class that will initialize the values / objects in your class to the expected values.编辑:为了解决问题,最好在 class 中有一个构造函数,它将 class 中的值/对象初始化为预期值。 Of course, this depends on the business logic - sometimes you would expect undefined values instead of empty array / empty object, etc, so update accordingly - below is just an example.当然,这取决于业务逻辑 - 有时您会期望未定义的值而不是空数组/空 object 等,因此请相应更新 - 下面只是一个示例。

export class AddressWithDisplayValue {
    email: string;
    name: string;
}

export class Email{
    from: AddressWithDisplayValue;
    to: AddressWithDisplayValue[];
    cc: AddressWithDisplayValue[];
    bcc: AddressWithDisplayValue[];
    subject: string;
    body: string;
    type: string;
    
    constructor() {
        this.from = new AddressWithDisplayValue();
        this.to = [];
        this.cc = [];
        this.bcc = [];
    }
}

Then, after it was initialized with emailJobData = new Email() you will be able to set those properties, eg然后,在使用emailJobData = new Email()初始化后,您将能够设置这些属性,例如

this.emailJobData.from.email = email;

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

相关问题 无法读取未定义的Angular 4打字稿的属性 - Cannot read property of undefined Angular 4 Typescript 无法读取未定义的Angular / Typescript的属性“ push” - Cannot read property 'push' of undefined Angular/Typescript Angular 2打字稿:无法读取未定义的属性newLine - Angular 2 typescript: Cannot read property newLine of undefined Angular 2+ / Typescript无法读取未定义的属性 - Angular 2+ / Typescript Cannot read property of undefined 无法读取Typescript Angular 5中未定义的属性somevalue - Cannot read property somevalue of undefined in Typescript Angular 5 Angular CLI - Typescript 抛出:无法读取未定义的属性“长度” - Angular CLI - Typescript throws: Cannot read property 'length' of undefined 打字稿角度错误:无法读取未定义的属性“ toLowerCase” - Typescript angular error : Cannot read property 'toLowerCase' of undefined TypeScript / Angular2-例外:TypeError:无法读取未定义的属性“ push” - TypeScript / Angular2 - EXCEPTION: TypeError: Cannot read property 'push' of undefined Angular2 - TypeError:无法读取(Typescript)中未定义的属性“Id” - Angular2 - TypeError: Cannot read property 'Id' of undefined in (Typescript) Typescript / Angular2 TypeError:无法读取undefined的属性 - Typescript/Angular2 TypeError: Cannot read property of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM