3

Creating my first Angular 2 app I'm trying to set my template in an html file using templateUrl.

At the moment, I have the component.ts and the view.html in the same folder under src/ folder and I'm using this:

@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)

Any idea what I'm doing wrong?

4
  • Are you using webpack? Commented Oct 17, 2016 at 10:12
  • try templateUrl: './view.html' Commented Oct 17, 2016 at 10:12
  • @rinukkusu I'm using webpack Commented Oct 17, 2016 at 10:26
  • @LDJ I've done it already and it does not work. Commented Oct 17, 2016 at 10:27

4 Answers 4

3

Set the moduleId, like this:

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

For documentation, see Component-relative Paths

Sign up to request clarification or add additional context in comments.

1 Comment

I've made it already and I'm getting "moduleId should be a string"
2

Solved!

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

2 Comments

Could you give me your presets in webpack.config.js?. I have added require in my project. but I found another issue. I still cannot solve it.
Dude, this was 6 years ago xD. As you may imagine, I don't have it :D
1

Are you sure you are putting the same name as html file name?

Ex: file name is "myView.html"

in templateUrl: 'myView.html'

1 Comment

100% sure and verified
1

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.

1 Comment

@ZanattMan Then something else is wrong. Can you provide a plunkr?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.