{"id":19978005,"url":"https://github.com/tuture-dev/vue-admin-template","last_synced_at":"2025-10-25T10:06:55.455Z","repository":{"id":56861287,"uuid":"238877018","full_name":"tuture-dev/vue-admin-template","owner":"tuture-dev","description":"基于实际项目而改造的精简化Vue后台管理模板，帮助初学者快速上手后台管理系统的开发","archived":false,"fork":false,"pushed_at":"2020-02-08T02:22:52.000Z","size":298,"stargazers_count":199,"open_issues_count":1,"forks_count":76,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-12T10:11:58.482Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Vue","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tuture-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-07T08:34:34.000Z","updated_at":"2025-01-06T10:17:59.000Z","dependencies_parsed_at":"2022-09-10T23:20:31.568Z","dependency_job_id":null,"html_url":"https://github.com/tuture-dev/vue-admin-template","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuture-dev%2Fvue-admin-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuture-dev%2Fvue-admin-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuture-dev%2Fvue-admin-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuture-dev%2Fvue-admin-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tuture-dev","download_url":"https://codeload.github.com/tuture-dev/vue-admin-template/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241411543,"owners_count":19958753,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T03:30:02.701Z","updated_at":"2025-10-25T10:06:55.139Z","avatar_url":"https://github.com/tuture-dev.png","language":"Vue","readme":"# vue+elementUI 后台管理极简模板\n## 写在前面\n\n此篇文章为一篇说明文档，不是教你从零构建一个后台管理系统，而是基于一个实际项目，已经搭建好了一个后台管理系统的基础框架，教你如何在此基础上快速开发自己的后台管理系统，能让读者能在掌握一些基础知识的情况下，也能上手vue后台开发。只有接触项目，才能更好地理解自己所学知识的意义，触类旁通把死知识点变成活学活用的技能。\n## 先跑起来\n```bash\n# 克隆项目\ngit clone https://github.com/tuture-dev/vue-admin-template.git\n# 进入目录\ncd vue-admin-template\n# 安装依赖\nnpm install --registry=https://registry.npm.taobao.org\n# 运行\nnpm run dev\n```\n![1gdrAf.gif](https://s2.ax1x.com/2020/02/07/1gdrAf.gif)\n## 增添侧边导航\n1. 新建文件。在**src/views/** 新建一个空白的文件夹 **test**，在此目录下新建文件 **test.vue**\n2. 添加路由。打开 **src/router/index.js**，此文件为该项目的后台路由配置文件。在**constantRoutes**这个数组中，添加路由的格式如下：\n\n```\n{\n  path: '/test',  //url路径\n  component: Layout, // 此处不用动，这个全局统一的一个布局文件\n  children: [{\n    path: 'test',  // 二级路径\n    name: 'test',\n    component: () =\u003e import('@/views/test/test'), // 懒加载，此处写所添加文件的路径\n    meta: {\n      title: '测试', icon:'plane' //配置选项可配置测试名称和图标\n    }\n  }]\n},\n```\n我们可以自定义图标，格式的文件，可以在[iconfont](https://www.iconfont.cn/)中下载，之后放入**src/icons/svg** 目录下即可\n\n![16KUN6.png](https://s2.ax1x.com/2020/02/06/16KUN6.png)\n\n\n对于二级导航可以按照如下的方式进行配置。\n\n![1NXiqJ.png](https://s2.ax1x.com/2020/02/03/1NXiqJ.png)\n\n```\n  {\n    path: '/material',\n    component: Layout,\n    redirect: '/material/upload',\n    meta: {\n      title: '素材管理', //元信息，一级导航的名称\n      icon: 'plane' // 元信息，导航图标的名称\n    },\n    children: [{\n        path: 'check-template',\n        name: 'check-template',\n        component: () =\u003e import('@/views/material/check-template'),\n        meta: {\n          title: '查看模板',\n        }\n      },\n      {\n        path: 'logo',\n        name: 'logo',\n        component: () =\u003e import('@/views/material/check-logo'),\n        meta: {\n          title: '查看logo',\n        }\n      },\n      {\n        path: 'generate',\n        name: 'generate',\n        component: () =\u003e import('@/views/material/generate'),\n        meta: {\n          title: '生成素材',\n        }\n      },\n      {\n        path: 'check',\n        name: 'check',\n        component: () =\u003e import('@/views/material/check'),\n        meta: {\n          title: '查看素材',\n        }\n      },\n    ]\n  },\n```\n在此配置完成后，框架会自动地根据路由配置文件，生成边侧导航条目。我们所需要做的工作就是根据业务需求，编写一个个vue组件，往框架里面填充内容就OK了。\n\n### 使用Element UI组件\n[Element UI](https://element.eleme.cn/#/zh-CN/component/installation)提供了很多可复用的组件，对于一般的后台应用，这些组件完全可以满足需求。如果个性化需求不高的话，我们完全可以做一名“复制粘贴”工程师又称“CV”工程师，快速开发。\n\n对于每一个组件，其文档上都有效果示例与代码，只需选择所需组件，将其代码粘贴进我们的代码文件中，稍加修改即可。\n\n## 网络请求\n当整个框架搭建完毕以后，前端程序员最主要的工作就是发起请求，渲染数据。现在我们就来完整地走一遍这个过程。\n### 基础配置\n1. 配置代理。\n\n因为跨域资源请求的问题，在开发阶段所有和后端交互的网络请求在底层由node.js代理。[相关文档](https://cli.vuejs.org/config/#devserver-proxy)\n\n打开根目录下的**vue.config.js**文件\n\n```\n// 代理所有以‘/admin’开头的网络请求\nproxy: {\n  '/admin': {\n    target: `http://localhost:8886/`, //后台服务地址\n    changeOrigin: true,\n    pathRewrite: {\n    }\n  }\n}\n```\n2. 配置地址\n\n生产环境与开发环境通常有不同的服务地址。编辑 **.env.development** 以及 **.env.production** 这两个文件，修改其中的 **VUE_APP_BASE_API** 配置项即可\n\n以开发环境为例：\n```\nVUE_APP_BASE_API = '/admin'\n```\n\n3. 配置拦截器\n\n打开**src/utils/request.js**，此文件封装了一个axios请求对象，该系统中的网络请求都是基于这个对象来处理的。\n我们可以在网络请求发送之前和收到服务端回复之后做一些通用性的工作。比如根据服务端的状态码判断请求是否正常，若不正常给出相应的提示。\n\n```\nservice.interceptors.response.use(\n  response =\u003e {\n    const res = response.data\n    // 如果服务器的状态码不为200，说明请求异常，应给出错误提示。\n    if (res.code !== 200) {\n      Message({\n        message: res.msg || 'Error check your token or method',\n        type: 'error',\n        duration: 2 * 1000\n      })\n      return Promise.reject(new Error(res.msg || 'Error'))\n    } else {\n      return res\n    }\n  },\n  error =\u003e {\n    console.log('err' + error) // for debug\n    Message({\n      message: error.message,\n      type: 'error',\n      duration: 2 * 1000\n    })\n    return Promise.reject(error)\n  }\n)\n```\n4. 挂载请求对象\n\n在**src/main.js**首先导入网络请求对象，并挂载至Vue全局对象，这样在每个组件中直接引用即可，不用要再导入。\n\n```\nimport request from '@/utils/request'\nVue.prototype.req = request\n```\n\n### 请求与渲染\n1. 搭建一个简易node服务\n\n仅供教程说明使用\n\n```\nlet http = require('http');\nlet querystring = require('querystring');\nlet my_result = [{\n    date: '2016-05-02',\n    name: '王小虎',\n    address: '上海市普陀区金沙江路 1518 弄'\n}, {\n    date: '2016-05-04',\n    name: '王小虎',\n    address: '上海市普陀区金沙江路 1517 弄'\n}, {\n    date: '2016-05-01',\n    name: '王小虎',\n    address: '上海市普陀区金沙江路 1519 弄'\n}, {\n    date: '2016-05-03',\n    name: '王小虎',\n    address: '上海市普陀区金沙江路 1516 弄'\n}]\n\nlet server = http.createServer((req, res) =\u003e {\n    let post = '';\n    req.on('data', function (chunk) {\n        post += chunk;\n    });\n\n    req.on('end', function () {\n        res.writeHead(200, {\n            'Content-Type': 'application/json; charset=utf-8'\n        })\n        post = querystring.parse(post);\n        console.log('post-data:', post);\n        if (post) {\n            let result = {\n                code: 200,\n                // msg: \"server error\"\n                data: my_result\n            }\n            res.end(JSON.stringify(result));\n        } else {\n            let result = {\n                code: '0',\n                msg: '没有接受到数据'\n            }\n            res.end(JSON.stringify(result));\n        }\n    });\n});\nserver.listen(8886)\n//在命令行 node server.js 即可运行\n```\n2. 发起请求\n\n```\nthis.req({\n  url: \"getInfo?id=6\", // 此处写不同业务对应的url，框架会自动与baseURL拼接\n  data: {},\n  method: \"GET\"\n}).then(\n  res =\u003e {\n    // 请求成功后的处理\n    console.log(\"res :\", res);\n  },\n  err =\u003e {\n    // 请求失败后的处理\n    console.log(\"err :\", err);\n  }\n);\n```\n按照最佳实践，应该把网络请求统一抽离到单一文件，然后在每个具体的页面进行对服务端数据的处理。\n比如下面的这种形式，首先创建文件**src/api/test.js**，把在**test**组件中需要用到的网络请求都写入此文件。\n\n```\n// src/api/test.js\nimport request from '@/utils/request'\n\nexport function getList(params) {\n  return request({\n    url: 'getTableData',\n    method: 'get',\n    params\n  })\n}\n\n\n```\n\n在组件**test.vue**中引入请求方法\n\n```\nimport { getTableData } from \"@/api/test.js\";\n……\nmounted: function() {\n// 网络请求统一处理\ngetTableData().then(res =\u003e {\n  console.log(\"api tableData :\", res);\n  this.tableData = res.data;\n},err=\u003e{\n  console.log(\"err :\", err);\n});\n// 网络请求直接写在文件中\nthis.req({\n  url: \"getTableData\",\n  data: {},\n  method: \"GET\"\n}).then(\n  res =\u003e {\n    console.log(\"tableData :\", res);\n    this.tableData = res.data;\n  },\n  err =\u003e {\n    console.log(\"err :\", err);\n  }\n);\n},\n```\n3. 网络数据流\n\n![1Uif9H.png](https://s2.ax1x.com/2020/02/03/1Uif9H.png)\n\n在控制台可以看出，我们的请求地址为localhost:9528,而后台服务的的地址为localhost:8886。为啥不一样呢？我们以一流程图说明\n\n![1UFtKI.png](https://s2.ax1x.com/2020/02/03/1UFtKI.png)\n\n应用程序上线后，对于CORS跨域资源访问的问题，可以用类似的方案（Nginx反向代理）在前端解决。\n\n### Hello Table\n现在我们在**test.vue**中用Element UI所提供的 Table组件写一个表格数据展示页面。\n1. 进入Element UI [Table](https://element.eleme.cn/#/zh-CN/component/table)组件的说明文档，复制粘贴对应的代码。框架对于Element UI已经进行了全局引入，所以这些组件拿来即用。如果是其他第三方的组件，还需要我们自己引入后再使用。\n\n\n```\n\u003ctemplate\u003e\n  \u003cel-table :data=\"tableData\" style=\"width: 100%\"\u003e\n    \u003cel-table-column prop=\"date\" label=\"日期\" width=\"180\"\u003e\u003c/el-table-column\u003e\n    \u003cel-table-column prop=\"name\" label=\"姓名\" width=\"180\"\u003e\u003c/el-table-column\u003e\n    \u003cel-table-column prop=\"address\" label=\"地址\"\u003e\u003c/el-table-column\u003e\n  \u003c/el-table\u003e\n\u003c/template\u003e\n```\n2. 在组件装载时请求数据\n\n```\n  mounted: function() {\n    this.req({\n      url: \"getTableData\",\n      data: {},\n      method: \"GET\"\n    }).then(\n      res =\u003e {\n        console.log(\"tableData :\", res);\n        this.tableData = res.data  // 数据渲染\n      },\n      err =\u003e {\n        console.log(\"err :\", err); // 当业务逻辑发生错误时 进行处理\n      }\n    );\n  },\n```\n3. 实际效果\n\n业务逻辑正常\n\n![168XP1.png](https://s2.ax1x.com/2020/02/06/168XP1.png)\n\n业务出错时，弹出服务端给的错误信息。弹出此信息是在拦截器**request.js**文件定义的，这是统一的业务逻辑错误处理，也可以在每个请求中单独处理。\n\n![16JiOU.png](https://s2.ax1x.com/2020/02/06/16JiOU.png)\n\n\n\n\n## 简易权限控制\n\n这种权限控制方式为静态方式，有些复杂的动态权限管理不在此说明。\n该框架每一次的路由跳转都会通过**router.beforeEach**检验一遍权限，我们可以在这里添加配置项。\n进入文件 **src/permission.js**，以只有管理员才能进入用户管理界面为例：\n\n```\nif (to.path === '/user/user') {\n  let id = JSON.parse(localStorage.getItem('userInfo')).id\n  console.log(id)\n  if (id \u003e 2) { //id\u003e2位普通用户，无权访问\n    next({ path: '/task' })\n    window.alert('permission denied')\n  }\n}\n```\n\n## 结语\n\n到此后台开发中最常用的操作已经介绍完毕，对于一些小项目已经是绰绰有余。花盆里长不出参天松，庭院里练不出千里马，项目写得多了很多东西就自然而然的通透了。一千个读者就有一千个哈姆雷特，这只是一个基础框架，在开发的过程，需要我们自己对其修改，让它成为你自己最顺手的框架。\n\n\u003e 此项目演绎自： https://github.com/PanJiaChen/vue-admin-template.git","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuture-dev%2Fvue-admin-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftuture-dev%2Fvue-admin-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuture-dev%2Fvue-admin-template/lists"}