Vuejs - 无法安装组件:模板或渲染函数未定义

时间:2018-05-18 15:26:41

标签: vue.js vuejs2 vue-component

我不确定我做错了什么。我收到以下错误:

控制台中的

错误消息:

[Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <VueChartjs>
       <Chatjsvue> at src\components\vueChartjs\Chatjsvue.vue
         <App> at src\App.vue
           <Root>

index.js

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'


Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      // component: HelloWorld
      component: require('../components/HelloWorld.vue').default
    }
  ]
})

App.vue

<template>
  <div id="app" class="container">
    <img src="./assets/logo.png" class="logo">

    <!-- chartjs -->
    <div class="chartjsvue">
      <Chatjsvue></Chatjsvue>
    </div>
    <div class="clear"></div>
    <!-- chartjs -->

  </div>
</template>

<script>
import Chatjsvue from '@/components/vueChartjs/Chatjsvue'

export default {
  name: 'App',
  components: {
    Chatjsvue
  }
}
</script>

Chatjsvue.vue

<template src="../../views/chartjshtml/chartsjs.html"></template>

<script>
  import Chartjsvue from '@/assets/javascripts/chartjs'

  export default {
    components: {
      'vue-chartjs': Chartjsvue 
    }
  };
</script>

chartsjs.html

<div class="wrapper">
  <vue-chartjs></vue-chartjs>
</div>

chartjs.js

file is rmpty- no code in the file

提到的错误是什么以及解决它需要做些什么?

3 个答案:

答案 0 :(得分:1)

我认为问题是你的chartjs.js是空的。你需要这样做:

import template from './chartjs.html' // in your questions it's chartsjs.html, please help to correct it
export default {
  template: template
}

答案 1 :(得分:1)

您的chartjs.js文件不应为空。它应该是一个Vue组件,其中包含可以呈现的模板。任何javascript都可以在脚本标签中自行编写。

组件对象应该只包含您需要在当前组件中使用的vue组件列表。每个组件都必须有一个模板。

答案 2 :(得分:0)

感谢所有为回答做出贡献的人。 js文件不应为空。这是显示chartjs所有图表的完整代码

main.js [src / main.js]

import Vue from 'vue'
import App from './App'
import router from './router'
import ChartJsPluginDataLabels from 'chartjs-plugin-datalabels'

Vue.config.productionTip = false

require('./assets/stylesheets/application.css')
require('./assets/javascripts/application.js')
require('./assets/javascripts/chartjs.js')


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { 
    App,
    ChartJsPluginDataLabels
  },
  template: '<App/>'
})

App.vue [src / App.vue]

<template>
  <div id="app" class="container">
    <!-- chartjs -->
    <div class="chartjsvue tab-content active" id="tab2">
      <Chatjsvue></Chatjsvue>
    </div>
    <div class="clear"></div>
    <!-- chartjs -->
  </div>
</template>

<script>
import Chatjsvue from '@/components/vueChartjs/Chatjsvue'
export default {
  name: 'App',
  components: {
    Chatjsvue
  }
}
</script>

Chatjsvue.vue [src / components / Chatjsvue.vue]

<template src="../../views/chartjshtml/chartsjs.html"></template>

<script>
  import Chartjsbarvue from '@/assets/javascripts/chartjsbar'
  export default {
    components: {
      'vue-chartbarjs': Chartjsbarvue   
    },
    mounted(){
      console.log('Data is chartjs',this)
    },
    methods: {},
  }
</script>

chartsjs.html [src / views / chartjshtml / chartsjs.html]

<div class="wrapper">
  <div class="chart_header">chartjs bar chart</div>
  <vue-chartbarjs></vue-chartbarjs>
</div>

chartjsbar.js [src / assets / javascripts / chartjsbar.js]

/*===================================================
    File: Chartsjsvue.vue
  ===================================================*/
import { Bar } from 'vue-chartjs'

export default {
  extends: Bar,
  data () {
    return {
      datacollection: {
        labels: ['VueJs', 'EmberJs', 'ReactJs', 'AngularJs'],
        datasets: [
          {
            label: '1st Dataset hello',
            backgroundColor: 'rgba(52,65,84, 0.3)',
            bordercolor: '#344154"',
            hoverBackgroundColor: "#344154",
            data: [40, 20, 12, 39]
          },
          {
            label: '2nd Dataset',
            backgroundColor: 'rgba(130,191,163, 0.5)',
            bordercolor: '#82BFA3"',
            hoverBackgroundColor: "#82BFA3",
            data: [50, 70, 22, 55]
          }
        ]
      },
      options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
          datalabels: {
            display: false
          }
        }
      }
    }
  },
  mounted () {
    this.renderChart(this.datacollection, this.options)
  }
}

enter image description here