将restaurants设置为异步加载,确保能在前台展示,api:http://192.168.2.106:9000/ceshi
时间: 2024-11-29 10:38:03 浏览: 7
为了实现 `restaurants` 数据的异步加载并确保在前端正确展示,你需要对现有的 Vue 实例进行一些修改。具体步骤如下:
1. **修改 `data` 中的 `restaurants` 初始值**:将其初始化为空数组。
2. **添加一个方法来获取数据**:使用 `axios` 或原生的 `fetch` API 来从指定的 API 获取数据。
3. **在 `mounted` 生命周期钩子中调用该方法**:确保数据在组件挂载后被加载。
以下是修改后的代码示例:
### 修改后的 HTML 和 JavaScript 部分
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="../static/element-ui/lib/theme-chalk/index.css">
<link rel="stylesheet" href="../static/bootstrap/dist/css/bootstrap.css">
<script src="../static/jquery/dist/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../static/bootstrap/dist/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div class="panel panel-primary">
<div class="panel-heading" style="text-align: center;" id="title">����ƻ�������ͳ��ϵͳ</div>
<div class="panel-body"></div>
<br>
<div id="app">
<template>
<el-autocomplete v-model="state" :fetch-suggestions="querySearchAsync" placeholder="����������" @select="handleSelect" clearable @clear="handleClear"></el-autocomplete>
</template>
<el-descriptions title="��������" :column="10" border style="font-size: medium;">
<el-descriptions-item label="سঞ��"> 920113 </el-descriptions-item>
<el-descriptions-item label="����"><el-tag type="danger" style="font-size: larger;">���Ѿ�</el-tag></el-descriptions-item>
<el-descriptions-item label="������ʽ"><span class="label label-warning" style="font-size: larger;">��Ƥ���������</span></el-descriptions-item>
<el-descriptions-item label="����edad��"><el-tag>����</el-tag></el-descriptions-item>
<el-descriptions-item label="����"><el-tag style="font-size: larger;">2024��11��6��14:29:07</el-tag-success></el-descriptions-item>
<el-descriptions-item label="����"><el-tag style="font-size: large;color: red;">2024-11-6 14:29:12</el-tag></el-descriptions-item>
</el-descriptions>
<hr>
<div style="background-color:lightskyblue;font-size: large;"><strong>������ͳ��</strong></div>
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column label="����" width="180">
<template slot-scope="scope">
<i class="el-icon-time"></i>
<span v-if="!scope.row.editing" style="margin-left: 10px">{{ scope.row.date }}</span>
<el-input v-else type="datetime-local" v-model="scope.row.date" clearable placeholder="���������ں�ʱ��"></el-input>
</template>
</el-table-column>
<el-table-column label="����" width="180">
<template slot-scope="scope">
<el-popover trigger="hover" placement="top">
<p>����: {{ scope.row.name }}</p>
<p>��ַ: {{ scope.row.address }}</p>
<div slot="reference" class="name-wrapper">
<el-tag size="medium">{{ scope.row.name }}</el-tag>
</div>
</el-popover>
</template>
</el-table-column>
<!-- 其他列保持不变 -->
<el-table-column label="����">
<template slot-scope="scope">
<el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)" v-if="!scope.row.editing">�༭</el-button>
<el-button size="mini" type="success" @click="handleSave(scope.$index, scope.row)" v-else>����</el-button>
<el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">ɾ��</el-button>
</template>
</el-table-column>
</el-table>
</template>
</div>
</div>
<script src="../static/vue/dist/vue.min.js"></script>
<script src="../static/element-ui/lib/index.js" type="text/javascript" charset="utf-8"></script>
<script>
let arr = [
{ "value": "920113", "address": "����������·144��" },
]
var app = new Vue({
el: '#app',
data: {
tableData: [
{ date: '2016-05-02', name: '��С��', address: '�Ϻ�����������ɳ��· 1518 Ū', editing: false },
{ date: '2016-04', name: '��С��', address: '�Ϻ�����������ɳ��· 1517 Ū', editing: false },
{ date: '2016-01', name: '��С��', address: '�Ϻ�����������ɳ��· 1519 Ū', editing: false },
{ date: '2016-05-03', name: '��С��', address: '�Ϻ�����������ɳ��· 1516 Ū', editing: false }
],
restaurants: [],
state: '',
timeout: null
},
mounted() {
this.fetchRestaurants();
},
methods: {
fetchRestaurants() {
axios.get('http://192.168.2.106:9000/ceshi')
.then(response => {
this.restaurants = response.data;
})
.catch(error => {
console.error('Error fetching restaurants:', error);
});
},
querySearchAsync(queryString, cb) {
var results = queryString ? this.restaurants.filter(this.createStateFilter(queryString)) : this.restaurants;
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
cb(results);
}, 2000 * Math.random());
},
createStateFilter(queryString) {
return (state) => {
return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
};
},
handleSelect(item) {
console.log(item);
},
handleClear() {
this.state = '';
},
handleEdit(index, row) {
this.$set(this.tableData[index], 'editing', true);
},
handleSave(index, row) {
this.$set(this.tableData[index], 'editing', false);
this.$message.success('����rxjs');
},
handleDelete(index, row) {
this.tableData.splice(index, 1);
this.$message({ type: 'error', message: 'ɾ���rxjs', duration: 2000 })
}
}
});
</script>
</body>
</html>
```
### 关键点解释
1. **引入 Axios**:在 `<head>` 标签中引入了 `axios` 库,用于发送 HTTP 请求。
2. **`fetchRestaurants` 方法**:在 `methods` 对象中定义了一个 `fetchRestaurants` 方法,该方法通过 `axios.get` 请求从指定的 API 获取数据,并将结果赋值给 `restaurants` 数组。
3. **`mounted` 生命周期钩子**:在 `mounted` 钩子中调用 `fetchRestaurants` 方法,确保在组件挂载后立即加载数据。
这样,当页面加载时,`restaurants` 数据会从指定的 API 异步加载并在前端展示。
阅读全文