日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

vue的mounted和created方法的执行

發布時間:2023/12/14 vue 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue的mounted和created方法的执行 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Vue的created 和 mouted的執行順序

今天在數據可視化的項目中,用了echarts和vue來進行展示。按照我的理解,先執行created中調用后臺數據的接口,然后再進行echarts的展現。很遺憾一直失敗,而且每次打印都有數據,但是始終無法給echarts綁定。

<template><div class="map"><dv-border-box-8 class="map-chart-border"><div ref="mapChart" class="map-chart"></div></dv-border-box-8></div> </template> <script>//引入geo數據//import mapApi from '@/api/chinaMap'import mapData from "@/lib/china";export default {data() {return {mapData: [],currentData: [],statisticData: []}},created() {this.getMapData();},methods: {initMapChart() {console.log(this.currentData)const el = this.$refs.mapChart;const myChart = this.$echarts.init(el);this.$echarts.registerMap("chinaMap", mapData);const option = {title: {text: '地圖可視化',left: '45%',textStyle: {fontSize: 20,color: "#fff"},},tooltip: {trigger: 'item'},visualMap: {min: 0,max: 100,text: ['High', 'Low'],realtime: false,calculable: true,inRange: {color: ['lightskyblue', 'yellow', 'orangered']},textStyle: {color: "#fff",fontSize: 18},left: '5%'},series: [{name: '全國疫情熱力圖',type: 'map',map: 'chinaMap',label: {show: false},zoom: 1.4,data: this.currentData,layoutCenter: ['50%', '70%'],layoutSize: 400}]}myChart.setOption(option);window.addEventListener("resize", function () {myChart.resize();});},getMapData() {mapApi.getMapData().then(response => {//刷新頁面this.mapData = response.data.data.mapDatafor (let i = 0; i < this.mapData.length; i++) {this.currentData.push({name: this.mapData[i].name,value: this.mapData[i].currentConfirmedCount})this.statisticData.push({name: this.mapData[i].name,value: this.mapData[i].confirmedCount})}})},},mounted() {this.initMapChart();},}; </script><style lang="less" scoped>.map {width: 60%;height: 100%; //要給指定高度,這里我在組件外加了固定高度,所以這里給了100%.map-chart-border {width: 100%;height: 100%;}.map-chart {width: 100%;height: 100%;padding: 10px;}} </style>

經過半天的探索發現其實created和mounted沒有嚴格的執行順序,因為異步執行兩段代碼。
最后附上正確代碼

<template><div class="map"><dv-border-box-8 class="map-chart-border"><div ref="mapChart" class="map-chart"></div></dv-border-box-8></div> </template> <script>//引入geo數據//import mapApi from '@/api/chinaMap'import mapData from "@/lib/china";export default {data() {return {mapData: [],currentData: [],statisticData: []}},created() {this.getMapData();},methods: {initMapChart() {console.log(this.currentData)const el = this.$refs.mapChart;const myChart = this.$echarts.init(el);this.$echarts.registerMap("chinaMap", mapData);const option = {title: {text: '地圖可視化',left: '45%',textStyle: {fontSize: 20,color: "#fff"},},tooltip: {trigger: 'item'},visualMap: {min: 0,max: 100,text: ['High', 'Low'],realtime: false,calculable: true,inRange: {color: ['lightskyblue', 'yellow', 'orangered']},textStyle: {color: "#fff",fontSize: 18},left: '5%'},series: [{name: '全國疫情熱力圖',type: 'map',map: 'chinaMap',label: {show: false},zoom: 1.4,data: this.currentData,layoutCenter: ['50%', '70%'],layoutSize: 400}]}myChart.setOption(option);window.addEventListener("resize", function () {myChart.resize();});},getMapData() {mapApi.getMapData().then(response => {//刷新頁面this.mapData = response.data.data.mapDatafor (let i = 0; i < this.mapData.length; i++) {this.currentData.push({name: this.mapData[i].name,value: this.mapData[i].currentConfirmedCount})this.statisticData.push({name: this.mapData[i].name,value: this.mapData[i].confirmedCount})}this.initMapChart();})},},mounted() {},}; </script><style lang="less" scoped>.map {width: 60%;height: 100%; //要給指定高度,這里我在組件外加了固定高度,所以這里給了100%.map-chart-border {width: 100%;height: 100%;}.map-chart {width: 100%;height: 100%;padding: 10px;}} </style>

效果如下:

總結

以上是生活随笔為你收集整理的vue的mounted和created方法的执行的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。