i am new in vuejs. i have created comment feature similar with here, but due to certain circumstances i have to improvise it. i use vue component but it couldn’t access method inside my vue. if user has already post comment, any user may be able to reply that particular comment. however, i received vue warn “Property or method ‘replyComment’ is not defined on the instance but referenced during render”. can anyone help me please?
JavaScript
x
135
135
1
Vue.component('reply-komen',{
2
template:'#replykomen',
3
props:{
4
edit:{
5
type:Boolean,
6
default:false
7
},
8
comment:{
9
type:Object,
10
default(){
11
return {
12
title: '',
13
body: '',
14
id: ''
15
}
16
}
17
}
18
},
19
methods:{
20
replyComment: function(comment_id){
21
console.log(comment.id);
22
var id={{$complaint->id}};
23
var users={{IlluminateSupportFacadesAuth::user()->id}};
24
const item=this.$refs.balaskomen;
25
const idkomen=item.dataset.id;
26
console.log(idkomen);
27
$.ajax({
28
url:'/api/complaint/comment',
29
type:"POST",
30
data:{
31
'users':users,
32
'id':id,
33
'comment':this.comment
34
},
35
success:function (response) {
36
komen2.comment.body= '';
37
komen2.fetchComments();
38
}
39
})
40
}
41
}
42
});
43
44
var komen2=new Vue({
45
el: '#kalas',
46
data:{
47
currentView:'',
48
edit:false,
49
comments:[],
50
comment: {
51
title:'',
52
body: '',
53
id: '',
54
}
55
},
56
57
created: function(){
58
this.fetchComments();
59
this.createComment();
60
this.editComment();
61
this.deleteComment();
62
this.showComment();
63
},
64
65
methods: {
66
fetchComments: function(){
67
var id={{$complaint->id}};
68
$.ajax({
69
url:'/api/complaint/getcomments',
70
type:"GET",
71
data:{
72
'id':id
73
},
74
success:function (response) {
75
komen2.comments = response;
76
}
77
})
78
},
79
80
createComment: function(){
81
var id={{$complaint->id}};
82
var users={{IlluminateSupportFacadesAuth::user()->id}};
83
$.ajax({
84
url:'/api/complaint/comment',
85
type:"POST",
86
data:{
87
'users':users,
88
'id':id,
89
'comment':this.comment
90
},
91
success:function (response) {
92
komen2.comment.body= '';
93
komen2.fetchComments();
94
}
95
})
96
},
97
98
editComment: function(comment_id){
99
$.ajax({
100
url:'/api/complaint/'+window.Laravel.post_id+'/comment'+comment_id,
101
type:"PATCH",
102
data:{
103
'comment':this.comment
104
},
105
success:function (response) {
106
komen2.comment.body= '';
107
komen2.comment.id= '';
108
komen2.fetchComments();
109
komen2.edit = false;
110
}
111
})
112
},
113
114
deleteComment: function(comment_id){
115
$.ajax({
116
url:'/api/complaint/'+window.Laravel.post_id+'/comment'+comment_id,
117
type:"DELETE",
118
success:function (response) {
119
komen2.comment.body= '';
120
komen2.fetchComments();
121
}
122
})
123
},
124
125
showComment: function(comment_id){
126
for (var i = 0; i < this.comments.length; i++) {
127
if (this.comments[i].id == comment_id) {
128
this.comment.body = this.comments[i].body;
129
this.comment.id = this.comments[i].id;
130
this.edit = true;
131
}
132
}
133
}
134
}
135
});
JavaScript
1
44
44
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
2
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
3
<div id="kalas">
4
<div class="container">
5
<h4>Add Comment</h4>
6
<form action="" @submit.prevent="edit ? editComment(comment.id) : createComment()">
7
<div class="input-group">
8
<textarea name="body" v-model="comment.body" ref="textarea" class="form-control"></textarea>
9
</div>
10
<div class="input-group">
11
<button type="submit" class="btn btn-primary" v-show="!edit">Add Comment</button>
12
<button type="submit" class="btn btn-primary" v-show="edit">Edit Comment</button>
13
</div>
14
</form>
15
<br>
16
<h4>Comments</h4>
17
<ul class="list-group" v-for="comment in comments">
18
{{--<li class="list-group-item" v-for="comment in comments">--}}
19
<form>
20
<div class="input-group">
21
<textarea class="form-control">@{{comment.body}}</textarea>
22
</div>
23
<div class="input-group" ref="balaskomen" v-bind:id="comment.id">
24
<a class="btn btn-default" v-on:click="currentView='reply-komen'">Reply</a>
25
<a class="btn btn-danger" v-on:click=" deleteComment(comment.id)">Delete</a>
26
</div>
27
</form>
28
{{--</li>--}}
29
</ul>
30
<div class="container balas" >
31
<component :is="currentView"></component>
32
</div>
33
</div>
34
</div>
35
<template id="replykomen" >
36
<form action="" @submit.prevent="replyComment(comment.id)">
37
<div class="input-group">
38
<textarea name="body" v-model="comment.body" ref="textarea" class="form-control"></textarea>
39
</div>
40
<div class="input-group">
41
<button type="submit" class="btn btn-primary" v-show="!edit">Add Comment</button>
42
</div>
43
</form>
44
</template>
Advertisement
Answer
replyComment
is defined in the second Vue instance (#kalas
), but referenced in the first instance’s template (#replykomen
). Move/copy the method to the first Vue instance’s methods to resolve the error.