在Ember.js中,我如何从App.Router中获得rootURL ?
In Ember.js how can I get the rootURL from App.Router?
我怎么能得到rootURL在App.Router在我的控制器在我的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');
我应该补充说,这通常是一个坏主意,但也许它会帮助你。
相关文章:
- 混合 ui-sref 和 $state.go 在 Angular ui-router 中进行状态转换
- this.router在AngularJS 2中未定义
- Meteor Router数据函数被调用两次
- 在Nodejs中Express输入DEBUG=app时权限被拒绝/bin
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- 如何在router.get()方法中传递url作为参数
- renderReact/Rect Router+Node/Express.js的服务器端/同构渲染中未定义renderP
- 骨干's Router.execute(callback,args,name)方法获取未定义的名称
- 有没有办法用routerLink/router.navigation附加查询/路由参数
- react router router.HistoryLocation以<noscript>但没有完美的工作
- what is "app.router" express.js
- ExpressJS app.use("/" router) router.get("/&q
- 未定义<在app.jsx中,使用react.js、react-router和webpack
- 在Ember.js中,我如何从App.Router中获得rootURL ?
- 安贝App.router.“应用程序未定义”
- 查看是否已登录- React Router App ES6
- 通过react router传递在app.jsx级别创建的道具
- 从App.router.method中访问App.method
- Express Router不从app.js中挑选页面
- express.Router() vs. app.get