[英]public methods of class not visible in TypeScript/Angular
I have a typescript class as below: 我有一个打字稿课,如下所示:
import { Injectable } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
@Injectable()
export class RouteParamsService {
public router : ActivatedRoute;
constructor(){};
public getRouteParams(){
return this.router;
}
}
I am using it in my component as below: 我在组件中使用它的方式如下:
import { ListItem } from "../../../shared/models/listItem.model";
import { RouteParamsService } from '../../../shared/services/routeParams.service';
export class CheckGroupsFilterValues {
private route = new RouteParamsService();
public rgr: string[] = []; //responsibilityGroups
public sys: string[] = []; //systems
public app: string[] = []; //applications
public cgr: string[] = []; //checkGroups
public cgi: string[] = []; //checkGroupInstances
public nat: string; //needsAttentions
constructor(){
this.rgr = ["MULTI_MULTI_Vale_Sellout (4213)"];
this.sys = ["MULTI_MULTI_Vale_Sellout (4213)"];
this.app = ["MULTI_MULTI_Vale_Sellout (4213)"];
this.cgr = ["MULTI_MULTI_Vale_Sellout (4213)"];
this.cgi = ["MULTI_MULTI_Vale_Sellout (4213)"];
console.log(this.route);
}
}
The problem is: console.log(this.route)
gives the following output, RouteParamsService {}
. 问题是:
console.log(this.route)
提供以下输出RouteParamsService {}
。 The public method getRouteParams()
defined in the class is not visible at all even though I have created an instance already. 即使我已经创建了一个实例,该类中定义的公共方法
getRouteParams()
也不可见。
There is more than one wrong things here. 这里有不止一件错误的事情。
1- The router
of RouteParamsService
will always be undefined
as it is not being assigned anywhere. 1-
RouteParamsService
的router
将始终是undefined
因为它没有被分配到任何地方。 In needs to be in constructor
in onder to be injected properly. 需要在in的
constructor
中正确注入。
constructor(public router : ActivatedRoute){};
2- private route = new RouteParamsService();
2-
private route = new RouteParamsService();
does not create an Angular service. 不会创建Angular服务。 In order to inject
ActivatedRoute
in the constructor of service, either it needs to be an Angular service (injectable) or you have to manually do it. 为了将
ActivatedRoute
注入服务的构造函数中,它要么必须是Angular服务(可注入),要么必须手动执行。 So, if you create the service with new
, you have to pass ActivatedRoute
to service manually. 因此,如果使用
new
创建服务,则必须手动将ActivatedRoute
传递给服务。
You can use Injector
( https://angular.io/api/core/Injector ) to manually inject injectables in your custom classes. 您可以使用
Injector
( https://angular.io/api/core/Injector )在您的自定义类中手动注入可注入对象。
UPDATE 更新
In order to inject an Angular injectable to a custom class, you can use Injector
. 为了将Angular可注入对象注入自定义类,可以使用
Injector
。
First, export an instance of Injector
from a feature module (in order to avoid circular dependencies) and import the feature module in your app module 首先,从功能模块导出
Injector
实例(以避免循环依赖),然后在应用程序模块中导入功能模块
export let InjectorInstance: Injector;
@NgModule(
/* *** */
)
export class MyFeatureModule {
constructor(private injector: Injector) {
InjectorInstance = this.injector;
}
}
-----
@NgModule(
imports: [
...,
MyFeatureModule,
...
]
)
export class AppModule {}
Then in your custom class, import InjectorInstance
from the feature module and use it to get injectable reference: 然后在您的自定义类中,从功能模块导入
InjectorInstance
并使用它获取可注入的引用:
import {InjectorInstance} from 'path/to/my-feature-module.module';
export class CheckGroupsFilterValues {
private route = InjectorInstance.get<RouteParamsService>(RouteParamsService);
};
And don't forget to provide your service in root
并且不要忘了提供
root
服务
@Injectable({
providedIn: 'root'
})
export class RouteParamsService {...}
You need to pass the router through constructor 您需要通过构造函数传递路由器
constructor(
private _route: ActivatedRoute, // <-- Usefull
private _router: Router,
){}
https://angular.io/guide/router https://angular.io/guide/router
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.