Ember 在具有 return 值的应用程序路由中调用操作
Ember call action in application route with return value
在 Ember 中,您可以使用 this.actions.actionName()
在当前上下文中调用操作,就像调用普通函数一样。这意味着该操作可以 return 一个值。
在我的登录路径中,我试图以这种方式从应用程序路径调用一个动作。但是 this.actions
仅包含我的登录路径中的操作。
登录路径
actions: {
test: function() {
console.log(this.actions.sameRouteAction()); //5
console.log(this.actions.applicationRouteAction()); // this.actions.applicationRouteAction() is not a function.
},
sameRouteAction: function() {
return 5;
}
}
申请途径
actions: {
applicationRouteAction: function() {
return 7;
}
}
this.actions.applicationRouteAction()
必须变成什么才能让它发挥作用?
从 Ember 3.1.0 开始,没有一种方法可以像在当前 route/class .可以使用send
函数向上发送一个动作:
actions: {
test: function() {
this.send('applicationRouteAction');
},
}
但是您无法通过这种方式获取 applicationRouteAction
函数的 return 值。
将操作结果从父路由传递到子路由的两个最佳选择是:
正在通过 transitionTo or refresh. This will update the route tree, and your child route can get the model of its parent via modelFor 更新父路由的模型。由于您在示例中使用的是 Application 路由,因此这似乎不是您想要使用的方法。
创建或使用现有的 Service 在路由之间进行通信。您可以在两个位置注入服务,从应用程序路由更新其状态,并从子路由读取其状态。
如果您可以添加有关您希望操作执行的操作的更多详细信息,我可以使用有关哪种模式更有意义以及原因的更多详细信息来更新此答案!
在 Ember 中,您可以使用 this.actions.actionName()
在当前上下文中调用操作,就像调用普通函数一样。这意味着该操作可以 return 一个值。
在我的登录路径中,我试图以这种方式从应用程序路径调用一个动作。但是 this.actions
仅包含我的登录路径中的操作。
登录路径
actions: {
test: function() {
console.log(this.actions.sameRouteAction()); //5
console.log(this.actions.applicationRouteAction()); // this.actions.applicationRouteAction() is not a function.
},
sameRouteAction: function() {
return 5;
}
}
申请途径
actions: {
applicationRouteAction: function() {
return 7;
}
}
this.actions.applicationRouteAction()
必须变成什么才能让它发挥作用?
从 Ember 3.1.0 开始,没有一种方法可以像在当前 route/class .可以使用send
函数向上发送一个动作:
actions: {
test: function() {
this.send('applicationRouteAction');
},
}
但是您无法通过这种方式获取 applicationRouteAction
函数的 return 值。
将操作结果从父路由传递到子路由的两个最佳选择是:
正在通过 transitionTo or refresh. This will update the route tree, and your child route can get the model of its parent via modelFor 更新父路由的模型。由于您在示例中使用的是 Application 路由,因此这似乎不是您想要使用的方法。
创建或使用现有的 Service 在路由之间进行通信。您可以在两个位置注入服务,从应用程序路由更新其状态,并从子路由读取其状态。
如果您可以添加有关您希望操作执行的操作的更多详细信息,我可以使用有关哪种模式更有意义以及原因的更多详细信息来更新此答案!