本文介绍了从组件触发路由动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组件,我想触发路线级别的操作,所以我可以转换到不同的路线。

I have a component that I want to trigger a route level action on so I can transition to a different route.

App = Ember.Application.create();

App.Router.map(function() {
});

App.IndexRoute = Ember.Route.extend({
  actions: {
    complete: function() {
      // This never happens :(
      console.log('Triggered complete!');
    }
  }
});

App.MyAreaComponent = Ember.Component.extend({
  actions: {
    clickMyButton: function() {
      console.log('Triggering complete action.');
      // Attempting to trigger App.IndexRoute.actions.complete here
      this.sendAction('complete');
    }
  }
});

我想要完成的是当MyAreaComponent的clickMyButton操作被触发时,它将触发IndexRoute的完​​成操作。

What I am trying to accomplish is when MyAreaComponent's 'clickMyButton' action is triggered, it will trigger the IndexRoute's 'complete' action.

我有设置一个jsbin来演示我的问题:

I have set up a jsbin to demonstrate my issue:

根据

Here is your updated sample -http://emberjs.jsbin.com/wivuyike/3/edit

所以从你的行动需要泡泡的组件,这可以通过

So from the component your action need to bubble herself up, this can be done by

this.sendAction('clickMyButton');

然后当您使用您的组件时,将需要触发的路由操作分配给您的组件操作如下

and then when you use your component, assign route action which needs to be triggered to your component action as below

{{my-area clickMyButton='complete'}}

这篇关于从组件触发路由动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 17:23
查看更多