I have a function to create a post, but I need to refresh the page to see the new post.
I dont want to force the reload because I think it is not a good practice.
Do you have good practice for me ?
import axios from "axios";
export default {
name: "PostCreate",
data() {
return {
title: "",
content: "",
userId: "",
selectdFile: null,
};
},
methods: {
onFileSelected(event) {
this.selectedFile = event.target.files[0];
console.log(this.selectedFile);
},
async createPost() {
const id = localStorage.getItem("userId");
const formData = new FormData();
formData.append("image", this.selectedFile, this.selectedFile.filename);
formData.append("title", this.title);
formData.append("content", this.content);
formData.append("userId", id);
console.log("thisid", id);
console.log(formData);
const response = await axios.post(
"http://localhost:3000/api/post",
formData,
{
headers: {
//'Content-Type': 'multipart/form-data',
Authorization: "Bearer " + localStorage.getItem("token"),
},
}
);
console.log("ok");
console.log(response);
console.log(formData);
},
},
};