vuex如何在组件中修改状态

vuex在module中定义了state和mutaition,
请问一下大家,我如何在组件中修改这个state.
因为这个state获取的是返回的日期,在组件中我希望用moment插件,this.$moment("这个日期")来进行格式化,在显示在页面中,看了很就文档,当时还是没明白改怎么修改呀?

下面是module是的代码:

import axios from 'axios'

axios.defaults.baseURL = 'xxxxxxx';


const state = {

  title:"",

  authorName:"",

  pubNews:"",

  channelNews:"",

  createAt:"",

  content:""

};

const mutations = {

  GETDETAILS(state,res){

    state.title=res.data.news.title;

    state.authorName=res.data.news.authorName;

    state.pubNews=res.data.news.pubNews;

    state.channelNews=res.data.news.channelNews;

    state.createAt=res.data.news.createAt;

    state.content=res.data.news.content;

  }

};


const actions = {

  getDetails({commit},id){

    axios.get("/news/detail",{withCredentials:true,params:{id:id}}).then(res=>{

      commit("GETDETAILS",res)

    })

  }

};

export default{

  state,

  mutations,

  actions

}

createAt是那个创建日期,我想要在组件中格式化这个日期

鸿蒙传说
浏览 4563回答 2
2回答

POPMUISE

getcreatTime({commit},param){commit('createAt',param)}在组件中调用的时候computed: {  ...mapGetters({       createAt: 'createAt'   }) },mounted(){this.$store.dipatch('getcreatTime', moment(this.createAt))}

慕仙森

你的意思是在vuex在module还是用原来的日期格式,在具体的组件页面中显示时才转化成moment的格式,是这个意思吗?如果是这个意思,你可以在组件页面中添加一个转换时间格式的方法,在调用state. createAt时调用<template>&nbsp; &nbsp; <div>{{changeTimeFormat()}}</div></template><script>&nbsp; import { mapGetters, mapActions } from 'vuex'&nbsp; var moment = require('moment')&nbsp; export default {&nbsp; &nbsp; computed: {&nbsp; &nbsp; &nbsp; ...mapGetters({&nbsp; &nbsp; &nbsp; &nbsp; createAt: 'createAt'&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; },&nbsp; &nbsp; methods: {&nbsp; &nbsp; &nbsp; changeTimeFormat () {&nbsp; &nbsp; &nbsp; &nbsp; var newTime = moment(this.createAt)&nbsp; &nbsp; &nbsp; &nbsp; return newTime&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }如果你每个地方都要转换格式的话,还不如直接在vuex在module中直接把时间格式转换过来,方便省事
打开App,查看更多内容
随时随地看视频慕课网APP