Programing

Vue.js Uncaused TypeError: _vueChartjs.선.extend가 함수가 아님

c10106 2022. 3. 7. 21:19
반응형

Vue.js Uncaused TypeError: _vueChartjs.선.extend가 함수가 아님

Vue.js와 웹팩을 시작하는 중.내 프로젝트에 vue-charts 기능을 추가하려고 해.다음 오류가 수신됨:

Uncaught TypeError: _vueChartjs.Line.extend is not a function
at Object.defineProperty.value (..\..\CommodityChart.vue:5)
at __webpack_require__ (bootstrap 7040a42393b737f78245:659)
at fn (bootstrap 7040a42393b737f78245:85)
at Object.<anonymous> (CommodityChart.vue:3)
at __webpack_require__ (bootstrap 7040a42393b737f78245:659)
at fn (bootstrap 7040a42393b737f78245:85)
at Object.defineProperty.value (..\..\fetch-data.vue:36)
at __webpack_require__ (bootstrap 7040a42393b737f78245:659)
at fn (bootstrap 7040a42393b737f78245:85)
at Object.<anonymous> (fetch-data.vue:7)

내 소포로json

"dependencies": {
"axios": "^0.15.3",
"bootstrap-vue": "^1.0.0-beta.9",
"chart.js": "^2.7.1",
"core-js": "^2.4.1",
"font-awesome": "^4.6.3",
"vue": "^2.1.8",
"vue-chartjs": "^3.0.0",
"vue-router": "^2.1.1",
"vue-server-renderer": "^2.1.8",
"vue-template-compiler": "^2.1.8",
"vuex": "^2.1.1",
"vuex-router-sync": "^4.0.1"

},

node_modules 폴더(chart.js 및 vue-chartjs)에서 종속성을 볼 수 있다.

오류를 발생시키는 내 .vue 파일은 다음과 같다.

<script>
  //Importing Line class from the vue-chartjs wrapper
  import { Line } from 'vue-chartjs'
  //Exporting this so it can be used in other components
  export default Line.extend({
    data () {
      return {
        datacollection: {
        //Data to be represented on x-axis
          labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
          datasets: [
            {
              label: 'Data One',
              backgroundColor: '#f87979',
              pointBackgroundColor: 'white',
              borderWidth: 1,
              pointBorderColor: '#249EBF',
              //Data to be represented on y-axis
              data: [40, 20, 30, 50, 90, 10, 20, 40, 50, 70, 90, 100]
            }
          ]
        },
        //Chart.js options that controls the appearance of the chart
        options: {
          scales: {
            yAxes: [{
              ticks: {
                beginAtZero: true
              },
              gridLines: {
                display: true
              }
            }],
            xAxes: [ {
              gridLines: {
                display: false
              }
            }]
          },
          legend: {
            display: true
          },
          responsive: true,
          maintainAspectRatio: false
        }
      }
    },
    mounted () {
    //renderChart function renders the chart with the datacollection and options object.
      this.renderChart(this.datacollection, this.options)
    }
  })
</script>

차트 라이브러리를 항목 js 파일의 다른 위치에 가져오거나 참조해야 하는가?웹 팩 참조?chart.vue 파일 없이도 프로젝트는 잘 되고 있다.

차트 구성 요소를 생성하기 위한 구문이 최신 버전에서 변경됨(3.0.0(vue-chartjs), 따라서 오류가 발생함.

새 구문에 따라 다음과 같이 차트 구성 요소를 생성하십시오.

import { Line } from 'vue-chartjs';

export default {
   extends: Line,
   data() {
      return {
         datacollection: {
            //Data to be represented on x-axis
            labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
            datasets: [{
               label: 'Data One',
               backgroundColor: '#f87979',
               pointBackgroundColor: 'white',
               borderWidth: 1,
               pointBorderColor: '#249EBF',
               //Data to be represented on y-axis
               data: [40, 20, 30, 50, 90, 10, 20, 40, 50, 70, 90, 100]
            }]
         },
         //Chart.js options that controls the appearance of the chart
         options: {
            scales: {
               yAxes: [{
                  ticks: {
                     beginAtZero: true
                  },
                  gridLines: {
                     display: true
                  }
               }],
               xAxes: [{
                  gridLines: {
                     display: false
                  }
               }]
            },
            legend: {
               display: true
            },
            responsive: true,
            maintainAspectRatio: false
         }
      }
   },
   mounted() {
      //renderChart function renders the chart with the datacollection and options object.
      this.renderChart(this.datacollection, this.options)
   }
}

자세한 내용은 공식 문서를 참조하십시오.

이것이 내가 vue.js와 laravel 프로젝트에 라인 차트를 추가한 방법이다.내가 한 일은 매달 완성 작업을 세어 라인 차트 그래프로 보여주는 것이다.

먼저 구성요소 디렉토리에서 꺽은선형 차트의 구성요소를 설정해야 한다.LineChart.js라는 이름의 파일을 저장하십시오.이 폴더는 다음을 포함한다.

import { Line, mixins } from 'vue-chartjs'
const { reactiveProp } = mixins

export default {
extends: Line,
mixins: [reactiveProp],
props: ['options'],
mounted () {
    // this.chartData is created in the mixin.
    // If you want to pass options please create a local options object
    this.renderChart(this.chartData, this.options)
 }
}

그러면 너는 너의 vue 파일에 코드를 써야 해.

<div class="display">
 <line-chart :chart-data="datacollection"></line-chart>

 mounted() {

        this.LoadCompleteTaskbyMonth();
    }

 data() {
        return {
            months: null,
            month_data: null,
            datacollection: null
        }
    }



methods:{
       LoadCompleteTaskbyMonth()
          {
           this.$Helpers.Get('task-complete', {},this.$store.state.token)
          .then((response) => {
           this.months = response.map(a => a.month);
           this.month_data = response.map(a => a.value);
           this.datacollection = {
           labels:this.months,
datasets: [
           {
             label: 'Completed Task',
             data:this.month_data
           }
          ]
   }
 })
}
}

그리고 이것이 쿼리 기능이다.

 public function completeTask()
{
    $data = array();
    $data = DB::table('tasks')
        ->select(
            DB::raw("DATE_FORMAT(tasks.created_at,'%Y-%m') AS month2"),
            DB::raw("DATE_FORMAT(tasks.created_at,'%Y-%M') AS month"),
            DB::raw('count(*) AS value')
        )
        ->groupBy('month','month2')
        ->orderBy('month2','ASC')
        ->where('task_status_id', '=', 5)
        ->get();
    return response()->json($data);
}

참고:태스크 완료는 URL임

참조URL: https://stackoverflow.com/questions/47078430/vue-js-uncaught-typeerror-vuechartjs-line-extend-is-not-a-function

반응형