I am trying to set value from v-for directive into localStorage. Right now it says
Storage ? length: 1 ? songs: "undefined"
This is what I want to store when pressing the button:
<article
class="all-tracks"
v-for="tracks in artist"
:key="tracks.id"
>
<h1 class="track-title">
{{tracks.track.track_name}}
</h1>
<div class="track_artist">
{{tracks.track.artist_name}}
</div>
<div class="track-album">
{{tracks.track.album_name}}
</div>
<button class="btn btn-secondary btn-sg" @click="addToPlaylist()">
Add to playlist
</button>
</article>
P.S I would appreciate any comment on my code as I am here to learn and trying to write the cleanest way, yet I am a beginner so please do mind my mistakes below
import axios from 'axios';
export default {
name: 'SearchArea',
props: {
},
data() {
return {
artist: [],
songs: [],
};
},
methods: {
fetchMusic(e) {
if (e.key === 'Enter') {
axios.get(``, {
headers: {
'Access-Control-Allow-Origin': '*',
},
})
.then((response) => response).then(this.setResults);
}
},
setResults(results) {
this.artist = results.data.message.body.track_list;
this.songs = results.data.message.body.track_list.track;
},
addToPlaylist() {
localStorage.songs = JSON.stringify(this.songs);
}
},
watch: {
artist: {
handler() {
localStorage.setItem('songs', JSON.stringify(this.songs));
},
deep: true,
},
},
}
question from:
https://stackoverflow.com/questions/66045589/cleanest-way-to-work-with-localstorage-in-vue 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…