超越ct 2023-01-17 11:46 采纳率: 57.1%
浏览 8
已结题

vue中使用动态路由使用echart出现chart找不到

问题遇到的现象和发生背景

使用vue-admin-template框架进行动态路由设置,在一个子路由中使用了echart出现找不到echart设置的chart.vue

运行结果及详细报错内容

img

img

遇到的现象和发生背景,请写出第一个错误信息
<template>
    <div class="app-container">
      
      <el-form :inline="true" class="demo-form-inline">
  
        <el-form-item>
          <el-select v-model="searchObj.type" clearable placeholder="请选择">
            <el-option label="学员登录数统计" value="login_num"/>
            <el-option label="学员注册数统计" value="register_num"/>
            <el-option label="课程播放数统计" value="video_view_num"/>
            <el-option label="每日课程数统计" value="course_num"/>
          </el-select>
        </el-form-item>
  
        <el-form-item>
          <el-date-picker
            v-model="searchObj.begin"
            type="date"
            placeholder="选择开始日期"
            value-format="yyyy-MM-dd" />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="searchObj.end"
            type="date"
            placeholder="选择截止日期"
            value-format="yyyy-MM-dd" />
        </el-form-item>
        <el-button
          :disabled="btnDisabled"
          type="primary"
          icon="el-icon-search"
          @click="showChart()">查询</el-button>
      </el-form>
  
      <div class="chart-container">
        <div id="chart" class="chart" style="height:500px;width:100%" />
      </div>
    </div>
  </template>
  <script>
  import echarts from 'echarts'
  import staApi from '@/api/sta'
  
  export default {
      data() {
          return {
              searchObj:{},
              btnDisabled:false,
              xData:[],
              yData:[]
          }
      },
      methods:{
          showChart() {
              staApi.getDataSta(this.searchObj)
                  .then(response => {
                      console.log('*****************'+response)
                      this.yData = response.data.numDataList
                      this.xData = response.data.date_calculatedList
  
                      //调用下面生成图表的方法,改变值
                      this.setChart()
                  })
          },
          setChart() {
              // 基于准备好的dom,初始化echarts实例
              this.chart = echarts.init(document.getElementById('chart'))
              console.log(this.chart)
  
              // 指定图表的配置项和数据
              var option = {
                  title: {
                      text: '数据统计'
                  },
                  tooltip: {
                      trigger: 'axis'
                  },
                  dataZoom: [{
                      show: true,
                      height: 30,
                      xAxisIndex: [
                          0
                      ],
                      bottom: 30,
                      start: 10,
                      end: 80,
                      handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
                      handleSize: '110%',
                      handleStyle: {
                          color: '#d3dee5'
  
                      },
                      textStyle: {
                          color: '#fff'
                      },
                      borderColor: '#90979c'
                      },
                      {
                      type: 'inside',
                      show: true,
                      height: 15,
                      start: 1,
                      end: 35
                   }],
                  // x轴是类目轴(离散数据),必须通过data设置类目数据
                  xAxis: {
                      type: 'category',
                      data: this.xData
                  },
                  // y轴是数据轴(连续数据)
                  yAxis: {
                      type: 'value'
                  },
                  // 系列列表。每个系列通过 type 决定自己的图表类型
                  series: [{
                      // 系列中的数据内容数组
                      data: this.yData,
                      // 折线图
                      type: 'line'
                  }]
              }
  
              this.chart.setOption(option)
          }
      }
  }
  </script>
  
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
import { constantRoutes } from '@/router'
import { getMenu } from '@/api/login'
import Layout from '@/layout/index'

function filterAsyncRouter(asyncRouterMap) { // 遍历后台传来的路由字符串,转换为组件对象
    // console.log(asyncRouterMap)
    try {
        const accessedRouters = asyncRouterMap.filter(route => {
            if (route.component) {
                if (route.component === 'Layout') { // Layout组件特殊处理
                    route.component = Layout
                } else {
                    const component = route.component
                    route.component = resolve => {
                        require(['@/views' + component + '.vue'], resolve)
                    }
                }
            }
            if (route.children && route.children.length) {
                route.children = filterAsyncRouter(route.children)
            }
            return true
        })
        return accessedRouters
    } catch (e) {
        console.log(e)
    }
}

const state = {
    routes: [],
    addRoutes: []
}

const mutations = {
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    }
  }

const actions = {
    async generateRoutes({ commit }, roles) {
        // 取后台路由
        console.log('0000')
        const asyncRouter = await getMenu()
        // console.log(asyncRouter)

        return new Promise(resolve => {
            const tmp = asyncRouter.data.permissionList
            const accessedRoutes = filterAsyncRouter(tmp)
            // console.log(accessedRoutes)
            commit('SET_ROUTES', accessedRoutes)
            resolve(accessedRoutes)
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 已结题 (查看结题原因) 1月17日
    • 创建了问题 1月17日

    悬赏问题

    • ¥15 如何使用python 实现对串口/dev/ttyUSB0进行上锁,使得该串口只能在一个python脚本中使用,其他脚本不能操作这个串口
    • ¥15 晶体塑性有限元——Damask求解
    • ¥15 写出这个有没有人能写一下今天中午就要
    • ¥30 设计一个图形用户界面来控制你机械臂的运动
    • ¥30 3d打印机无法识别到SD卡,如何解决?(相关搜索:格式化)
    • ¥15 RPG游戏架构设计和开发方法
    • ¥15 前端返回pdf时不显示内容
    • ¥50 如何在不能联网影子模式下的电脑解决usb锁
    • ¥20 服务器redhat5.8网络问题
    • ¥15 如何利用c++ MFC绘制复杂网络多层图