当前位置:   article > 正文

Vue基础(九)——ElementUI_vue elementui

vue elementui

一、介绍

ElementUI是一个基于Vue的UI框架。(bootstrap是基于jQuery的UI框架)

二、安装

1、下载依赖:

npm i element-ui -S

2、在main.js中引入element-ui:

  1. import Vue from 'vue';
  2. import ElementUI from 'element-ui';
  3. import 'element-ui/lib/theme-chalk/index.css';
  4. import App from './App.vue';
  5. Vue.config.productionTip = false
  6. Vue.use(ElementUI);
  7. new Vue({
  8. el: '#app',
  9. render: h => h(App),
  10. }).$mount('#app')

三、使用ElementUI组件(参考文档,不做介绍)

1、按钮

  1. <template>
  2. <div id="app">
  3. <h1>hello elementUI</h1>
  4. <el-button type="primary">主要按钮</el-button>
  5. </div>
  6. </template>

 

2、表单

3、表格

四、学生信息管理系统

查询、添加、删除。

1、后台使用Koa实现数据接口;

2、添加功能使用对话框(Dialog)弹出表单;

3、删除需要有确认提示(MessageBox)。

1、后台数据(example02项目)

(1)下载并引入模块:

  1. const Koa = require("koa");
  2. //post请求模块
  3. const parser = require("koa-parser");
  4. //设置路由
  5. const router = require("koa-router")();
  6. ///允许跨域
  7. const cors = require('koa2-cors');
  8. //静态目录
  9. const static = require("koa-static");
  10. const app = new Koa();
  11. app.use(cors());
  12. app.use(parser());
  13. app.use(static(__dirname + "/public"))
  14. app.use(router.routes());

(2)serve.js:

  1. const Koa = require("koa");
  2. //post请求模块
  3. const parser = require("koa-parser");
  4. //设置路由
  5. const router = require("koa-router")();
  6. ///允许跨域
  7. const cors = require('koa2-cors');
  8. //静态目录
  9. const static = require("koa-static");
  10. const app = new Koa();
  11. app.use(cors());
  12. app.use(parser());
  13. app.use(static(__dirname + "/public"))
  14. app.use(router.routes());
  15. //模拟数据库
  16. const studentList = [{
  17. id: 1,
  18. name: "小明",
  19. age: 2
  20. },
  21. {
  22. id: 2,
  23. name: "小红",
  24. age: 4
  25. },
  26. {
  27. id: 3,
  28. name: "小亮",
  29. age: 6
  30. },
  31. ]
  32. //get方法:获取学生列表
  33. router.get("/student", async ctx => {
  34. ctx.body = studentList;
  35. })
  36. //post方法:添加学生
  37. router.post("/student", async ctx => {
  38. let student = ctx.request.body.student;
  39. fruitList.push(student);
  40. ctx.body = true;
  41. })
  42. //delete方法:删除学生
  43. router.delete("/student/:id", async ctx => {
  44. let id = ctx.params.id;
  45. studentList.map((item, index) => {
  46. if (item.id == id) {
  47. studentList.splice(index, 1);
  48. }
  49. })
  50. ctx.body = true;
  51. })
  52. app.listen(3000, () => {
  53. console.log("server is running")
  54. })

2、前端项目(example01)

(1)App.vue:

  1. <template>
  2. <div id="app">
  3. <el-button type="text" @click="dialogVisible = true">添加学生</el-button>
  4. <el-table :data="studentList" border style="width: 100%">
  5. <el-table-column prop="id" label="ID" width="150">
  6. </el-table-column>
  7. <el-table-column prop="name" label="姓名" width="120">
  8. </el-table-column>
  9. <el-table-column prop="age" label="年龄" width="120">
  10. </el-table-column>
  11. <el-table-column label="操作" width="100">
  12. <template slot-scope="scope">
  13. <!-- scrop.row:是当前行的对象 -->
  14. <el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
  15. </template>
  16. </el-table-column>
  17. </el-table>
  18. <el-dialog title="添加学生信息" :visible.sync="dialogVisible" width="30%">
  19. <el-form>
  20. <el-form-item label="id">
  21. <el-input v-model="form.id"></el-input>
  22. </el-form-item>
  23. <el-form-item label="姓名">
  24. <el-input v-model="form.name"></el-input>
  25. </el-form-item>
  26. <el-form-item label="年龄">
  27. <el-input v-model="form.age"></el-input>
  28. </el-form-item>
  29. </el-form>
  30. <span slot="footer" class="dialog-footer">
  31. <el-button @click="dialogVisible = false">取 消</el-button>
  32. <el-button type="primary" @click="onSubmit">确 定</el-button>
  33. </span>
  34. </el-dialog>
  35. </div>
  36. </template>
  37. <script>
  38. import axios from "axios";
  39. export default {
  40. methods: {
  41. handleClick(row) {
  42. this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
  43. confirmButtonText: "确定",
  44. cancelButtonText: "取消",
  45. type: "warning",
  46. })
  47. .then(() => {
  48. axios
  49. .delete(`http://127.0.0.1:3000/student/${row.id}`)
  50. .then(() => {
  51. this.$message({
  52. type: "success",
  53. message: "删除成功!",
  54. });
  55. this.getStudentList();
  56. });
  57. })
  58. .catch(() => {
  59. this.$message({
  60. type: "info",
  61. message: "已取消删除",
  62. });
  63. });
  64. },
  65. getStudentList() {
  66. axios.get("http://127.0.0.1:3000/student").then((res) => {
  67. this.studentList = res.data;
  68. });
  69. },
  70. onSubmit() {
  71. axios
  72. .post("http://127.0.0.1:3000/student", {
  73. student: this.form,
  74. })
  75. .then(() => {
  76. this.getStudentList();
  77. //添加后,关闭添加框
  78. this.dialogVisible = false;
  79. });
  80. },
  81. },
  82. data() {
  83. return {
  84. studentList: [],
  85. dialogVisible: false,
  86. form: {
  87. id: "",
  88. name: "",
  89. age: "",
  90. },
  91. };
  92. },
  93. created() {
  94. this.getStudentList();
  95. },
  96. };
  97. </script>

3、效果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/96301
推荐阅读
相关标签
  

闽ICP备14008679号