前端笔记-vue cli中v-bind动态数据实时更新
目录
基本概念
代码
基本概念
如下的例子,刚开始运行:
点击按钮:
数据库修改下数据:
再点击按钮刷新下:
下面给出请求的json数据:
刷新有2个方式
第一种是强制刷新,这样是有问题的,数据和图表不能同步:
this.$forceUpdate()
本人推荐下面这种方式,通过绑定组建的key值
<barChartItem ref="barItem" v-bind:count="numCount" v-bind:valueList="valueList" v-bind:key="valueList.toString()"></barChartItem>
把key绑定成变动的值,就可以实时刷新了
代码
程序结构如下:
关键代码
BarCharts.vue
<template><div><div id="main" style="width: 500px; height:500px"></div></div>
</template><script>
export default {props: ['count', 'valueList'],watch:{count:val => {alert(this.count)}},mounted(){this.initGra();},methods: {//开始画图了initGra(){// 基于准备好的dom,初始化echarts实例let myChart = this.$echarts.init(document.getElementById('main'));this.getData();},getData(){if (myChart != null && myChart != "" && myChart != undefined) {myChart.dispose();}// 基于准备好的dom,初始化echarts实例let myChart = this.$echarts.init(document.getElementById('main'));//初始化数据let dataList = [];for(let i = 0; i < this.count; i++){dataList.push(this.valueList[i]);}console.log(this.count)console.log(this.valueList)//alert(this.count)//alert(this.valueList)// 指定图表的配置项和数据let option = {title: {text: 'ECharts 入门示例'},tooltip: {},legend: {data:['销量']},xAxis: {data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]},yAxis: {},series: [{name: '销量',type: 'bar',data: dataList}]};// 使用刚指定的配置项和数据显示图表。myChart.setOption(option);}}
}
</script><style scoped>.item{color: green;}
</style>
main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Axios from 'axios'
import echarts from 'echarts'
import TestEChart from './TestEChart'Vue.config.productionTip = false
Vue.prototype.$axios=Axios
Vue.prototype.$echarts = echarts/* eslint-disable no-new */
new Vue({el: '#app',components: { TestEChart },template: '<TestEChart/>',
})
TestEChart.vue
<template><div><barChartItem ref="barItem" v-bind:count="numCount" v-bind:valueList="valueList" v-bind:key="valueList.toString()"></barChartItem><div><button @click="handleSubmit">获取</button></div></div>
</template><script>import BarChartItem from './components/BarCharts'export default {data() {return {numCount: 0,valueList: []}},components: {'barChartItem' : BarChartItem},methods: {handleSubmit(){this.$axios({type: 'get',url: '/gra/list'}).then(res => {//console.log(res.data.data)let jsonTest = res.data.data.content//目前只有1条数据 展示这么写this.numCount = jsonTest[0].countthis.valueList = jsonTest[0].value;//console.log(this.numCount)//console.log(this.valueList)this.$refs.barItem.getData()//this.$set()//this.$forceUpdate()}).catch(err=>{alert(err)})},}
}
</script><style></style>
前端笔记-vue cli中v-bind动态数据实时更新相关推荐
- 前端笔记-vue cli中axios批量发送get和post请求及注意事项
目录 基本概念 关键代码 基本概念 这里安装好axios后,还要安装qs npm install --save qs 在vue cli中在created里面调用方法,他会在页面加载完成后自动调用! 批 ...
- 前端笔记-vue cli中使用router-link进行路由跳转
目录 以前的方法 新方法 以前的方法 以前是通过router的push进行跳转,代码如下: <template><div class="XXXXXXXXXXXXXXXXX& ...
- 前端笔记-Vue cli中使用axios后JavaScript变量传输给vue变量要注意的地方
目录 axios.all中的注意事项 为什么要采用全局的JavaScript变量给vue变量 axios.all中的注意事项 如下的请求: methods:{getData(){axios.all([ ...
- 前端笔记-vue cli中使用echarts画江苏省地图
目录 基本概念 代码与实例 基本概念 这里使用echarts,框架用vue cli,下面这个链接是vue cli导入echarts https://blog.csdn.net/qq78442761/a ...
- Web前端笔记-vue cli中使用echarts加载geo地图
效果是这样的: 此处是使用echarts加载geo地图. 这里简单说下逻辑: 首先npm下个包: npm install echarts 然后用个vue组件封装下. 如下代码: <templat ...
- 前端笔记-vue cli使用echarts
目录 过程 代码与实例 过程 首先下载echarts cnpm install echarts -S //或者是 cnpm install echarts --save 下载好后: 在main.js中 ...
- 前端笔记-vue cli为web添加底纹
目录 基本概念 代码与实例 基本概念 这里主要是使用了css预处理 首先下载包 npm install stylus stylus-loader 随后创建文件,然后写代码 这里还要导入代码: 这里还要 ...
- 前端笔记-vue cli引入sementic-ui(sementic-ui-vue)
运行截图如下: 原文链接如下: https://semantic-ui-vue.github.io/#/ 下面给出截图,部分网络不能访问
- Leaflet笔记-把leaflet-tilelayer-wmts移植到vue cli中(含思路)
目录 前言 过程 前言 关于leaflet的webpackage使用npm安装官方是有明显的解析 但是关于插件特别是TileLayer.WMTS是不提供的,但提供了源码,可以稍微修改下,就能在vue ...
最新文章
- Python 来分析,堪比“唐探系列”!B站9.5分好评如潮!
- linux 重启udev服务,Linux 7.x 中 UDEV 生效的方法
- UVa 1393 (容斥原理、GCD) Highways
- tensorflow学习(2.网络模型的存储以及提取)
- 基于springboot2.x集成缓存注解及设置过期时间
- Feature Flag 功能发布控制
- 一起来做个免登录资源导航小程序!
- 开源 | 近期遥感航空影像的检索、检测、跟踪、计数、分类相关工作
- ruby on rails ajax select
- 手绘水彩卡通插画 | 艺术品因有灵魂而珍藏
- java解压zip异常_zip 文件解压缩问题解决 java.util.zip.ZipException:error in opening zip file | 学步园...
- 微信小程序入门指南——程序员计算器设计(一)
- php微信抽奖系统源码,微信互动游戏营销活动抽奖系统 v1.1
- 中国物联网、物联网卡进入蓬勃发展阶段
- 区块链安全保卫战:三分技术,七分管理
- Cocos2dx游戏教程(十二):“见缝插针”,游戏结算界面和音效
- FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecate;的解决办法
- 4核处理器_苹果电脑便宜卖!4核i5处理器,480G固态硬盘,带刻录,13.4寸,双系统...
- (新SOTA)UNETR++:轻量级的、高效、准确的共享权重的3D医学图像分割
- Unity游戏开发工具的下载与安装