Created
July 25, 2020 16:55
-
-
Save joeczar/12517c09cefdbe8a157bacda52b70010 to your computer and use it in GitHub Desktop.
Vue component v-for error: [Vue warn]: Property or method "each" is not defined on the instance but referenced during render.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Vue.component('comments', { | |
| props: ['img'], | |
| data: function () { | |
| return { | |
| comment: '', | |
| username: '', | |
| comments: [], | |
| }; | |
| }, | |
| mounted: function () { | |
| const self = this; | |
| console.log('Sending:', { imageid: this.img }); | |
| axios | |
| .post('/comments', { imageid: self.img }) | |
| .then(({ data }) => { | |
| this.comments = data; | |
| }) | |
| .catch((err) => { | |
| console.log('error in get comments', err); | |
| }); | |
| }, | |
| methods: { | |
| submitComment: function (e) { | |
| e.preventDefault(); | |
| console.log('comment this.img', this.img); | |
| axios | |
| .post('/comment', { | |
| comment: this.comment, | |
| usrName: this.username, | |
| imgId: this.img, | |
| }) | |
| .then(({ data }) => { | |
| console.log(data); | |
| }); | |
| }, | |
| }, | |
| template: ` | |
| <div class="commentWrapper"> | |
| <h1 id="commentTitle">Comments</h1> | |
| <form id="commentForm" class="commentForm shadow-3"> | |
| <label for="comment"> | |
| <textarea v-model="comment" name="comment" id="comment" placeholder="Type your comment here." class="shadow-2"></textarea> | |
| </label> | |
| <label for="username"> | |
| <input v-model="username" id="username" placeholder="Username" class="shadow-2"> | |
| </label> | |
| <button @click=submitComment class="shadow-3 btn" id="commentSubmit">Submit</button> | |
| </form> | |
| <hr> | |
| <div class="commentsArrayWrapper> | |
| <div v-for='each in comments'> | |
| <p class="commentBody">{{each.comment}}</p> | |
| <span class="userName">From: {{each.username}}</span> | |
| <span class="date">On: {{each.date}}</span> | |
| </div> | |
| </div> | |
| </div> | |
| `, | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment