【问题标题】:Ionic Cannot read property of undefined离子无法读取未定义的属性
【发布时间】:2018-06-01 19:17:21
【问题描述】:

我已经阅读了其他条目,但我仍然不知道如何解决我的问题。问题是下一个:

TypeError:无法读取未定义的属性“状态” 在 Object.eval [作为 updateRenderer] (ng:///AppModule/MyApp.ngfactory.js:105:62) 在 Object.debugUpdateRenderer [作为 updateRenderer] (http://localhost:8100/build/vendor.js:14730:21) 在 checkAndUpdateView (http://localhost:8100/build/vendor.js:13866:14) 在 callViewAction (http://localhost:8100/build/vendor.js:14211:21) 在 execComponentViewsAction (http://localhost:8100/build/vendor.js:14143:13) 在 checkAndUpdateView (http://localhost:8100/build/vendor.js:13867:5) 在 callViewAction (http://localhost:8100/build/vendor.js:14211:21) 在 execEmbeddedViewsAction (http://localhost:8100/build/vendor.js:14169:17) 在 checkAndUpdateView (http://localhost:8100/build/vendor.js:13862:5) 在 callViewAction (http://localhost:8100/build/vendor.js:14211:21)

还有代码:

import { Injectable } from '@angular/core';
@Injectable()

export class AppState {
_state = {}

constructor() {
}

get state() {
   return this._state = this._clone(this._state);
}

set state(value) {
   throw new Error('do not mutate the `.state` directly');
}
get(prop?: any) {
   const state = this.state;
   return state.hasOwnProperty(prop) ? state[prop] : state;
}
set(prop: string, value: any) {
   return this._state[prop] = value;
}
_clone(object) {
   return JSON.parse(JSON.stringify(object));
}
}

在 Ubuntu 16.04 上使用 Ionic3。

【问题讨论】:

  • 您的代码中有错字:this.state 而不是 this._state

标签: typescript ionic-framework undefined ionic3


【解决方案1】:

在我看来,错误来自这里:

const state = this.state;

您的服务中有this._state,而不是this.state

【讨论】:

  • 不,问题仍然存在。
  • @BryanArreolaMurguía,您能否说明您是如何访问组件内服务的属性_state 的?
  • @BryanArreolaMurguía,我的意思是你是如何在你发布的代码中调用函数的?您是否进行了我建议的更改,即将这一行 const state = this.state; 更改为这一行 const state = this._state;
猜你喜欢
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 2017-08-02
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多