I'm creating a social network for project in my formation, i have a like system and it work.
i have a components cardArticle with all info and i try to do a like count. It work but when i refresh the page or going on other page, i lost all my data likes (my data is not saved)
components/CardArticle.vue
<template>
<div id="card">
<div>
<a >
<img v-if="post.imageUrl !== undefined" :src="post.imageUrl" />
<h2 > {{ post.title }}</h2>
<p > {{ post.description }}</p>
</a>
</div>
<div >
<div>
<button type="button" id="buttonDelete" @click="deletePost"
v-if="this.post.userId === this.user.userId || this.user.isAdmin === true">Supprimer</button>
<button type="button" id="buttonEdit" @click="modifyPost"
v-if="this.post.userId === this.user.userId || this.user.isAdmin === true">
Editer
</button>
</div>
<div >
<div >
<a @click="sendLike">
<i ></i>
</a>
</div>
<div >
<p> {{ likes }} </p>
</div>
</div>
</div>
</div>
</template>
<script>
import axios from "axios";
import router from "../router/index.js";
export default {
name: 'CardArticle',
data () {
return {
likes: 0
}
},
props: {
post: {
type: Object
}
},
computed: {
user() {
return this.$store.getters.user;
}
},
methods: {
sendLike() {
axios.post("http://localhost:3000/api/articles/" this.post._id "/like", {
userId: this.user.userId
}, {
headers: {
Authorization: "Bearer " this.user.token
}
})
.then(response => this.likes = response.data.article.likes)
.catch(error => console.log(error))
}
}
}
</script>
views/home.vue
<template>
<div v-if="this.user.token !== null">
<CardArticle v-for="post in allPosts" v-bind:key="post.id" :post="post" />
</div>
</template>
<script>
import CardArticle from "../components/CardArticle.vue"
import axios from "axios";
export default {
name: 'HomeArticle',
data () {
return {
post: {
title: "",
description: "",
imageUrl: ""
},
allPosts: [],
}
},
computed: {
user() {
return this.$store.getters.user;
}
},
components: {
CardArticle,
},
mounted() {
axios.get("http://localhost:3000/api/articles", {
headers: {
Authorization: "Bearer " this.user.token
}
})
.then(response => {
this.allPosts = response.data;
})
.catch(error => {
return error;
})
}
}
</script>
What i should do for not losing my data ? I would not use vuex or localstorage for that if possible, you have idea ?
Thanks for your help
CodePudding user response:
If you loading data from server, then refresh page, you always will be lose data, because browser loading page again from server, and application will load data again.
If you don't want use vuex
(but why not?), you can write data to cookies
(by setting cookie value), then load it on app startup (when page is loaded). But it's not best practice at all. You can use vue3-cookies
lib (link).
By the way, better learn to use stores, most progressive, I think, is Pinia
.
Check: https://pinia.vuejs.org/
CodePudding user response:
i lost all my data likes (my data is not saved)
likes
is belong to each articles and It should have been saved to your db and call API to retrieve it again on component mounting:
export default {
name: 'CardArticle',
data () {
return {
likes: 0 // It's not managed by component state
}
},
methods: {
sendLike() {
axios.post("http://localhost:3000/api/articles/" this.post._id "/like", {
userId: this.user.userId
}, {
headers: {
Authorization: "Bearer " this.user.token
}
})
.then(
// invalidates, update allPosts props (emit to parent)
)
.catch(error => console.log(error))
}
}
}