亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

vuex如何在組件中修改狀態

vuex如何在組件中修改狀態

鴻蒙傳說 2018-09-04 17:44:41
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是那個創建日期,我想要在組件中格式化這個日期
查看完整描述

2 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

getcreatTime({commit},param){
commit('createAt',param)
}
在組件中調用的時候
computed: {

  ...mapGetters({ 
     createAt: 'createAt'
  })
},

mounted(){
this.$store.dipatch('getcreatTime', moment(this.createAt))

}


查看完整回答
反對 回復 2018-09-09
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

你的意思是在vuex在module還是用原來的日期格式,在具體的組件頁面中顯示時才轉化成moment的格式,是這個意思嗎?
如果是這個意思,你可以在組件頁面中添加一個轉換時間格式的方法,在調用state. createAt時調用

<template>

    <div>{{changeTimeFormat()}}</div>

</template>

<script>

  import { mapGetters, mapActions } from 'vuex'

  var moment = require('moment')

  export default {

    computed: {

      ...mapGetters({

        createAt: 'createAt'

      })

    },

    methods: {

      changeTimeFormat () {

        var newTime = moment(this.createAt)

        return newTime

      }

    }

  }

如果你每個地方都要轉換格式的話,還不如直接在vuex在module中直接把時間格式轉換過來,方便省事

查看完整回答
反對 回復 2018-09-09
  • 2 回答
  • 0 關注
  • 4603 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號