多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
首先我们移除 eslint 的校验 ![](https://img.kancloud.cn/f0/1f/f01f410dd703044abde0a318beb8d58c_2412x1454.png) 接下来我们正式进入`增删改查` 的正式操作。 查询 ## 1、在 `router/index.js` 下添加如下的路由 ``` import User from '@/components/User' ``` ``` { path: '/user', name: 'User', component: User } ``` ![](https://img.kancloud.cn/0b/72/0b72169f410af2edb848f6fb9e3e8109_1264x1358.png) 同时 在 components 下新建 User 文件夹,并在 User 文件夹下新建 index.vue 文件 ![](https://img.kancloud.cn/35/7b/357bf6812989d7d701dff01be99f5774_608x620.png) ![](https://img.kancloud.cn/08/fd/08fd6546152745a623994a65d871327d_798x324.png) ## 2、启动 npm 调试 ``` npm run dev ``` 访问 http://localhost:8080/#/user 看到如下页面 ![](https://img.kancloud.cn/1e/82/1e82b9a5d57a40181e394d4488b69d54_2604x1042.png) ## 3、去 elementui 网站去找 table组件 https://element.eleme.cn/#/zh-CN/component/table 从业务需求来看,我们需要在列表上展示 `id`,`name`, `age`,`create_time` 同时还得有个操作的按钮,这几个内容,因此我们需要编辑表格的 表头 ![](https://img.kancloud.cn/87/84/878416ed87c14773ed615c34fe6ec075_1766x1144.png) 另外,我们操作的地方放了 两个点击的操作,因此我们要在 methods 中定义这两个操作 ![](https://img.kancloud.cn/9d/77/9d77d4ad943f2ac5eefcb4d6a5529bea_836x832.png) ``` <template> <div class="userList"> <el-table :data="tableData" style="width: 100%"> <el-table-column prop="id" label="ID"> </el-table-column> <el-table-column prop="name" label="姓名"> </el-table-column> <el-table-column prop="age" label="年龄"> </el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button> <el-button @click="handleDel(scope.row)" type="text" size="small">删除</el-button> </template> </el-table-column> </el-table> </div> </template> <script> export default { data() { return { tableData: [] } }, methods: { // 编辑 handleEdit(row) { }, // 删除 handleDel(row) { }, } } </script> ``` ## 4、开始请求接口获取列表数据 我们定义一个方法 叫 getUserList 去调用接口获取数据 根据接口文档, 请求地址: http://api2.pfecms.com/user/index 参数:page limit ``` getUserList() { this.$http({ method: 'get', url: 'http://api2.pfecms.com/user/index', data: { limit: 15, page: 1 }, headers:{ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } }).then((res) => { let result = res.data; }).catch((error) => { console.log(error); }); } ``` 并且在 组件挂载之后就调用,填入我们对应的 地址和参数,就可以调用接口了 在 then的回调中,res.data 就是服务器返回的数据 ![](https://img.kancloud.cn/fe/ea/feeac99e24d421ef351d4f0de9fa7abc_846x366.png) >[danger] 服务器返回的数据中data 中的 data 就是存放的用户列表数据 ``` let result = res.data; if (result.code == 0) { this.tableData = result.data.data } ``` 因此,我们只需要将 tableData 赋值,即可让数据表格显示用户列表数据 ![](https://img.kancloud.cn/fb/f0/fbf0367b2359c3db2140ec8e54afa6e9_1916x928.png) ![](https://img.kancloud.cn/32/b4/32b4521a09e61fac9e7f2964c99db0b8_3374x456.png)