• Echarts与后台(mongoose)交互


    Echarts引入地址可参考

    echarts组件引入

    1. <script setup>
    2. import { onMounted, ref } from 'vue';
    3. import * as echarts from 'echarts';
    4. import api from '@/components/utils/api'
    5. const boxlist = ref([])
    6. const mc = ref([])
    7. const jg = ref([])
    8. const chafen = () => {
    9. boxlist.value.forEach(item => {
    10. mc.value.push(String(item.name))
    11. jg.value.push(Number(item.price))
    12. console.log(mc.value, jg.value);
    13. // console.log(item);
    14. });
    15. }
    16. const zhuxing = () => {
    17. // 基于准备好的dom,初始化echarts实例
    18. var myChart = echarts.init(document.getElementById('main'));
    19. // 指定图表的配置项和数据
    20. var option = {
    21. title: {
    22. text: 'ECharts与后台交互示例'
    23. },
    24. tooltip: {},
    25. legend: {
    26. data: ['销量']
    27. },
    28. xAxis: {
    29. data: mc.value
    30. },
    31. yAxis: {},
    32. series: [
    33. {
    34. name: '销量',
    35. type: 'bar',
    36. data: jg.value
    37. }
    38. ]
    39. };
    40. // 使用刚指定的配置项和数据显示图表。
    41. myChart.setOption(option);
    42. }
    43. onMounted(async () => {
    44. const { data: { data } } = await api.get('zx')
    45. boxlist.value = data
    46. chafen()
    47. zhuxing()
    48. })
    49. script>
    50. <style lang="scss" scoped>style>

    前端代码

    1. <script setup lang="ts">
    2. import { reactive, toRefs, onMounted,ref,computed } from 'vue'
    3. import CountUp from 'vue-countup-v3'
    4. import type { ICountUp, CountUpOptions } from 'vue-countup-v3'
    5. import zhuxing from '@/components/gundong/zhuxing.vue'
    6. import api from '@/components/utils/api'
    7. const count=ref(null)
    8. const data = reactive({
    9. startVal: 0, // 开始值
    10. endVal: computed(()=>([count.value, 5000, 10000])), // 结束值 -- 可以写成动态的
    11. duration: 5, // 跳动时长 - 单位:秒
    12. decimals: 0, // 小数点位数
    13. countUp: undefined as ICountUp | undefined, // 跳动的对象
    14. // countUps: [] as Array | [], // 跳动的对象数组
    15. options: {
    16. // 配置分隔符
    17. separator: '❤️'
    18. } as CountUpOptions
    19. })
    20. const onInit = (ctx: ICountUp) => {
    21. data.countUp = ctx
    22. console.log(`开始`, ctx)
    23. }
    24. const onFinished = () => {
    25. console.log('结束')
    26. }
    27. const { endVal, duration, decimals, options } = toRefs(data)
    28. onMounted(async () => {
    29. const {data:{data}}=await api.get('zx')
    30. count.value=data.length
    31. console.log(count.value);
    32. // onInit()
    33. // onFinished()
    34. })
    35. script>
    36. <style lang="less" scoped>
    37. .body {
    38. .box {
    39. display: flex;
    40. justify-content: flex-start;
    41. // font-weight: bold;
    42. .count {
    43. font-size: 20px;
    44. font-weight: bold;
    45. margin: 0 30px;
    46. }
    47. }
    48. }
    49. style>

    后端代码

    后端配置参考地址

    1. var express = require("express");
    2. var router = express.Router();
    3. const mongoose = require("mongoose");
    4. const Zhuxing = mongoose.model("zhuxing",{name:String,price:Number},'zhuxing')
    5. // Zhuxing.create({
    6. // name:'周日',
    7. // price:6000
    8. // })
    9. router.get("/zx", async function (req, res, next) {
    10. const data = await Zhuxing.find();
    11. // console.log(data);
    12. res.send({ code: "200", message: "zx show ok", data });
    13. });
    14. module.exports = router;

    效果查看

  • 相关阅读:
    【C++】list
    随机手机号查询易语言代码
    PMC在制造企业中发挥哪些价值?
    后端防止重复点击
    【JavaEE初阶】多线程 _ 基础篇 _ 阻塞队列(案例二)
    数据血缘分析-Python代码的智能解析
    黑马C++ 01基础 —— 指针、结构(重点和难点)
    Bugku MISC easy_nbt & telnet
    palywright: API测试(APIRequestContext, APIResponse)
    Leetcode 17. 电话号码的字母组合
  • 原文地址:https://blog.csdn.net/m0_74060440/article/details/136263789