4

I want to create a component in my components file, and then include this (like ng-include used to) in my base app.

my App.vue looks like this:

   <template>
    <div id="app">
     <div class="container-fluid">
      <div class="row>
       <navigation></navigation>
      </div>
      <div class="row">
       <router-view></router-view>
      </div>
     </div>
    </div>
   </template>

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

   <style>
    #app {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
    }
   </style>

And my main.js file looks like this:

   import Vue from 'vue'
   import App from './App'
   import VueRouter from 'vue-router'
   import homepage from './components/homepage'
   import navigation from './components/navigation'

   Vue.use(VueRouter)

   const routes = [
    { path: '/', component: homepage}
   ]

   const router = new VueRouter({
    routes,
    mode: 'history'
   })

   new Vue({

    el: '#app',
    template: '<App/>',
    components: { 
     App, navigation 
    },
    router
   }).$mount('#app')

navigation.vue:

    <template>
     <div>
      <p>test</p>
     </div>
    </template>

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

When I run the above I get:

[Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.

found in

---> at src/App.vue

I know how to add a vue.component, but i what I want is an external component, like the homepage, pulled from the components folder, and included in the base app.

4
  • Have you tried import homepage and put in components:{ App, homepage }? Commented Sep 25, 2017 at 14:47
  • I created a navigation.vue component in the components file. I then added it to { App, navigation } like so in the main.js file. I also added a 'import navigation from './components/navigation' ' at the top of the main.js file. I was given this as a response: [Vue warn]: Unknown custom element: <navigation> - did you register the component correctly? Commented Sep 25, 2017 at 14:59
  • can you update your question with yout new code? Commented Sep 25, 2017 at 15:04
  • I can do this: Vue.component('nav-test', { template: '<div>test</div>' }) and it works. But i need to pull the external components code into the template somehow. Commented Sep 25, 2017 at 15:13

1 Answer 1

6

For use the component in new Vue instance, or you use (like you have said) VUE.component, or you should import and register the component. see https://v2.vuejs.org/v2/guide/components.html#Local-Registration and you could try with in App.VUE:

    <template>
    <div id="app">
     <div class="container-fluid">
      <div class="row>
       <navigation></navigation>
      </div>
      <div class="row">
       <router-view></router-view>
      </div>
     </div>
    </div>
   </template>

   <script>
    import navigation from './components/navigation.vue'

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

   <style>
    #app {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
    }
   </style>

Where navigation.vue component can be like:

<template src="template.html"></template>
<script>
  export default {
    data(){
      return {} 
    }

  }
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

I edited the main question, take a look please and thank you.
thank you for that. i still get Uncaught ReferenceError: navigation is not defined
have you import correctly navigation component? with correct path? ( import navigation from 'path_component.vue') now the problem is that navigation component is not found.
my exact code: <script> import navigation from './components/navigation.vue' export default { name: 'app', components: { navigation } } </script>
No. The problem was removing the reference to the navigation component in the main.js, thats why it was not being found, because i moved the import over to the app.vue

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.