2016-12-05 71 views
2

有人可以幫助我理解相對和絕對路徑的概念。我真的很困惑他們是如何在我的目錄中工作的? 我有下面的代碼,我不能包含PostService模塊。根據站立和絕對路徑javascript

import { Component } from '@angular/core'; 
import { PostService } from '../services/post.service'; 

@Component({ 
moduleId: module.id, 
selector: 'search', 
templateUrl: 'search.component.html', 
providers:[PostService] 
}) 

export class SearchComponent { 
    posts: post[]; 

    constructor(){ 
       this.posts = [ 
     { 
      id: 1, 
      title:"Post heading", 
      body: "They want a self-starter. They don't need more handholding than necessary. They want someone whos not a script kiddy, who knows solid software engineering fundamentals. They want someone who will work well with the team, and be proactive in improving the situation, rathe t just playing the victim when things go wrong." 
     }, 
        { 
      id: 2, 
      title:"Post heading", 
      body: "They want a self-starter. They don't need more handholding than necessary. They want someone whos not a script kiddy, who knows solid software engineering fundamentals. They want someone who will work well with the team, and be proactive in improving the situation, rathe t just playing the victim when things go wrong." 
     }, 
        { 
      id: 3, 
      title:"Post heading", 
      body: "They want a self-starter. They don't need more handholding than necessary. They want someone whos not a script kiddy, who knows solid software engineering fundamentals. They want someone who will work well with the team, and be proactive in improving the situation, rathe t just playing the victim when things go wrong." 
     } 
    ] 
    } 
} 

文件結構如下:

-- app 
    '-- components 
     '-- search 
      '-- search.component 
    '-- services 
     '-- post.service 

回答

1

如果設置moduleId: module.id,那麼你templateUrlstyleSheetsUrl路徑成爲相對於當前目錄你在

所以,如果你去1上級別爲../您將位於組件目錄中。爲了進入服務文件夾所在的應用程序目錄,您需要再上一層。

所以路徑應該是:../../services/post.service

+1

謝謝@echonax –

+0

@AhmadAbdullah很高興我能幫忙。 – echonax