从组件刷新/重新加载ember路径

nul*_*ter 4 javascript router components routes ember.js

我有一个组件,这实际上是一个模态对话框.当我完成该对话框并按下"确定"按钮后,我想留在我打开该对话框的停留页面上.哪个不难.

但问题是对话框会更改数据(我通过REST调用获取数据)所以我需要刷新我已经开启的路径以反映数据更改.

既然,我是从一个组件调用它,我没有 Route这么不能打电话route.refresh().

我试着拿到路由器:

this.set('router', Ember.getOwner(this).lookup('router:main'));

并转换到同一页面:

_this.get('router').transitionTo('my-route')

但由于路线没有改变(我只打开了一个对话框),transitionTo没有被触发!

有没有办法可以强制触发transitionTo或刷新我所在的页面?

谢谢!

Ram*_*oya 6

首先,您可以通过将路由服务注入组件来轻松获取当前路由名称.
然后,您可以获取当前路由实例并应用其refresh方法:

// app/components/a-component.js

import Component from "ember-component";
import service from "ember-service/inject";
import getOwner from "ember-owner/get";

export default Component.extend({
	routing: service("-routing"),
	actions: {
		refresh() {
			const currentRouteName = this.get("routing.currentRouteName");
			const currentRouteInstance = getOwner(this).lookup(`route:${currentRouteName}`);
			currentRouteInstance.refresh();
		}
	}
});
Run Code Online (Sandbox Code Playgroud)


Emb*_*eak 5

为此,refreshCurrentRoute在最近的路由或应用程序路由文件中定义方法。

actions:{
 refreshCurrentRoute(){
  this.refresh();
 }
}
Run Code Online (Sandbox Code Playgroud)

从您的组件中,您需要调用refreshCurrentRoute操作。您可以使用ember-route-action-helper或通过关闭操作。