vue.jsvue-componentvue-material

Vuejs single file components mixed with normal components


I am trying to mix vuejs single file components with the normal style of components (not sure what they are called) which I have existing code developed for already.

main.js

import Vue from 'vue'
import test from './test.vue'
import VueMaterial from 'vue-material'

Vue.use(VueMaterial)

new Vue({
  el: '#app',
  render: h => h(test, 
    {props: {
      testprop: 'ttttt'
    }
  }),
  data:{
    // /testprop: 'tytytytyty'
  }
})

test.vue

<template>
  <div>
    <my-component></my-component>
    <div>This is the first single page component</div>
  </div>
</template>

<script>
import MyComponent from '../src/components/MyComponent.js'
import TestTest from '../src/components/TestTest.vue'
  export default {
    name: 'MainApp',
    props: ['testprop'],
    components: {
            TestTest,
            MyComponent

    },
    mounted: function(){

    },
    computed:{
      returnProp: function(){
        return this.testprop
      }
    }
  }
</script>

<style lang="scss" scoped>
  .md-menu {
    margin: 24px;
  }
</style>

MyComponent.js Normal style component

window.Vue = require('Vue') //would give errors vue undefined if i dont't add this line
Vue.component('my-component', {
    name: 'my-component',
    template: '<div>Normal component</div>'
})

index.html

<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta content="width=device-width,initial-scale=1,minimal-ui" name="viewport">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic|Material+Icons">
    <link rel="stylesheet" href="https://unpkg.com/vue-material@beta/dist/vue-material.min.css">
    <link rel="stylesheet" href="https://unpkg.com/vue-material@beta/dist/theme/default.css">

    <title>vuematerial</title>
  </head>
  <body>
    <div id="app">
        <main-app :testprop="testprop"></main-app>
    </div>

    <script src="dist/build.js"></script>

  </body>
</html>

The single file component and a child single file component (not showed here) display fine. The normal type will show up as

<!--function (t,n,r,i){return Mt(e,t,n,r,i,!0)}-->

In the generated html.

Iv'e also tried doing the MyComponent import in the main.js file. Any ideas?

I don't really want to convert all my existing components into single file ones :(


Solution

  • According to the docs, a child component is an object, not something attached to the Vue instance (i.e Vue.component()) so declare your component like this:

    MyComponent.js

    export default {
        name: 'my-component',
        template: '<div>Normal component</div>'
    }
    

    If you want to keep the format of MyComponent as is then you'll need to register the component before the new Vue call.