我有来自API laravel的数据,这是我的状态代码.js

import axios from 'axios'
import {apiPostGet} from '../api/api'
export default {
  data: axios({
    method: 'GET',
    url: apiPostGet('Kategori')
  }).then(
    response => {
      return response.data.kategori
    }
  ).catch(
    error => {
      return error.response
    }
  )
}

这是我在gteeters中的代码.js

export default {
  datas: state => {
    return state.data
  }
}

这是我在索引中的代码.js

import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import getters from './getters'

Vue.use(Vuex)

export default new Vuex.Store({
   state,
   getters
})

and this picture from dev tool vue js

推荐答案

Data钩子需要同步返回.您必须将负载添加到createdmounted,只需将属性添加到数据/状态,这样react 性就起作用了.

Axios的数据加载需要通过一个操作触发,因为它是异步的.Mutations 需要同步运行.我已经在created中添加了初始加载.(mounted也可以.)

我已经使用Vuex helper mapState将状态属性映射到组件.使用getter也可以,但mapState更容易编写.

请看下面的演示或这fiddle.

还可以在fiddle中取消Vuex版本下面的代码注释,并对上面的应用程序进行注释,以了解Axios如何使用out Vuex,从而更好地理解.

const URL = 'https://jsonplaceholder.typicode.com/posts';

const store = new Vuex.Store({
  state: {
    posts: [],
    loading: true
  },
  actions: {
    loadData({
      commit
    }) {
      axios.get(URL).then((response) => {
        // console.log(response.data, this)
        commit('updatePosts', response.data)
        commit('changeLoadingState', false)
      })
    }
  },
  mutations: {
    updatePosts(state, posts) {
      state.posts = posts
    },
    changeLoadingState(state, loading) {
      state.loading = loading
    }
  }
})

new Vue({
  el: '#app',
  computed: Vuex.mapState(['posts', 'loading']),
  store,
  created() {
    //console.log(this.$store)
    this.$store.dispatch('loadData') // dispatch loading
  }
})

/*
	//example with-out vuex
  
new Vue({
	el: '#app',
	data() {
  	return {
    	loading: true,
      posts: [] // add posts here so reactivity is working, also undefined would be OK
    }
  },
  created() {
  	//this.loading = true --> not needed already set in data
  	axios.get(URL).then((response) => {
    	// console.log(response.data, this)
      this.posts = response.data
      this.loading = false
    })
  }
})

*/
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuex/3.0.1/vuex.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.1/axios.js"></script>
<div id="app">
  <div v-if="loading">
    loading...
  </div>
  <div v-else>
    <ul>
      <li v-for="post in posts">
        <h1>
          {{post.title}}
        </h1>
        <p>
          {{post.body}}
        </p>
      </li>
    </ul>
  </div>
</div>

Vue.js相关问答推荐

Nuxt 3翻转本视频

将事件监听器绑定到动态元素

复选框列未在vutify中排序

如何使用composition api v-model Select 框获取其他JSON 值?

VueJS3 - 在 for 循环中获取元素的句柄

如何在 vue js 的一页路由下显示不同的 category._id

如何渲染数组以 Select 选项 vue.js

为什么当我 Select 第二个输入框时音译transliteration不起作用?

如何使用 vue-resource 添加额外的标头来发布请求?

SVG 加载 vue-svg-loader; [Vue 警告]:无效的组件定义

JavaScript/VueJS:判断数组是否包含具有特定值元素的对象

VueJS 使用 prop 作为数据属性值

在 vue/vuex(/flux?) 中使用 ES6 类是一种反模式吗?

Vue 组件 Vue-Instant

了解 Nuxt.js 中的State状态和Getters:Getters不起作用

Vuejs css 范围性能

更改事件上的 Select2 在 Vuejs 中不起作用

如何从扩展面板(Vuetify)中删除阴影?

如何在 VeeValidate 中自定义错误信息(字段名称)?

如何修复套接字 io 中的 400 错误错误请求?