vue+vuex+axios從後台擷取資料存入vuex,組件之間共用資料

來源:互聯網
上載者:User

標籤:pos   api   erro   流程   this   建立   getter   mis   sga   

在vue項目中組件間相互傳值或者後台擷取的資料需要供多個組件使用的情況很多的話,有必要考慮引入vuex來管理這些淩亂的狀態,今天這邊博文用來記錄這一整個的過程,後台api介面是使用webpack-server類比的介面,這個前面的文章中有提到,需要的可以去翻閱。

整個的流程是在組件的created中提交dispatch,然後通過action調用一個封裝好的axios然後再觸發mutation來提交狀態改變state中的資料,然後在組件的計算屬性中擷取state的資料並渲染在頁面上

首先新需要在項目中安裝vuex:

運行命令 npm install vuex --save-dev

在項目的入口js檔案main.js中

import store from ‘./store/index‘

並將store掛載到vue上

new Vue({  el: ‘#app‘,  router,  store,  template: ‘<App/>‘,  render: (createElement) => createElement(App)})



這裡我建立了檔案夾fetch用來編寫所有的axios處理和axios封裝

在fetch檔案夾下建立api.js檔案:

import axios from ‘axios‘export function fetch(url, params) {    return new Promise((resolve, reject) => {        axios.post(url, params)            .then(response => {                 alert(‘Api--ok‘);                resolve(response.data);            })            .catch((error) => {              console.log(error)               reject(error)            })    })}export default {  // 擷取我的頁面的後台資料  mineBaseMsgApi() {     alert(‘進入api.js‘)    return fetch(‘/api/getBoardList‘);  }}

在store的入口檔案index.js中:

 

import Vue from ‘vue‘import Vuex from ‘vuex‘import mine from ‘./modules/mine‘;Vue.use(Vuex);export default new Vuex.Store({  modules: {    mine  }});

在你需要請求後台資料並想使用vuex的組件中的created分發第一個dispatch:

created() {    this.$store.dispatch(‘getMineBaseApi‘);  }

然後在store/modules下的對應模組js檔案中,這裡我使用的mine.js檔案中編寫state、action和mutation

import api from ‘./../../fetch/api‘;import * as types from ‘./../types.js‘;const state = {  getMineBaseMsg: {    errno: 1,    msg: {}  }}const actions = {  getMineBaseApi({commit}) {    alert(‘進入action‘);    api.mineBaseMsgApi()    .then(res => {        alert(‘action中調用封裝後的axios成功‘);        console.log(‘action中調用封裝後的axios成功‘)        commit(types.GET_BASE_API, res)    })  }}const getters = {  getMineBaseMsg: state => state.getMineBaseMsg}const mutations = {  [types.GET_BASE_API](state, res) {    alert(‘進入mutation‘);    state.getMineBaseMsg = { ...state.getMineBaseMsg, msg: res.data.msg }    alert(‘進入mutations修改state成功‘);  }}export default {  state,  actions,  getters,  mutations}

 然後在想取回state的組件中使用mapGetters擷取state:

import { mapGetters } from ‘vuex‘;export default {  ...  computed: {    ...mapGetters([      ‘getMineBaseMsg‘    ])  },  ...      }

然後在控制台查看把:

getter和mutation都已經成功了,同時我在提交state的整個過程都添加了alert,大家可以看看整個流程是如何走的;

來源: http://www.cnblogs.com/jasonwang2y60/

  

 

  

  

vue+vuex+axios從後台擷取資料存入vuex,組件之間共用資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.