可能在Ember服务中进行路由转换

可能在Ember服务中进行路由转换

本文介绍了可能在Ember服务中进行路由转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ember-cli 1.13.8,而且我有一个服务来处理我的大部分逻辑。现在我有一个函数来监听某些事情是真的还是假的,然后可以根据这个改变路由。我宁愿不需要从每个路线的内部调用这个功能,因为我想要在每一条路线上发生。它的目标是确定玩家是否赢了,并且游戏中的每个互动都会驱动这个。



我的游戏服务内部:

  init(){
...
if(true){
console.log(you won won!);
this.transitionTo(恭喜);
}
},

当然,这是因为这个不是像Ember所期望的路由。我知道我可以从每个路线的内部调用这个方法,但我想知道是否有更好的方法来做到这一点。



感谢



修改



到目前为止,我已经尝试在App中导入,然后尝试扩展路由器。

解决方案

您可以使用路由服务(这是一个私有的API):


 路由:Ember.inject.service(' -  routing'),

init(){
...
if(true){
console.log(you won won!);
this.get(routing)。transitionTo(恭喜);
}
},


I'm using ember-cli 1.13.8 and I have a service that handles most of my logic. Right now I have a function that listens to whether certain things are true or false and then can make a route change based upon that. I'd rather not have to call that function from inside every route since I want it to happen on every route. Its goal is to determine whether the player won and every interaction in the game drives this.

Inside of my game service:

init() {
  ...
  if(true) {
    console.log("you've won!");
    this.transitionTo("congratulations");
  }
},

Of course, this fails because this isn't a route like Ember expects. I know I can call this method from inside of every route instead but I'm wondering if there is a better way to do this.

Thanks

Edit

So far I've tried importing in the App and then trying to extend the Router. This seems like a bad idea though.

解决方案

You can use the routing service (which is a private API):

routing: Ember.inject.service('-routing'),

init() {
  ...
  if(true) {
    console.log("you've won!");
    this.get("routing").transitionTo("congratulations");
  }
},

这篇关于可能在Ember服务中进行路由转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:52