如何从vue中创建的方法访问dispatch事件?

时间:2018-04-15 06:18:29

标签: vue.js

我试图访问组件中的数据,我可以使用模板访问它,但我实际上希望能够从创建的方法访问调度用户详细信息。 我怎样才能访问它?

<template>
<div>
<input type='text' class='e_pearl ep_pearl form-control' placeholder='Perle..' autoComplete='false' autoFocus spellCheck='false' :value='user.pearl_color' >
  </div>
</template>

<script>
var colors = '#006064';
export default {
  mixins: [userMixin],
  components: {
    Sidebar,
    'swatches-picker': Swatches
  },
  methods: {
  created(){
    let {
        session: { email },
        $store: { dispatch }
      } = this
    dispatch('userDetails', email)
    console.log(this.$data.colors); // access to data from created
  },
  data () {
    return {
      colors
    }
  },
  mounted(){
    $('.ep_password').focus()
  }
}
</script>

我试过了:

console.log(this.$store.state.user);

{ ob :观察者} 会议 : (......) 为userDetails : (...)

模块

import actions from '../actions/user-a'
import $ from 'jquery'

let d = $('.data')

export default {
  state: {
    session: {
      id: d.data('session'),
      email: d.data('email')
    },
    userDetails: {}
  },
  mutations: {
    USER_DETAILS: (state, payload) => state.userDetails = payload,
  },
  actions,
}

动作

import { post } from 'axios'

export default {

  userDetails: async (context, payload) => {
    console.log(payload);
    let { data } = await post('/api/get-details', { email: payload })
    context.commit('USER_DETAILS', data)
  },

}

如果我尝试console.log(this.$store.state.userDetails);

数据不可用,但我可以从中看到它们。$ store.state.user

但没有运气!

0 个答案:

没有答案