I am new to front-end development but have a basic understanding of HTML5, CSS and Javascript.
I created a vue.js project and added bootstrap and axios. My application loads data and displays them. However, once I click the "Show", "Update" or "Delete" button, I get "Variable not found: updateDocument" although the method exists.
Please tell me where to find the error.
Node version v18.15.0 npm version 9.0.5 vue.js 3 boostrap v5.3
To make sure the problem is not caused by data-doc.id-, I replaced it with a fixed number.
Code snippet from DocumentVault.vue:
<div v-else> <ul v-for="doc in documents" :key="doc.id" > <div class="card" > <div class="card-body"> <h4 class="card-header">{{ doc.title}} </h4> <p class="card-text"> Short Description: {{ doc.shortDescription}}</p> <p class="card-text">Categorie: {{ doc.categorie }}</p> <p class="card-text">Comments: {{ doc.comments }}</p> <a class="card-text" v-for="tag in doc.tags" :key="tag.id"> <div class="badge bg-info" >{{ tag.name }}</div> </a> <br><br> <div class="card-footer"> <button onclick=updateDocument(1) type="button" class="btn btn-outline-primary">Show</button> <button onclick=updateDocument(doc.id) type="button" class="btn btn-outline-primary">Update</button> <button onclick=deleteDocument(doc.id) type="button" class="btn btn-outline-primary">Delete</button> </div> </div> </div> </ul> <form @submit.prevent="submitForm"> <label> Title: <input v-model="title" type="text" required /> </label> <label> Mediatype: <input v-model="mediatype" type="text" required /> </label> <label> Short Description: <input v-model="shortDescription" type="text" required /> </label> <label> Categorie: <input v-model="categorie" type="text" required /> </label> <label> Comments: <textarea v-model="comments"></textarea> </label> <label> Tags: <input v-model="tags" type="text" /> </label> <button type="submit">Create</button> </form> </div>
mounted() { axios.get("/api/documents").then((response) => { this.documents = response.data; console.info(this.documents) }).catch((error) => { this.errorMessage = "Failed to retrieve documents."; console.error(error); }); },
I tried 2 methods for this method, but the error was the same
Variation A)
updateDocument: function (id) { axios.post(`/api/documents/${id}`).then(() => { this.documents = this.documents.filter((doc) => doc.id === id); }).catch((error) => { this.errorMessage = "Failed to update document."; console.error(error); }); },
Variation B)
updateDocument(id) { axios.post(`/api/documents/${id}`).then(() => { this.documents = this.documents.filter((doc) => doc.id === id); }).catch((error) => { this.errorMessage = "Failed to update document."; console.error(error); }); },
main.js
import 'bootstrap/dist/css/bootstrap.css' import { createApp } from 'vue' import App from './App.vue' createApp(App).mount('#app') import 'bootstrap/dist/js/bootstrap.js'
Application View
import DocumentVault from './components/DocumentVault.vue' export default { name: 'App', components: { DocumentVault } }
P粉8322127762023-09-10 10:05:22
I'm assuming you are using the options API, if so, try the following:
methods: { updateDocument() { // your code } },
and change your click event to:
<button @click="updateDocument">Show</button>
Here is the link to the vue documentation: Declaration method