Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js:从组件';s javascript_Javascript_Ember.js_Handlebars.js_Ember Components - Fatal编程技术网

Ember.js:从组件';s javascript

Ember.js:从组件';s javascript,javascript,ember.js,handlebars.js,ember-components,Javascript,Ember.js,Handlebars.js,Ember Components,我正在构建一个组件,我注意到一个行为对我来说非常奇怪。My component的调用如下所示: {{my-component model=model}} type: DS.belongsTo('type') 我的模型包含如下关系: {{my-component model=model}} type: DS.belongsTo('type') 现在,在my component.js中,如果我登录到控制台this.get('model.type.name')(或this.get('model

我正在构建一个组件,我注意到一个行为对我来说非常奇怪。My component的调用如下所示:

{{my-component model=model}}
type: DS.belongsTo('type')
我的模型包含如下关系:

{{my-component model=model}}
type: DS.belongsTo('type')
现在,在
my component.js
中,如果我登录到控制台
this.get('model.type.name')
(或
this.get('model').get('type').get('name')
),我会得到
未定义的
。但是,如果在
my component.hbs
I insert
{{{model.type.name}}
中正确显示该值

我真的不理解这种行为:如何像在组件的把手模板中一样从组件的javascript中访问模型的关系


谢谢

在余烬数据中,关系被视为承诺,因此您应该使用
然后
作为结果

this.get('model').get('type').then((result) =>{
 console.log(' Name ', result.get('name'));
});
参考:

感谢您的反馈,但是,如果我尝试执行
this.get('model')。然后(model=>{console.log(model)})
,我会得到
TypeError:this.get(…)。那么它不是一个函数。我不明白为什么会出现这种情况:/不过,您的代码片段实际上是有效的,所以我想知道为什么
this.get('model')。那么
不是一个函数……像
belongsTo
hasMany
这样的关系将返回
Promise
。这里的
model
model
hook的解析模型无关。你可以只为允诺人做
,这很有道理。非常感谢你!