//store.js
import {createStore} from "vuex"; import axios from "axios"; const store = createStore({ state() { return { merchants: [] } }, getters:{ }, mutations: { getMerchants(state, data) { state.merchants = data } }, actions: { getMerchants(context) { //向后端要数据 axios.get('http://localhost:3000/get-merchants') .then(response => { // console.log(response) context.commit('getMerchants', response.data) }) } } }) export default store
//serve/router.js const {Router} = require('express') const router = Router() const fs=require('fs') router.get('/get-merchants',(req,res)=>{ fs.readFile('data/merchants.json',(error,data)=>{ if (error) throw error res.send(JSON.parse(data)) }) }) module.exports = router
后端通过get方法指定 /get-merchants 对外发出数据 res.send(JSON.parse(data))
vuex使用axios方法get http://localhost:3000/get-merchants 路径上的数据
标签:merchants,axios,const,从后,get,vuex,data From: https://www.cnblogs.com/smx-BKY/p/16733215.html