简体   繁体   English

Angular 2相对templateUrl不起作用

[英]Angular 2 relative templateUrl not working

Creating my first Angular 2 app I'm trying to set my template in an html file using templateUrl. 创建我的第一个Angular 2应用程序,我正在尝试使用templateUrl在html文件中设置模板。

At the moment, I have the component.ts and the view.html in the same folder under src/ folder and I'm using this: 目前,我在src /文件夹下的同一文件夹中有component.ts和view.html,我正在使用它:

@Component({
  selector: 'relative-path',
  templateUrl: 'view.html'
})

Once running the app the console say: 运行该应用程序后,控制台会说:

zone.js:1274GET http://localhost:3000/personalData.html 404 (Not Found) zone.js:1274GET http:// localhost:3000 / personalData.html 404(未找到)

Any idea what I'm doing wrong? 知道我在做什么错吗?

Set the moduleId , like this: 设置moduleId ,如下所示:

@Component({
  moduleId: module.id,
  selector: 'relative-path',
  templateUrl: 'some.component.html',
  styleUrls:  ['some.component.css']
})

For documentation, see Component-relative Paths 有关文档,请参阅组件相对路径

Are you sure you are putting the same name as html file name? 您确定要输入与html文件名相同的名称吗?

Ex: file name is "myView.html" 例如:文件名为“ myView.html”

in templateUrl: 'myView.html' 在templateUrl中:“ myView.html”

You need to add ./ and it will work. 您需要添加./ ,它将起作用。

@Component({
  selector: 'relative-path',
  templateUrl: './view.html'
})

It's a lot easier to work with an absolute path and you will most likely never need a relative path when trying to find a template for a component if you follow the official style guide. 使用绝对路径要容易得多,而且如果遵循官方的样式指南,则在尝试为组件查找模板时很可能不需要相对路径。

Solved! 解决了!

@Component({
  selector: 'relative-path',
  template: require('view.html')
})

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

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