4

I'm trying to use a Vue component (a header) inside of another component, but it's not rendering it correctly.

My code:

App.vue:

<template>
  <div id="app">
    <header></header>
  </div>
</template>

<script>
export default {
  name: 'app'
}
</script>

Header.vue:

<template>
    <nav class="nav">
         <div class="container">
             <div class="nav-left">
                 <p class="title"></p>
             </div>
         </div>
     </nav>
</template>

<script>
export default {
  name: 'header'
}
</script>

And, I register my component in main.js, with the file looking like this:

import Vue from 'vue'
import App from './App'
import router from './router'

Vue.component('header', require('./components/Header'))

Vue.config.productionTip = false

new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})

With my very limited understanding this should be like rendering a React <Header /> in my App, but this is not working that way. What's getting rendered to the DOM is a plain <header></header> tag inside of my #app, which isn't exactly ideal.

What key point am I missing? Thanks!

2
  • are you sure the ./components/Header is correct? because your code should work. also try adding .vue to end of that path. Commented Sep 17, 2017 at 15:18
  • Thanks for the effort. You are right -- it should work if I wasn't stupid enough to name my component header, which is an existing HTML tag. Commented Sep 18, 2017 at 16:10

1 Answer 1

15

You cannot name a component with the name of an existing HTML tag. The development version of Vue will warn you of this in the console.

[Vue warn]: Do not use built-in or reserved HTML elements as component id: header

Rename your header component with some other name.

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

3 Comments

Awesome, you are right. Thank you -- so many hours lost on this!
is the name: '' all we have to change? can the filename still include an html-like name? e.g. 'Footer.vue', and also in the import Footer from ./components/Footer do these also have to be renamed? or only in Vue instance name definition
IME, only the instance name needs to be changed; the component filename can still be whatever you want. For example, I have a Map.vue component, and when declared with name: 'map', I get the Vue warning as Bert described above. Changing it to something else like name: 'mapComp' cleared the error. In either case, my app was still functional, but it's better to heed warnings, at the very least to keep exercising semantic HTML markup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.