簡體   English   中英

在Ember.js中,如何從App.Router獲取rootURL?

[英]In Ember.js how can I get the rootURL from App.Router?

如何在控制器的App.Router中獲取rootURL以在JSON請求中使用?

如果我這樣指定rootURL:

App.Router.reopen({
  rootURL: '/site1/'
});

我希望能夠做這樣的事情:

FooController = Ember.ObjectController.extend({
   needs: ["application"],
   actions: {
     examine: function() {
         var rootURL = this.get('controllers.application.router.rootURL');
         $.getJSON(rootURL + "/examine/" + id).then(function(response) {
         // do stuff with response
         });
      }
    }
});

路由器被注入到所有路由上,您可以將該動作向上移動到該路由上,並從該路由中獲取路由器。

FooRoute = Ember.Route.extend({
   actions: {
     examine: function() {
         var rootURL = this.get('router.rootURL');
         $.getJSON(rootURL + "/examine/" + id).then(function(response) {
         // do stuff with response
         });
      }
    }
});

或者,您可以僅在路由設置控制器時將屬性添加到控制器。

FooRoute = Ember.Route.extend({
  setupController: function(controller,model){
    this._super(controller, model);
    controller.set('rootURL', this.router.rootURL);
  }
});

示例: http//emberjs.jsbin.com/tomuhe/1/edit

以下代碼在Ember 2.10上對我有用(在服務和組件上):

const router = Ember.getOwner(this).lookup('router:main'),
  rootURL = router.get('rootURL');

我應該補充一點,這通常不是一個好主意,但也許可以幫到您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM