Javascript 错误类型错误:无法读取属性'';未定义的
不确定为什么会发生这种情况,我从我的组件中得到以下错误:Javascript 错误类型错误:无法读取属性'';未定义的,javascript,angular,typescript,Javascript,Angular,Typescript,不确定为什么会发生这种情况,我从我的组件中得到以下错误: 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 (
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\u offset}}
作为模板上的唯一内容 在呈现数据时,您的数据似乎尚未加载到配置文件
对象中。只有在异步操作完成后,才会在配置文件
对象中设置数据<代码>?。应在对象在异步调用上运行时使用<仅当定义了配置文件
对象时,才会计算code>时区偏移量
由于异步操作,您可能会遇到该错误。试试下面的方法
{{profile?.timezone_offset}
在呈现时,您的数据似乎尚未加载到profile
对象中。只有在异步操作完成后,才会在配置文件
对象中设置数据<代码>?。应在对象在异步调用上运行时使用<仅当定义了配置文件
对象时,才会计算code>时区偏移量
由于异步操作,您可能会遇到该错误。试试下面的方法
{{profile?.timezone_offset}}
使用?。
使用?。
请显示您的模板。可能是由于异步操作导致的,您将获得该错误。请尝试{{profile?.timezone\u offset}}
。您是否尝试过{{profile?.timezone\u offset}}
?是的,这似乎解决了问题。你能给我解释一下这是什么吗?does@SandraWillford我在回答中加了一些解释。请检查。请出示您的模板。可能是由于异步操作导致的,您将获得该错误。请尝试{{profile?.timezone\u offset}}
。您是否尝试过{{profile?.timezone\u offset}}
?是的,这似乎解决了问题。你能给我解释一下这是什么吗?does@SandraWillford我在回答中加了一些解释。请检查一下。
{{ profile?.timezone_offset }}