使用vuex中的mutations改变state里面的全局状态的值,获取到的数据却怎么都是undefined
一开始使用的是这种赋值方式
this.$store.commit('currentSongInfo', res.data.songs[0].name, res.data.songs[0].ar[0].name, res.data.songs[0].al.name, res.data.songs[0].al.picUrl)
mutations: {
currentSongId (state, id) {
state.songId = id
},
currentSongInfo (state, songName, artists, albumName, picUrl) {
state.songInfo.artists = artists
state.songInfo.songName = songName
state.songInfo.albumName = albumName
state.songInfo.picUrl = picUrl
}
},
数据始终为空
后来发现mutations只能传单个参数,所以把数据放在一个对象传进来
this.$store.commit('currentSongInfo', {
songName: res.data.songs[0].name, artists: res.data.songs[0].ar[0].name, albumName: res.data.songs[0].al.name, picUrl: res.data.songs[0].al.picUrl})
mutations: {
currentSongId (state, id) {
state.songId = id
},
currentSongInfo (state, info) {
state.songInfo.artists = info.artists
state.songInfo.songName = info.songName
state.songInfo.albumName = info.albumName
state.songInfo.picUrl = info.picUrl
}
},
就可以获取到数据啦