简体   繁体   English

templateUrl和styleUrls的Angular2相对路径?

[英]Angular2 relative paths for templateUrl and styleUrls?

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

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. 实际上,问题出在我的文件夹结构中。我从dist文件夹加载我的所有.js文件,而我的视图(.html文件)在src文件夹中。 So when I use moduleId: module.id like this angular took the path from dist folder, instead of src folder. 所以当我使用moduleId: module.id ,这个angular从dist文件夹中获取路径,而不是src文件夹。

So anybody here helps me tell that How to set custom moduleId for my component angualr2? 所以这里有人帮我告诉我如何为我的组件angualr2设置自定义moduleId?

My folder structure like this. 我的文件夹结构是这样的。

                                  App
                                  /\
                                 /  \
             (.js + .map files)Dist    Src(.ts + .html + .css files)
  • Folder Dist containes all .map and .js files Folder Dist包含所有.map和.js文件
  • Folder src containes all .ts, .HTML, and .css file. 文件夹src包含所有.ts,.HTML和.css文件。

Actual coding (working) - 实际编码(工作) -

@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']
})

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

Resolved my Problem by changing path (from src to dist )of importing file at the run time like this :- 通过在运行时更改导入文件的路径(从srcdist )解决了我的问题,如下所示: -

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

By doing so you can change path directory of module ID. 通过这样做,您可以更改模块ID的路径目录。

thanks to @Nicolai for this awesome comment 感谢@Nicolai这篇精彩的评论

PS:- Posting as answer may help someone PS: - 发布答案可能对某人有所帮助

I also had some questions about what worked and didn't work for TemplateUrl paths. 我还有一些关于TemplateUrl路径有效和无效的问题。 I found the following was true in my testing: 我在测试中发现以下情况属实:

templateUrl: 'folder name / file name' did not work templateUrl:'文件夹名称/文件名'不起作用

templateUrl: '/folder name / file name' did not work templateUrl:'/文件夹名称/文件名'不起作用

templateUrl: './folder name / file name' DID work templateUrl:'。/ folder name / file name'DID工作

so you might try that out. 所以你可以尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM