Angular 角度2注入到另一个服务的服务未定义
我正在尝试创建一个小框架,使我更容易实现CRUD功能 因此,我创建了一个BaseHttpService,如下所示:Angular 角度2注入到另一个服务的服务未定义,angular,typescript,dependency-injection,Angular,Typescript,Dependency Injection,我正在尝试创建一个小框架,使我更容易实现CRUD功能 因此,我创建了一个BaseHttpService,如下所示: ... <imports> ... @Injectable() export class BaseHttpService { constructor( private http: Http ) { console.log('ctor'); this.apiUrl$.subscribe(apiUrl => this.rootApiUr
... <imports> ...
@Injectable()
export class BaseHttpService {
constructor(
private http: Http
) {
console.log('ctor');
this.apiUrl$.subscribe(apiUrl => this.rootApiUrl = apiUrl.removeTrailingSlash());
}
public getPagedResultOfType<T extends IBaseEntity>(url: string, vm: ISearchVm<T>, options?: RequestOptions): Observable<PagedResult<T>>{ ... <implementation> ... }
... <other methods> ...
}
import { BaseHttpService} from '../servicePath';
这是我目前拥有的,但这不起作用。我得到的错误是:
无法读取未定义的属性“getPagedResultOfType”
如何将BaseHttpService注入我的BaseAppsumentyReadOnlyService?如下导入您的服务:
... <imports> ...
@Injectable()
export class BaseHttpService {
constructor(
private http: Http
) {
console.log('ctor');
this.apiUrl$.subscribe(apiUrl => this.rootApiUrl = apiUrl.removeTrailingSlash());
}
public getPagedResultOfType<T extends IBaseEntity>(url: string, vm: ISearchVm<T>, options?: RequestOptions): Observable<PagedResult<T>>{ ... <implementation> ... }
... <other methods> ...
}
import { BaseHttpService} from '../servicePath';
然后在构造函数中这样声明它:
constructor(
private baseHttpService: BaseHttpService
) {
console.log(this.baseHttpService);
}
让我知道它是否像这样工作,如果不是,请复制/粘贴控制台错误:D
编辑1:
@NgModule({
declarations: [
myComponent
],
imports: [
CommonModule,
HttpModule
],
providers: [
BaseHttpService,
BaseAppSumEntityReadOnlyService,
]
})
export class AppSumFrameworkModule { }
服务:BaseHttpService
... <imports> ...
@Injectable()
export class BaseHttpService {
constructor(
private http: Http
) {
console.log('ctor');
this.apiUrl$.subscribe(apiUrl => this.rootApiUrl = apiUrl.removeTrailingSlash());
}
public getPagedResultOfType<T extends IBaseEntity>(url: string, vm: ISearchVm<T>, options?: RequestOptions): Observable<PagedResult<T>>{ ... <implementation> ... }
... <other methods> ...
}
@Injectable()
export class BaseAppSumEntityReadOnlyService<T extends IBaseEntity>{
constructor(
private baseHttpService: BaseHttpService
) {
console.log('ro ctor');
console.log(this.baseHttpService); // <-- this is always undefined
}
getAll(vm: ISearchVm<T>): Observable<PagedResult<T>>{
const url = `${this.baseUrl}`;
console.log(this.baseHttpService);
return this.baseHttpService.getPagedResultOfType<T>(url, vm); // <-- this fails because baseHttpService is undefined: Cannot read property 'getPagedResultOfType' of undefined
}
... <more methods>
}
... <imports> ...
@Component()
export class myComponent {
constructor(
private baseAppService: BaseAppSumEntityReadOnlyService
) {
console.log(this.baseAppService);
}
}
模块:
@NgModule({
declarations: [
myComponent
],
imports: [
CommonModule,
HttpModule
],
providers: [
BaseHttpService,
BaseAppSumEntityReadOnlyService,
]
})
export class AppSumFrameworkModule { }
组件:myComponent
... <imports> ...
@Injectable()
export class BaseHttpService {
constructor(
private http: Http
) {
console.log('ctor');
this.apiUrl$.subscribe(apiUrl => this.rootApiUrl = apiUrl.removeTrailingSlash());
}
public getPagedResultOfType<T extends IBaseEntity>(url: string, vm: ISearchVm<T>, options?: RequestOptions): Observable<PagedResult<T>>{ ... <implementation> ... }
... <other methods> ...
}
@Injectable()
export class BaseAppSumEntityReadOnlyService<T extends IBaseEntity>{
constructor(
private baseHttpService: BaseHttpService
) {
console.log('ro ctor');
console.log(this.baseHttpService); // <-- this is always undefined
}
getAll(vm: ISearchVm<T>): Observable<PagedResult<T>>{
const url = `${this.baseUrl}`;
console.log(this.baseHttpService);
return this.baseHttpService.getPagedResultOfType<T>(url, vm); // <-- this fails because baseHttpService is undefined: Cannot read property 'getPagedResultOfType' of undefined
}
... <more methods>
}
... <imports> ...
@Component()
export class myComponent {
constructor(
private baseAppService: BaseAppSumEntityReadOnlyService
) {
console.log(this.baseAppService);
}
}
。。。
@组件()
导出类myComponent{
建造师(
专用baseAppService:BaseAppsumentyReadOnlyService
) {
console.log(this.baseAppService);
}
}
这是当前的操作方式:从“/base http.service”导入{BaseHttpService}@Injectable()导出类BaseAppsumentyReadOnlyService{constructor(受保护的baseHttpService:baseHttpService){console.log('ro-ctor-');console.log(this.baseHttpService);}}//控制台日志给出:'ro-ctor-'UndeinedChange(在那里为私有进行第二次受保护的
并重新编译应用程序。查看问题是否仍然存在。我会继续调查你的问题,看看能不能找到问题所在is@AppSum在您的示例代码中,我看到这个@Inject(forwardRef(()=>BaseHttpService))公共BaseHttpService:BaseHttpService
。您当时使用的是哪个版本?你的OP上的那个还是这条评论上的那个?将其更改为private会得到相同的结果。(为了让您知道为什么,我将其设置为受保护,以便能够在将实现其余CRUD功能并扩展此BaseReadOnlyService的服务中使用它)我使用Angular CLI,因此更改代码和测试不同的内容非常容易。我尝试了forwardRef和专用的baseHttpService:baseHttpService。结果是一样的。