Angular2 templateUrl和styleUrls的相对路径? [英] Angular2 relative paths for templateUrl and styleUrls?

查看:218
本文介绍了Angular2 templateUrl和styleUrls的相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在搜索时,我找到了一个名为moduleId的东西来设置模板和CSS文件的相对路径,但是我不知道到底如何在angular2的组件中使用moduleId?

While searching I found something named as moduleId to set the relative paths of template and CSS files, but I don't know exactly how to use moduleId in our components of angular2?

实际上,问题出在我的文件夹结构中.我正在从dist文件夹加载我的所有.js文件,而我的视图(.html文件)在src文件夹中.因此,当我像这样使用moduleId: module.id时,angular从dist文件夹而不是src文件夹获取了路径.

Actually, the problem is in my folder structure.I am loading my all .js files from dist folder whereas my view(.html files) are in the src folder. So when I use moduleId: module.id like this angular took the path from dist folder, instead of src folder.

所以这里有人可以帮助我告诉我如何为组件angualr2设置自定义moduleId吗?

So anybody here helps me tell that How to set custom moduleId for my component angualr2?

我的文件夹结构是这样的.

My folder structure like this.

                                  App
                                  /\
                                 /  \
             (.js + .map files)Dist    Src(.ts + .html + .css files)

  • 文件夹距离包含所有.map和.js文件
  • 文件夹src包含所有.ts,.HTML和.css文件.
  • 实际编码(有效)-

    @Component({
        selector: 'class-timing',
        templateUrl: 'src/components/TimeTable/class-timing/class-timing.html',
        styleUrls: ['src/app.css']
    })
    

    修改的编码(由于路径错误而无法使用)-

    Modified coding (Not working due to incorrect path) -

    @Component({
        selector: 'class-timing',
        templateUrl: 'class-timing.html',
        moduleId: module.id,
        styleUrls: ['src/app.css']
    })
    

    参考本教程 http://schwarty.com/2015/12/22/angular2-relative-paths-for-templateurl-and-styleurls/

    推荐答案

    通过在运行时更改导入文件的路径(从srcdist)解决了我的问题:-

    Resolved my Problem by changing path (from src to dist)of importing file at the run time like this :-

    moduleId: module.id.replace("/dist/", "/src/")
    

    这样做可以更改模块ID的路径目录.

    By doing so you can change path directory of module ID.

    感谢@Nicolai对此很棒的评论

    thanks to @Nicolai for this awesome comment

    PS:-发布答案可能会帮助某人

    PS:- Posting as answer may help someone

    这篇关于Angular2 templateUrl和styleUrls的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆