我正在编写此代码,其中Vue.JS模块调用一个PHP文件,该文件从外部服务器获取数据并返回JSON响应。我编写了一个循环,用于从JSON获取特定字段并以对话格式打印它们。但是,它仅显示没有文字的空消息气泡(消息气泡的数量是正确的,但其中没有文本)。
这是我的代码:
const app = new Vue({
el: "#chatview",
data: {
data:[],
txtInput: '',
mid:0
},
methods:
{
GetBubbleType: function (name){
if(name === "Support")
return "yours messages";
else
return "mine messages";
},
},
mounted(){
axios.post(
'./ConversationGetter.php',
{
function2call: 'getRecord',
id: 1,
}
)
.then(response => {this.data=response.data.data;
console.log(response.data);
})
.catch(error => {});
},
template: `
<div style ="font-family:Open Sans;font-size:16px">
<div v-for="message in data">
<div class="fade-in">
<div v-bind:class="GetBubbleType(data.name)">
<div class="message last">
<p>{{data.message}}</p>
</div>
</div></div></div> `
})
PHP返回的响应:
{
"data": [
{
"Modified_Time": "2019-12-13T16:08:36+05:30",
"$currency_symbol": "$",
"Message": "Hey!",
"Created_Time": "2019-12-13T16:08:36+05:30",
"Name": "Me",
},
{
"Modified_Time": "2019-12-13T16:08:27+05:30",
"$currency_symbol": "$",
"Message": "Yo!",
"Created_Time": "2019-12-13T16:08:27+05:30",
"Name": "Me",
},
],
}
答案 0 :(得分:3)
您应该调用该消息,因为您正在遍历数据属性,因此它应该在段落标记内{{message.Message}}
而不是{{data.message}}
当您遍历数据属性时,每个循环对象都包含消息元数据
{
"Modified_Time": "2019-12-13T16:08:27+05:30",
"$currency_symbol": "$",
"Message": "Yo!",
"Created_Time": "2019-12-13T16:08:27+05:30",
"Name": "Me",
},
您应该能够在v-for循环中像这样object property
那样访问每个{{message.Message}}