Angular 将服务链接到组件
我正在我的组件中导入我的服务,但由于某些原因,它使我无法找到模块 这是我的选择hotel.component.tsAngular 将服务链接到组件,angular,Angular,我正在我的组件中导入我的服务,但由于某些原因,它使我无法找到模块 这是我的选择hotel.component.ts import { Component } from '@angular/core'; import { GetHotelService } from '/src/app/services/gethotel/getHotel.service'; //Error Here @Component({ moduleId: module.id, selector: 'sel
import { Component } from '@angular/core';
import { GetHotelService } from '/src/app/services/gethotel/getHotel.service'; //Error Here
@Component({
moduleId: module.id,
selector: 'selectHotel',
templateUrl: 'selectHotel.component.html',
providers: [GetHotelService]
})
export class selectHotelComponent { }
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class GetHotelService {
constructor(private http: Http){
console.log('Init');
}
}
这是我的getHotel.service.ts
import { Component } from '@angular/core';
import { GetHotelService } from '/src/app/services/gethotel/getHotel.service'; //Error Here
@Component({
moduleId: module.id,
selector: 'selectHotel',
templateUrl: 'selectHotel.component.html',
providers: [GetHotelService]
})
export class selectHotelComponent { }
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class GetHotelService {
constructor(private http: Http){
console.log('Init');
}
}
我的项目文件夹视图如下所示
项目>src>应用程序>组件>选择酒店>选择酒店.component.ts
项目>src>应用程序>服务>gethotel>gethotel.service.ts
我在这里做错了什么?导入路径应该是相对于当前文件的 尝试:
导入项目中编写的项目时,最好使用相对路径 因此,将GetHotelService导入更改为
import { GetHotelService } from '../../services/gethotel/getHotel.service';
前导的。
(或在其他情况下为/
)表示这是相对导入
PS.
不要太迂腐,但你可能想为你的项目看一个不同的文件夹结构——通常最好将与特定功能相关的项目分组在一起,而不是按类型分组。换句话说,将酒店服务与酒店组件放在同一文件夹中,而不是将所有服务分组在一起,并与组件分开
有关详细信息。您是否尝试过
“../../services/gethotel/gethotel.service”
?错误来自何处,确切的消息是什么@谢谢你。我不知道……可以用twice@SLASH没问题,我会回答的。谢谢。从现在起我会记住的