Ember & Typescript - 如何注入路由服务
Ember & Typescript - how to inject router service
我正在尝试将 RouterService 注入我的控制器:
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import RouterService from '@ember/routing/router-service';
export default class Search extends Controller.extend({
// anything which *must* be merged to prototype here
}) {
@service router!: RouterService;
@action
actionClick(){
this.router.transitionTo('protected.apk.detail')
}
}
// DO NOT DELETE: this is how TypeScript knows how to look up your controllers.
declare module '@ember/controller' {
interface Registry {
'search': Search;
}
}
但我得到一个错误:Error: Assertion Failed: Attempting to inject an unknown injection: 'service:router'
我猜这是因为没有 service:router,而是 router:main。
你能告诉我如何正确注入 RouterService 吗?
这个控制器在我的引擎里。
ember版本:3.18.0
谢谢
你注入路由器服务如下
@service router;
服务底部的类型定义是一个线索。
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { Registry as Services } from '@ember/service';
export default class Search extends Controller {
@service router!: Services['router'];
@action
actionClick(){
this.router.transitionTo('protected.apk.detail')
}
}
这应该有效:
import Service from '@ember/service';
import type RouterService from '@ember/routing/router-service';
export default class MyService extends Service {
@service declare router: RouterService;
}
我正在尝试将 RouterService 注入我的控制器:
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import RouterService from '@ember/routing/router-service';
export default class Search extends Controller.extend({
// anything which *must* be merged to prototype here
}) {
@service router!: RouterService;
@action
actionClick(){
this.router.transitionTo('protected.apk.detail')
}
}
// DO NOT DELETE: this is how TypeScript knows how to look up your controllers.
declare module '@ember/controller' {
interface Registry {
'search': Search;
}
}
但我得到一个错误:Error: Assertion Failed: Attempting to inject an unknown injection: 'service:router'
我猜这是因为没有 service:router,而是 router:main。
你能告诉我如何正确注入 RouterService 吗?
这个控制器在我的引擎里。
ember版本:3.18.0
谢谢
你注入路由器服务如下
@service router;
服务底部的类型定义是一个线索。
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { Registry as Services } from '@ember/service';
export default class Search extends Controller {
@service router!: Services['router'];
@action
actionClick(){
this.router.transitionTo('protected.apk.detail')
}
}
这应该有效:
import Service from '@ember/service';
import type RouterService from '@ember/routing/router-service';
export default class MyService extends Service {
@service declare router: RouterService;
}