如何在 ember-file-upload 中执行组件操作而不是路由操作

How to preform a component action rather than a route action in ember-file-upload

我正在使用这个包 https://github.com/tim-evans/ember-file-upload

在他们的示例中,他们调用路由操作来上传图片

             onfileadd=(route-action "uploadImage")}}

但是我想改为调用组件操作

我试过

                 onfileadd="uploadImage"}}

并在组件中添加了操作:

uploadImage: function (file) {
  console.log(file);
  let self = this;
  RSVP.cast(Ember.$.get(ENV.APP.API_HOST + "/v1/teams/signed_url/")).then(function (response) {
    return file.upload(response.url, {
      data: response.credentials
    });
  }).then(function (response) {
    self.set('url', response.headers.Location);
  });
},

但是我得到了错误:

Uncaught TypeError: Cannot read property 'uploadImage' of undefined

如果我将它移动到路由,操作将按预期工作,但我需要更新组件中的正确属性(路由中不存在的属性)

知道如何将其更改为组件函数吗?

您应该使用 action 助手:

onfileadd=(action "uploadImage")}}

还要确保 uploadImage 在组件的 actions 散列中:

export default Ember.Component.extend({
  // ...
  actions: {
    uploadImage() { // <-- make sure this is inside the actions hash
      // ...
    }
  }
})