我有这个使用 Typescript 编写的VueJS应用程序。我正在使用Axios从我的数据库中获取数据。这很有效,我得到的结果是一个数组,这正是我所期望的。当我console.log在这个数组上做时,我可以看到它是正确的结果。
但是,当我尝试遍历这个数组来为我的选择下拉列表创建选项时,我得到一个空白列表。为什么即使我循环的变量是一个数组,结果也没有出现?
编辑:我在这里添加了一张图片,显示了 Axios 结果 ( response.data)
export default class EditRoute extends Vue {
result: any;
selectedRoute: string;
constructor(){
super();
this.selectedRoute = "";
this.result = [];
}
loadData() {
axios.get('http://localhost:8080/routes')
.then(response => (this.result = response.data));
}
}
Run Code Online (Sandbox Code Playgroud)
<select name="routeSelect" v-model="selectedRoute">
<option v-for="routes in result" v-bind:key="routes.name" v-bind:value="routes.name"> {{ routes.name }} </option>
</select>
<button @click="loadData">Load data</button>
Run Code Online (Sandbox Code Playgroud) 我送一个FormData从VueJS应用程序中使用Axios。问题是当我输出FormData它时它是空的。我之前在发送文件时使用过相同的方法(我现在不发送文件),然后FormData显示我附加到它的正确数据。我附加的数据是字符串类型。
客户端
onUpload(): void {
const fd = new FormData();
fd.append("id", this.chosenRoute.id);
fd.append("name", this.routeName);
fd.append("description", this.routeDescription);
fd.append("activity", this.activity);
fd.append("preamble", this.preamble);
axios.post('http://localhost:8080/editRoute', fd, {
onUploadProgress: uploadEvent => {
console.log('Upload Progress' + Math.round(uploadEvent.loaded / uploadEvent.total) * 100 + " %");
}
}).then(
res => {
console.log(res);
});
}
Run Code Online (Sandbox Code Playgroud)
服务器端
app.post('/editRoute', function(req,res){
console.log(req.body);
const routeId = req.body.id;
console.log(routeId);
Route.findById(routeId, (err, route) => {
res.set("Access-Control-Allow-Origin", "*");
route.update(req.body);
});
});
Run Code Online (Sandbox Code Playgroud)