person.js 858 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //人员管理相关的配置
  2. import axios from 'axios'
  3. import { nanoid } from 'nanoid'
  4. export default {
  5. namespaced:true, //开启命名空间
  6. actions:{
  7. addPersonWang(context,value){
  8. if(value.name.indexOf('王') === 0){
  9. context.commit('ADD_PERSON',value)
  10. }else{
  11. alert('添加的人必须姓王!')
  12. }
  13. },
  14. addPersonServer(context){
  15. axios.get('https://api.uixsj.cn/hitokoto/get?type=social').then(
  16. response => {
  17. context.commit('ADD_PERSON',{id:nanoid(),name:response.data})
  18. },
  19. error => {
  20. alert(error.message)
  21. }
  22. )
  23. }
  24. },
  25. mutations:{
  26. ADD_PERSON(state,value){
  27. console.log('mutations中的ADD_PERSON被调用了')
  28. state.personList.unshift(value)
  29. }
  30. },
  31. state:{
  32. personList:[
  33. {id:'001',name:'张三'}
  34. ]
  35. },
  36. getters:{
  37. firstPersonName(state){
  38. return state.personList[0].name
  39. }
  40. },
  41. }