可以在 Ember 的服务内部进行路由转换吗? [英] Possible to make a route transition inside of a service in Ember?

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

问题描述

我正在使用 ember-cli 1.13.8 并且我有一个服务可以处理我的大部分逻辑.现在我有一个函数可以监听某些事情是真是假,然后可以基于此更改路线.我宁愿不必从每条路线内部调用该函数,因为我希望它发生在每条路线上.它的目标是确定玩家是否赢了,游戏中的每一次互动都会推动这一点.

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.

我的游戏服务内部:

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

当然,这失败了,因为 this 不是 Ember 期望的路线.我知道我可以从每条路线内部调用这个方法,但我想知道是否有更好的方法来做到这一点.

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.

谢谢

编辑

到目前为止,我已经尝试在应用程序中导入,然后尝试扩展路由器.不过,这似乎是个坏主意.

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

推荐答案

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

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 的服务内部进行路由转换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆