Vue2项目练手——通用后台管理项目第一节

这篇具有很好参考价值的文章主要介绍了Vue2项目练手——通用后台管理项目第一节。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

知识补充

yarn和npm区别

npm的缺点:

  1. npm install时候巨慢
  2. 同一个项目,安装的时候无法保持一致性。 由于package.json文件中版本号的特点。

“5.0.3” 安装指定的5.0.3版本
“~5.0.3” 表示安装5.0.X中最新的版本
“^5.0.3” 表示安装5.X.X中最新的版本

有时候会出现版本不一致不能运行的情况。

yarn的优点

  1. 速度快
    并行安装:同步执行所有任务,不像npm按照队列执行每个package,package安装不完成,后面也无法执行。
    离线模式:安装过得软件包,直接从缓存中获取,不用像npm从网络中获取。
  2. 安装版本统一
  3. 更简洁的输出:
    npm输出所有被安装上的依赖,但是yarn只打印必要的信息
  4. 多注册来源处理:安装某个包,只会从一个注册来源去安装,
  5. 更好的语义化,yarn安装和卸载是yarn add/remove,npm是npm install/uninstall

npm查看镜像和设置镜像

npm config get registry
npm config set registry https://registry.npmmirror.com/

项目介绍

项目演示

Vue2项目练手——通用后台管理项目第一节,前端,Vue基础,前端,javascript,vue.js

项目的技术栈

Vue2项目练手——通用后台管理项目第一节,前端,Vue基础,前端,javascript,vue.js

  1. 使用yarn安装vue-cli

yarn global add @vue/cli

项目搭建

先vue create创建一个项目,然后安装element-ui组件和vue-router,less等组件

文件目录

Vue2项目练手——通用后台管理项目第一节,前端,Vue基础,前端,javascript,vue.js

创建路由,引入element-ui

router/index.js

import VueRouter from "vue-router";
import Login from "@/pages/Login.vue";
import Users from "@/pages/Users.vue";
import Main from '@/pages/Main.vue'
import Home from "@/pages/Home.vue";

const router= new VueRouter({
    // 浏览器模式设置,设置为history模式
    mode:'history',
    routes:[
        {
            path:"/login",
            component:Login,
            meta:{title:"登录"},
        },
        {
            // 主路由
            name:"main",
            path:'/',
            component:Main,
            children:[  //子路由
                {
                    name:"users",
                    path:"users",
                    component:Users,
                    meta:{title:"用户"}
                },
                {
                    name:"home",
                    path:"home",
                    component:Home,
                    meta:{title:"主页"}
                }
            ]
        }

    ]
})

// 后置路由守卫
router.afterEach((to,from)=>{
    document.title=to.meta.title||"通用后台管理系统"
})
export default router

main.js

import Vue from 'vue'
import App from './App.vue'
import VueRouter from "vue-router";
import router from "@/router";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css'
Vue.config.productionTip = false
Vue.use(VueRouter)
Vue.use(ElementUI)
new Vue({
  router,
  render: h => h(App),
}).$mount('#app')

pages/Users.vue

<template>
  <div>
    我是Users组件
  </div>

</template>

<script>
export default {
  name: "Users",
}
</script>

<style scoped>

</style>

pages/Main.vue

<template>
  <div>
    <h1>main</h1>
    <router-view></router-view>
  </div>

</template>

<script>
export default {
  name: "Main",
}
</script>

<style scoped>

</style>

pages/Home.vue

<template>
  <div>
    home内容
  </div>

</template>

<script>
export default {
  name: "Home",
}
</script>

<style scoped>

</style>

pages/Login.vue

<template>
  <div id="app">
    <div class="main-content">
      <div class="title">系统登录</div>
      <div class="content">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="name">
            <el-input v-model="ruleForm.name" ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" type="password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-row :gutter="20">
              <el-col :span="12" :offset="4"><router-link to="/login"><el-button type="primary" @click="submitForm('ruleForm')">登录</el-button></router-link></el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </div>
    </div>

  </div>

</template>

<script>
export default {
  name: "login",
  data(){
    return{
      ruleForm: {
        name: '',
        password:""
      },
      rules: {
        name: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        password: [
          {required:true,message:"请输入密码",trigger:"blur"}
        ]
      }
    }
  },
  methods:{
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style lang="less" scoped>
*{
  padding: 0;
  margin: 0;
}
#app {
  display: flex;
  background-color: #333;
  height: 800px;
  .main-content{
    height: 300px;
    width: 400px;
    background-color: #fff;
    margin: 200px auto;
    border-radius: 10px;
    padding: 30px;
    box-sizing: border-box;
    box-shadow: 5px  5px 10px rgba(0,0,0,0.5),-5px -5px 10px rgba(0,0,0,0.5);
    .title{
      font-size: 20px;
      text-align: center;
      //margin-top: 30px;
      font-weight: 300;
    }
    .content{
      margin-top: 30px;
    }
  }
}
</style>

App.vue

<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>

export default {
  name: 'App',
}
</script>

<style lang="less">
*{
  padding: 0;
  margin: 0;
}
</style>

Vue2项目练手——通用后台管理项目第一节,前端,Vue基础,前端,javascript,vue.js

使用element-ui搭建主页样式

main页面布局使用这个

Vue2项目练手——通用后台管理项目第一节,前端,Vue基础,前端,javascript,vue.js
Vue2项目练手——通用后台管理项目第一节,前端,Vue基础,前端,javascript,vue.js

Main.vue

<template>
  <div>
    <el-container>
      <el-aside width="200px">Aside</el-aside>
      <el-container>
        <el-header>Header</el-header>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>

</template>

<script>
export default {
  name: "Main",
}
</script>

<style scoped>

</style>

Vue2项目练手——通用后台管理项目第一节,前端,Vue基础,前端,javascript,vue.js

导航栏使用

Vue2项目练手——通用后台管理项目第一节,前端,Vue基础,前端,javascript,vue.js

导航栏适配

Main.vue

<template>
  <div>
    <el-container>
      <el-aside width="200px">
        <CommonAside></CommonAside>
      </el-aside>
      <el-container>
        <el-header>Header</el-header>
        <el-main>
<!--         路由出口,路由匹配到的组件将渲染在这里 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>

</template>

<script>
import CommonAside from "@/components/CommonAside.vue";
export default {
  name: "Main",
  components:{CommonAside}
}
</script>

<style scoped>

</style>

App.vue

<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>

export default {
  name: 'App',
}
</script>

<style lang="less">
html,body,h3{
  padding: 0;
  margin: 0;
}
</style>

CommonAside

<template>
    <el-menu default-active="1-4-1" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
             :collapse="isCollapse" background-color="#545c64" text-color="#fff"
             active-text-color="#ffd04b">
      <h3>通用后台管理系统</h3>
      <el-menu-item index="2" v-for="item in noChildren" :key="item.name" :index="item.name">
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title">{{item.label}}</span>
      </el-menu-item>
      <el-submenu :index="item.label" v-for="item in hasChildren" :key="item.label">
        <template slot="title">
          <i :class="`el-icon-${item.icon}`"></i>
          <span slot="title">{{item.label}}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item :index="subItem.path" :key="subItem.path" v-for="subItem in item.children">
            {{subItem.label}}
          </el-menu-item>
        </el-menu-item-group>
      </el-submenu>
    </el-menu>

</template>



<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.el-menu{
  height: 100vh;  //占据页面高度100%
  h3{
    color: #fff;
    text-align: center;
    line-height: 48px;
    font-size: 16px;
    font-weight: 400;
  }
}
</style>

<script>
export default {
  data() {
    return {
      isCollapse: false,
      menuData:[
        {
          path:'/',
          name:"home",
          label:"首页",
          icon:"s-home",
          url:'Home/Home'
        },
        {
          path:'/mail',
          name:"mall",
          label:"商品管理",
          icon:"video-play",
          url:'MallManage/MallManage'
        },
        {
          path:'/user',
          name:"user",
          label:"用户管理",
          icon:"user",
          url:'userManage/userManage'
        },
        {
          label:"其他",
          icon:"location",
          children:[
            {
              path:'/page1',
              name:"page1",
              label:"页面1",
              icon:"setting",
              url:'Other/PageOne'
            },
            {
              path:'/page2',
              name:"page2",
              label:"页面2",
              icon:"setting",
              url:'Other/PageTwo'
            },
          ]
        },
      ]
    };
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    }
  },
  computed:{
    //没有子菜单的数据
    noChildren(){
      return this.menuData.filter(item=>!item.children)
    },
    hasChildren(){
      return this.menuData.filter(item=>item.children)
    }
    //有子菜单数组
  }
}
</script>

Vue2项目练手——通用后台管理项目第一节,前端,Vue基础,前端,javascript,vue.js

导航栏跳转

文件目录

Vue2项目练手——通用后台管理项目第一节,前端,Vue基础,前端,javascript,vue.js

src/router/index.js

import VueRouter from "vue-router";
import Login from "@/pages/Login.vue";
import Users from "@/pages/Users.vue";
import Main from '@/pages/Main.vue'
import Home from "@/pages/Home.vue";
import Mall from "@/pages/Mall.vue";
import PageOne from "@/pages/PageOne.vue";
import PageTwo from "@/pages/PageTwo.vue";

const router= new VueRouter({
    // 浏览器模式设置,设置为history模式
    // mode:'history',
    routes:[
        {
            path:"/login",
            component:Login,
            meta:{title:"登录"},
        },
        {
            // 子路由
            name:"main",
            path:'/',
            redirect:"/home",  //重定向 当路径为/,则重定向home
            component:Main,
            children:[
                {
                    name:"user",
                    path:"user",
                    component:Users,
                    meta:{title:"用户管理"}
                },
                {
                    name:"home",
                    path:"home",
                    component:Home,
                    meta:{title:"首页"}
                },
                {
                    name:"mall",
                    path:"mall",
                    component:Mall,
                    meta:{title:"商品管理"}
                },
                {
                    name:"page1",
                    path:"page1",
                    component:PageOne,
                    meta:{title:"页面1"}
                },
                {
                    name:"page2",
                    path:"page2",
                    component:PageTwo,
                    meta:{title:"页面2"}
                }
            ]
        }

    ]
})

// 后置路由守卫
router.afterEach((to,from)=>{
    document.title=to.meta.title||"通用后台管理系统"
})
export default router

src/pages/Mall.vue

<template>
  <div>
    我是mall
  </div>

</template>

<script>
export default {
  name: "Mall",
}
</script>

<style scoped>

</style>

src/pages/pageOne.vue

<template>
  <div>
    我是PageOne
  </div>

</template>

<script>
export default {
  name: "PageOne",
}
</script>

<style scoped>

</style>

src/pages/PageTwo.vue

<template>
  <div>
    我是PageTwo
  </div>

</template>

<script>
export default {
  name: "PageTwo",
}
</script>

<style scoped>

</style>

src/components/CommonAside.vue

<template>
    <el-menu default-active="1-4-1" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
             :collapse="isCollapse" background-color="#545c64" text-color="#fff"
             active-text-color="#ffd04b">
      <h3>通用后台管理系统</h3>
      <el-menu-item @click="clickMenu(item)"  v-for="item in noChildren" :key="item.name" :index="item.name">
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title">{{item.label}}</span>
      </el-menu-item>
      <el-submenu :index="item.label" v-for="item in hasChildren" :key="item.label">
        <template slot="title">
          <i :class="`el-icon-${item.icon}`"></i>
          <span slot="title">{{item.label}}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item @click="clickMenu(subItem)" :index="subItem.path" :key="subItem.path" v-for="subItem in item.children">
            {{subItem.label}}
          </el-menu-item>
        </el-menu-item-group>
      </el-submenu>
    </el-menu>

</template>



<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.el-menu{
  height: 100vh;  //占据页面高度100%
  h3{
    color: #fff;
    text-align: center;
    line-height: 48px;
    font-size: 16px;
    font-weight: 400;
  }
}
</style>

<script>
export default {
  data() {
    return {
      isCollapse: false,
      menuData:[
        {
          path:'/',
          name:"home",
          label:"首页",
          icon:"s-home",
          url:'Home/Home'
        },
        {
          path:'/mall',
          name:"mall",
          label:"商品管理",
          icon:"video-play",
          url:'MallManage/MallManage'
        },
        {
          path:'/user',
          name:"user",
          label:"用户管理",
          icon:"user",
          url:'userManage/userManage'
        },
        {
          label:"其他",
          icon:"location",
          children:[
            {
              path:'/page1',
              name:"page1",
              label:"页面1",
              icon:"setting",
              url:'Other/PageOne'
            },
            {
              path:'/page2',
              name:"page2",
              label:"页面2",
              icon:"setting",
              url:'Other/PageTwo'
            },
          ]
        },
      ]
    };
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    clickMenu(item){
      // console.log(item)
      this.$router.push(item.path)
    }
  },
  computed:{
    //没有子菜单的数据
    noChildren(){
      return this.menuData.filter(item=>!item.children)
    },
    //有子菜单数组
    hasChildren(){
      return this.menuData.filter(item=>item.children)
    }
  }
}
</script>

Vue2项目练手——通用后台管理项目第一节,前端,Vue基础,前端,javascript,vue.js文章来源地址https://www.toymoban.com/news/detail-681266.html

到了这里,关于Vue2项目练手——通用后台管理项目第一节的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 超适合练手的一套JavaWeb项目 (超市后台管理系统)

    1.搭建一个maven web项目 2.配置Tomcat 3.测试项目是否能够跑起来 4.导入项目中遇到的jar包 5.创建项目结构 1.数据库配置文件 db.properties文件代码 2.编写数据库的公共类 java代码 3.编写字符编码过滤器 xml代码 java dao层接口代码 java dao接口的实现类代码 java service接口代码

    2024年02月05日
    浏览(40)
  • 基于VUE3+Layui从头搭建通用后台管理系统(前端篇)一:项目规划及初始化

      使用vue3+Layui实现通用管理系统前端,使用vue3+layui搭建系统UI界面,使用nodejs搭建模拟web服务器,使用echarts实现系统可视化模块,可以此项目为基础进行扩展开发,快速搭建管理系统,具体内容如下:    1. 常见功能实现: 实现用户登录(用户名密码登录、手机验证码

    2024年02月13日
    浏览(35)
  • Vue2实现仿小米商城练手项目前端篇(2-首页实现)

    去年寒假里学习了 Vue.js ,开学后想做一个完整的练手项目实战一下,最后决定模仿小米手机官网做一个网站项目,具体参考了Github上一位作者的项目。 现在已经基本完成了,分享在CSDN作为学习记录。 技术支持 :该项目采用前后端分离的设计结构,使用 Vue2 + Vue-Router + Vue

    2024年02月09日
    浏览(70)
  • 【Vue2+Element ui通用后台】面包屑和tag功能

    Element ui 面包屑:显示当前页面的路径,快速返回之前的任意页面,完成效果如下: 我们之前把头部的代码封装到了 CommonHeader.vue 中,面包屑部分直接写死了一个首页,我们可以把官网的面包屑代码先直接复制过来: 之前我们在 store/tab.js 中我们写了头部的是否开关,这里我

    2024年02月02日
    浏览(37)
  • 【Vue2+Element ui通用后台】整体布局、数据展示、axios封装

    我们新建 Home 组件来展示右侧的内容 整体布局我们使用layout布局,通过基础的 24 分栏,迅速简便地创建布局。由于左侧占比较小,我们分为 8 和 16 即可 然后每个卡片样式的部分,我们使用 Card 卡片 我们先增加一个个人信息的展示: 然后在App.vue 中给 p 标签去掉默认样式 在

    2024年02月14日
    浏览(24)
  • 【vue后台管理系统】基于Vue+Element-UI+ECharts开发通用管理后台(中)

    点击菜单图标之前: 点击菜单图标之后: 首先我们要知道菜单栏的收缩,由el-menu的collapse属性控制: 我们通过分析可以知道: 菜单按钮的点击是在CommonHeader.vue组件中,而我们修改的collapse属性却在CommonAside.vue中,这是两个不同的组件。很明显这涉及到了组件间的通信问题,

    2024年02月03日
    浏览(38)
  • Vue2-快速搭建pc端后台管理系统

    vue-element-admin Star(84k) vue-antd-admin Star(3.5k) 官网链接: https://panjiachen.github.io/vue-element-admin-site/zh/ 我这里搭建的是基础模版 vue-admin-template (推荐) 官网链接: https://iczer.gitee.io/vue-antd-admin-docs/

    2024年02月11日
    浏览(26)
  • Vue2+element-ui后台管理系统(静态页面)

    node:https://nodejs.org/en/ git:https://git-scm.com/ vue:https://v2.cn.vuejs.org/v2/guide/installation.html element-ui:https://element.eleme.cn/#/zh-CN/component/installation 项目所需:https://pan.baidu.com/s/1ua0jp9YCtPH6slE49HDUBw 提取码:kkkk 在node和vue都调试、配置好的情况下使用vscode 在终端中输入命令 npm i -g @vue

    2024年02月06日
    浏览(46)
  • 基于VUE3+Layui从头搭建通用后台管理系统(前端篇)十一:通用表单组件封装实现

      本章实现通用表单组件,根据实体配置识别实体属性,并自动生成编辑组件,实现对应数据填充、校验及保存等逻辑。 1. 详细课程地址: https://edu.csdn.net/course/detail/38183 2. 源码下载地址: 点击下载

    2024年02月10日
    浏览(38)
  • 从零开发一个自己的Shiro+Vue通用后台管理系统(附源码)

    分享一个基于Shiro和Vue构建的通用后台管理系统项目,该项目实现了jwt无状态登录、redis缓存、token续期和可控权限管理。

    2024年02月06日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包