【发布时间】:2019-09-02 00:48:33
【问题描述】:
我一直在搞乱 Vue.js 和 Django,我在按钮上添加了一个函数调用。对于每种不同类型的食物,该按钮会多次显示,但仅在单击第一个按钮时才会显示警报。可能是什么原因?
Django 模板:
{%if context%}
{%for fooditem in context%}
<div id = "vue-app2">
<button class = "btn-foodname" v-on:click="changeFoodName()">{{fooditem.food_name}}</button>
<p>{{fooditem.country_of_origin}}</p>
</div>
{%endfor%}
{%else%}
<p>Nothing to see here</p>
{%endif%}
JS:
var x = new Vue({
el: '#vue-app2',
data:{
new_message: 'Yes, this is a good food!'
},
methods:{
changeFoodName: function(){
alert(this.new_message);
}
}
});
【问题讨论】:
标签: javascript django vue.js