4
votes

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.

1
Have you tried import homepage and put in components:{ App, homepage }?LorenzoBerti
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?Josh Winters
can you update your question with yout new code?LorenzoBerti
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.Josh Winters

1 Answers

6
votes

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://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>