vue中vuex,echarts,地圖,ueditor的使用(一篇就夠)

前言

vue-cli生成的template還須要配置axios,vuex,element等插件,該項目中將這些經常使用插件進行了配置; 項目開發中template能夠快速複用,也是能夠快速上手vue的一個demo;javascript

1.動態效果圖

2.技術棧

  1. 技術棧:vue+vue-router+webpack+axios+echarts+ueditor+element UI+map+node-sass;
  2. 功能模塊:數據可視化,地圖,普通表格的增刪,可編輯表格,合併表格,左側菜單可展收;
  3. 適配:使用百分比佈局,適配pc全部機型;
  4. 目的:項目開發能夠快速複用的項目模板;

3.詳細技術點

  1. props+$emit:父子組件傳值;
  2. axios: axios.interceptors.request(response)實現axios的全局攔截 axios.get(post)請求接口
  3. vuex:實現公共數據模塊化管理和非父子組件通信 4 .vuex-persistedstate:實現vuex數據的緩存 5 .echarts:折線圖,柱狀圖,扇形圖和儀表等數據可視化
  4. 高德地圖:地圖展現
  5. ueditor:富文本編輯器 8 .utiles:裏面封裝了經常使用工具類
  6. element UI+slot:可編輯表格
  7. table:原生table實現表格拆分,展現複雜數據

github源碼地址

戳這裏 這個template後期會持續更新完善,歡迎star,謝謝噠html

4.項目目錄

5.核心代碼分析

5.1store模塊代碼

1.入口index.jsvue

import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'//能夠將vuex數據緩存到sessionStorage中
import comTable from './modules/comTable'
 Vue.use(Vuex)
    
 export default new Vuex.Store({
   modules: {
     comTable//將vuex拆分紅模塊
   },
   plugins: [createPersistedState({ storage: window.sessionStorage })]
  })
複製代碼

2.modules下面comTable.js文件:java

import * as comTableApi from '@/api/comTable'//將請求接口文件拆分

// initial state
const state = {
  tableData: [],
}

// getters
const getters = {
  allTableData: state => state.tableData,
}

// actions,異步提交,將ajax相關代碼寫入這個屬性,然後commit改變mutation
const actions = {
  getAllData ({ commit }) {
    comTableApi.getComAjax('static/comTable.json',{obj1:'',obj2:''},(tableData) => {
      commit('setTableData', tableData)
     })
  }
}

// mutations,同步提交,能夠改變state的值
const mutations = {
  setTableData (state,tableData) {
    state.tableData = tableData
  }
}
複製代碼

3.在.vue中的使用 兩種方法: this.$store.comTable.state(distapch)能夠設置 藉助mapGetters,mapActions輔助函數:node

import { mapGetters, mapActions } from "vuex";
computed: mapGetters({
  tableData: "allTableData",
}),
mounted() {
  this.getAllData();
},
methods:{
  ...mapActions([
   'getAllData'//須要調用
]),}
複製代碼

5.2 echarts模塊

echarts官網提供了setOption的參數,只須要獲取頁面的dom,而後設置setOption屬性webpack

let histogram = this.$echarts.init(document.getElementById("histogram"));//tempalte設置一個標籤
 // 繪製圖表
 histogram.setOption({//對象參數爲obj
  title: { text: "ECharts 入門示例" },
  tooltip: {},
  xAxis: {
    data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]//橫向座標值
  },
  yAxis: {},
  series: [
  {
   name: "銷量",
   type: "bar",
   data: [5, 20, 36, 10, 10, 20]
   }
   ]
   });
複製代碼

5.3 ueditor模塊

將下載好的static放到static目錄下,在main.js引入,在對應的vue文件中ios

this.editor = UE.getEditor('editor', this.config); // 初始化UE
this.editor.addListener("ready", function () {
  _this.editor.setContent(_this.defaultMsg); // 確保UE加載完成後,放入內容。
});
this.editor.getContent()//獲取富文本內容
複製代碼

5.4 地圖

我是使用高德地圖,在index.html全局導入git

<script src="http://webapi.amap.com/maps?v=1.4.6&key=9f63e48dcb3ccddc5cf6601788186f13&plugin=AMap.MouseTool"></script>//key爲我申請的,也能夠本身去申請
複製代碼

高德地圖官網案例github

相關文章
相關標籤/搜索