【问题标题】:Under standingrelative and abolute path javascript了解相对路径和绝对路径 javascript
【发布时间】:2017-04-19 09:12:40
【问题描述】:

谁能帮助我理解相对路径和绝对路径的概念。我真的很困惑他们如何在我所在的目录中工作? 我有以下代码,我不能包含 PostService 模块。

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

【问题讨论】:

    标签: javascript angular path relative-path


    【解决方案1】:

    如果您设置了moduleId: module.id,,那么您的templateUrlstyleSheetsUrl 路径将相对于您所在的当前目录。

    因此,如果您使用../ 进入上一层,您将位于组件目录中。您需要再上一级才能进入服务文件夹所在的应用目录。

    所以路径应该是:../../services/post.service

    【讨论】:

    • @AhmadAbdullah 很高兴我能帮上忙。
    猜你喜欢
    • 2011-06-30
    • 2010-09-15
    • 1970-01-01
    • 2013-07-14
    • 2010-12-17
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 2012-01-11
    相关资源
    最近更新 更多