错误类型错误:无法读取未定义的属性“...”

桑德拉·威尔福德

不知道为什么会在这里发生,我从我的组件中收到此错误:

ERROR TypeError: Cannot read property 'timezone_offset' of undefined
at Object.eval [as updateRenderer] (SettingsComponent.html:16)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:14377)
at checkAndUpdateView (core.js:13513)
at callViewAction (core.js:13858)
at execComponentViewsAction (core.js:13790)
at checkAndUpdateView (core.js:13514)
at callViewAction (core.js:13858)
at execEmbeddedViewsAction (core.js:13816)
at checkAndUpdateView (core.js:13509)
at callViewAction (core.js:13858)

有趣的是,它仍然在模板中显示正确的字符串,并且 ng-cli 中没有错误。这是产生错误的组件的代码:

import { Component, OnInit } from '@angular/core';
import { GetProfileService } from '../../services/get-profile.service';

@Component({
    selector: 'app-settings',
    templateUrl: './settings.component.html'
})
export class SettingsComponent implements OnInit {

    results: any;
    profile: any;

    constructor(private getProfileService: GetProfileService) { }

    ngOnInit() {

        this.getProfileService.profileAPI().subscribe(
            data => {
                this.results = data
                this.profile = this.results
                console.log(this.profile)
            }
        );

    }

}

我现在只是{{ profile.timezone_offset }}在我的模板上用作唯一的东西......

阿米特·奇加达尼

似乎您的数据profile在呈现时尚未加载到对象中。只有在异步操作完成后,profile对象内的数据才被设置?.当您的对象在异步调用上运行时应该使用。timezone_offset将仅在profile定义对象时进行评估

由于异步操作,您可能会收到该错误。尝试以下

{{ profile?.timezone_offset }}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类型错误:无法读取未定义的属性(读取“类型”)

类型错误:无法读取未定义错误的属性“_id”

角度:错误类型错误:无法读取未定义的属性___

错误类型错误:无法读取未定义的属性“列表”

错误类型错误:无法读取未定义的属性“标题”

错误类型错误:无法读取未定义的属性

渲染错误:“类型错误:无法读取未定义的属性‘’”

错误类型错误:无法读取未定义的属性“填充”

错误类型错误:无法读取未定义的属性“stateName”

“类型错误:无法读取未定义的属性‘名称’”错误

错误类型错误:无法读取未定义的属性“_id”

错误类型错误:无法读取未定义的属性“getUsers”

错误类型错误:无法读取未定义的属性“doc”

**错误** 类型错误:无法读取未定义的属性“替换”

类型错误:无法读取未定义的属性(读取“图像”)

类型错误:无法读取未定义的属性(读取“暗”)

类型错误:无法读取未定义的属性(读取“缓存”)

类型错误:无法读取未定义的属性(读取“reduce”)

类型错误:无法读取未定义读取“2”的属性

类型错误:无法读取未定义的属性“类型”

类型错误:无法读取未定义的属性“indexOf”,无法读取未定义的属性“toLowerCase”

类型错误:无法在 setTimeout() 中读取未定义的属性“then”

类型错误:无法读取未定义的属性“ChangeClassName”

类型错误:无法读取未定义 NodeJS 的属性“样式”

AngularJS类型错误:无法读取未定义的属性“ GET()”

类型错误:未定义无法读取属性“webpackJsonp”

类型错误:无法读取未定义的属性“_alreadyWrapped”

opencv - 类型错误:无法读取未定义的属性“长度”

类型错误:无法读取未定义的属性“querySelector”