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 在控制器中使用Mixin属性_Ember.js - Fatal编程技术网

Ember.js 在控制器中使用Mixin属性

Ember.js 在控制器中使用Mixin属性,ember.js,Ember.js,这是一个糟糕的例子,但我只是尝试在控制器中使用mixin的属性。我在一条路线上也做了同样的事情,可以进入那片土地。我试过各种方法去引用我知道的一处房产。。。我误解了什么 // app/mixins/author-data.js import Ember from 'ember'; export default Ember.Mixin.create({ authorName: 'Example author name', }); //app/controllers/application

这是一个糟糕的例子,但我只是尝试在控制器中使用mixin的属性。我在一条路线上也做了同样的事情,可以进入那片土地。我试过各种方法去引用我知道的一处房产。。。我误解了什么

// app/mixins/author-data.js
import Ember from 'ember';

export default Ember.Mixin.create({
  authorName: 'Example author name',
});

//app/controllers/application.js
从“余烬”导入余烬;
从“app name/mixins/author data”导入AuthorDatas;
导出默认的Ember.Controller.extend(AuthorDatas{
siteTitle:`siteTitle`,
fromAuthorData:this.get('authorName'),

//返回控制器上的
fromAuthorData
属性的定义如下(我认为):


为了理解这个问题,我们需要讨论范围,当您扩展/创建一个对象时,您仅仅是在传递选项,您的代码与以下代码没有什么不同:

// app/controllers/application.js
import Ember from 'ember';
import AuthorDatas from 'app-name/mixins/author-data';

const { computed } = Ember; 

export default Ember.Controller.extend(AuthorDatas, {
  siteTitle: `Site title`,   

  // We add `authorName` as the dependent key, should it change `fromAuthorData` will update
  fromAuthorData: computed('authorName', function() {
    // your author data stuff
    let authorName = this.get('authorName');
    // ...
    return authorDetails;
  }), 

  actions: {
    showAuthor() {
      var author = this.get('fromAuthorData');
      console.log(`Author from controller: ${author}`);
    },
  },
});
现在,要访问依赖于该
对象的属性,您需要一个函数,该函数与该对象一起运行,作为返回该值的上下文,输入。 您的代码变成:


fromAuthorData
在您的示例中,实际上是一堆poo,您能用源代码替换它吗?想象一下
this.get('authorName'))
在那里吗我想,在ember twiddle中,一切都会正常工作,对于mixin,你需要创建文件,比如
mixin/author data.js
为什么?如果mixin只是添加属性,为什么它不与
siteTitle:“我的应用程序”,
let options = {

  siteTitle: `Site title`, 

  // `this` is undefined since we are in strict mode
  fromAuthorData: this.get('authorName'),

  actions: {
    showAuthor() {
      var author = this.get('fromAuthorData');
      console.log(`Author from controller: ${author}`);
    },
  }
};

export default Ember.Controller.extend(AuthorDatas, options);
// app/controllers/application.js
import Ember from 'ember';
import AuthorDatas from 'app-name/mixins/author-data';

const { computed } = Ember; 

export default Ember.Controller.extend(AuthorDatas, {
  siteTitle: `Site title`,   

  // We add `authorName` as the dependent key, should it change `fromAuthorData` will update
  fromAuthorData: computed('authorName', function() {
    // your author data stuff
    let authorName = this.get('authorName');
    // ...
    return authorDetails;
  }), 

  actions: {
    showAuthor() {
      var author = this.get('fromAuthorData');
      console.log(`Author from controller: ${author}`);
    },
  },
});