下面代码没有报错,我觉得没问题,检查了好几遍,服务器传过来有数据,同样代码放visual studio都正常,但prompt始终为空,微信开发者工具真是太库辣😅😅😅
var prompt: { id: number, promptname: string, prompt: string, valuesname: string,values: string,username: string,account: string }[]=[];
onLoad() {
this.getprompt()
},
getprompt(){
var that = this
wx.request({
url: 'http://'+app_element.globalData.ip+':1337/getprompt',//访问路由
method:'POST',
data:{
account:app_element.globalData.account,
password:app_element.globalData.password
},
success: function (res) {
var temp = JSON.stringify(res.data);
var tempres = JSON.parse(temp);
console.log(tempres)
var tempprompt: { id: number, promptname: string, prompt: string, valuesname: string,values: string,username: string,account: string }[]=[]
for(var m = 0; m < tempres.length; m++){
tempprompt.push ({
id: tempres[m].id,
promptname: tempres[m].promptname,
prompt:tempres[m].prompt,
valuesname: tempres[m].valuesname,
values: tempres[m].values,
username: tempres[m].username,
account: tempres[m].account
});
}
that.setData({
prompt:tempprompt
})
}
})
},

“我在其他地方用prompt数组渲染时发现为空”,有这句话,判断是JS异步问题。去百度了解一下这个课题。
that.setData({
prompt:tempprompt
})
var that = thiswx.request({url: 'http://'+app_element.globalData.ip+':1337/getprompt',//访问路由method:'POST',data:{account:app_element.globalData.account,password:app_element.globalData.password},success: function (res) {var temp = JSON.stringify(res.data);var tempres = JSON.parse(temp);console.log(tempres)var tempprompt: { id: number, promptname: string, prompt: string, valuesname: string,values: string,username: string,account: string }[]=[]for(var m = 0; m < tempres.length; m++){tempprompt.push({id: tempres[m].id,promptname: tempres[m].promptname,prompt: tempres[m].prompt,valuesname: tempres[m].valuesname,values: tempres[m].values,username: tempres[m].username,account: tempres[m].account});}that.setData({prompt: tempprompt})}})改成
var tempprompt = []
这是服务器数据
[
{
id: 0,
promptname: '添加模板',
prompt: '写一篇字数{number}的工作总结',
valuesname: '字数',
values: 'number',
username: '山里灵活的狗',
account: '1'
},
{
id: 1,
promptname: '学习报告',
prompt: '写一篇主题为{theme}字数{number}的学习报告',
valuesname: '主题,字数',
values: 'theme,number',
username: '山里灵活的狗',
account: '1'
},
{
id: 2,
promptname: '测试',
prompt: '啊啊{a}',
valuesname: '啊',
values: 'a',
username: '恶神桃花',
account: '2'
},
{
id: 3,
promptname: '3',
prompt: '3',
valuesname: '3',
values: '3',
username: '3',
account: '3'
},
{
id: 4,
promptname: '4',
prompt: '4',
valuesname: '4',
values: '4',
username: '4',
account: '4'
}
]