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

news2024/9/22 1:13:08

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

  • 知识补充
    • yarn和npm区别
      • npm的缺点:
      • yarn的优点
    • npm查看镜像和设置镜像
  • 项目介绍
    • 项目的技术栈
  • 项目搭建
    • 文件目录
  • 创建路由,引入element-ui
    • router/index.js
    • main.js
    • pages/Users.vue
    • pages/Main.vue
    • pages/Home.vue
    • pages/Login.vue
    • App.vue
  • 使用element-ui搭建主页样式
    • main页面布局使用这个
      • Main.vue
    • 导航栏使用
  • 导航栏适配
    • Main.vue
    • App.vue
    • CommonAside
  • 导航栏跳转
    • 文件目录
    • src/router/index.js
    • src/pages/Mall.vue
    • src/pages/pageOne.vue
    • src/pages/PageTwo.vue
    • src/components/CommonAside.vue

知识补充

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/

项目介绍

项目的技术栈

在这里插入图片描述

  1. 使用yarn安装vue-cli

yarn global add @vue/cli

项目搭建

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

文件目录

请添加图片描述

创建路由,引入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>

请添加图片描述

使用element-ui搭建主页样式

main页面布局使用这个

请添加图片描述
请添加图片描述

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>

请添加图片描述

导航栏使用

请添加图片描述

导航栏适配

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>

请添加图片描述

导航栏跳转

文件目录

请添加图片描述

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>

请添加图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/931946.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vue脚手架的安装并创建项目

在之前的练习中我们都是采用引入jquery、vue文件的形式&#xff0c;很多es6的新特性都是无法使用的&#xff0c;因此现在我们采用脚手架进行安装相关的配置。 1、下载node.js&#xff0c;使用其中内置的npm来下载安装包。 直接在官网中下载长期支持版本即可。点击进入node.js官…

建模杂谈系列234 基于图的程序改造

说明 为了进一步提升程序设计与运维的可靠性&#xff0c;我觉得&#xff08;目前看来&#xff09;只有依赖图的结构。 提升主要包含如下方面&#xff1a; 1 程序结构的简洁性&#xff1a;节点和边2 程序执行的可视化&#xff1a;交通图(红、黄、绿)3 程序支持的逻辑复杂性。…

Multisim软件安装包分享(附安装教程)

目录 一、软件简介 二、软件下载 一、软件简介 Multisim软件是一款电路仿真和设计软件&#xff0c;由美国国家仪器公司&#xff08;National Instruments&#xff09;开发。它提供了一个交互式的图形界面&#xff0c;使用户能够轻松地构建和仿真电路。以下是Multisim软件的详…

什么是devos勒索病毒,中招之后该怎么办?勒索病毒解密,数据恢复

Devos勒索病毒是一种比较常见的勒索病毒病毒&#xff0c;它利用加密技术来锁定用户的文件&#xff0c;并要求支付赎金才能解锁。这种病毒已经引起了全球范围内的关注&#xff0c;也给众多的企业主和个人造成了不可估量的损失。 Devos勒索病毒的起源尚不清楚&#xff0c;但它的攻…

无涯教程-分类算法 - 逻辑回归

逻辑回归是一种监督学习分类算法&#xff0c;用于预测目标变量的概率&#xff0c;目标或因变量的性质是二分法&#xff0c;这意味着将只有两种可能的类。 简而言之&#xff0c;因变量本质上是二进制的&#xff0c;其数据编码为1(代表成功/是)或0(代表失败/否)。 在数学上&…

Linux保存退出和不保存退出命令

Vim编辑器 vim 要编辑的文件输入i进入编辑模式保存退出&#xff1a; 按Esc键退出insert模式&#xff0c;然后输入冒号(:)&#xff0c;输入wq!可以保存并退出. 不保存退出&#xff1a; 按Esc键退出insert模式&#xff0c;然后输入冒号(:)&#xff0c;输入q!可以不保存并退出。…

Autosar存储入门系列03_Autosar中NVM状态机及存储调用逻辑

本文框架 0.前言1. NVM状态机介绍2. NVM读/写基本逻辑2.1 NVM读操作2.2 NVM写操作2.2.1 实时写2.2.2 下电写 2.3 NVM写入注意事项 0.前言 本系列是Autosar存储入门系列&#xff0c;希望能从学习者的角度把存储相关的知识点梳理一遍&#xff0c;这个过程中如果大家觉得有讲得不…

图文并茂:Python Tkinter从入门到高级实战全解析

目录 介绍什么是Tkinter&#xff1f;准备工作第一个Tkinter程序界面布局事件处理补充知识点 文本输入框复选框和单选框列表框弹出对话框 综合案例&#xff1a;待办事项列表总结 介绍 欢迎来到本篇文章&#xff0c;我们将带您深入了解如何在Python中使用Tkinter库来创建图形用…

弯道超车必做好题集锦二(C语言选择题)

前言&#xff1a; 编程想要学的好&#xff0c;刷题少不了&#xff0c;我们不仅要多刷题&#xff0c;还要刷好题&#xff01;为此我开启了一个弯道超车必做好题锦集的系列&#xff0c;每篇大约10题左右。此为第二篇选择题篇&#xff0c;该系列会不定期更新&#xff0c;后续还会…

【网络云盘客户端】——项目简介

项目简介 网络云盘客户端时基于QT/C框架实现了一个网络云盘客户端软件&#xff0c;主要功能包括用户的注册&#xff0c;登录&#xff0c;显示用户的个人文件列表&#xff0c;以及文件的上传&#xff0c;下载&#xff0c;删除&#xff0c;共享文件。 登录界面 主窗口界面 文件…

Postman中参数区别及使用说明

一、Params与Body 二者区别在于请求参数在http协议中位置不一样。Params 它会将参数放入url中以&#xff1f;区分以&拼接Body则是将请求参数放在请求体中 后端接受数据: 二、body中不同格式 2.1 multipart/form-data key - value 格式输入&#xff0c;主要特点是可以上…

Proteus软件安装包分享(附安装教程)

目录 一、软件简介 二、软件下载 一、软件简介 Proteus软件是一款电路设计和仿真的综合性软件&#xff0c;由Labcenter公司开发。它提供了一个交互式的图形界面&#xff0c;用户可以在其中构建电路、仿真结果并实时观察仿真结果。 1、Proteus的历史和演变 Proteus软件最初于…

Matlab图像处理-加法运算

加法运算 图像加法运算的一个应用是将一幅图像的内容叠加到另一幅图像上&#xff0c;生成叠加图像效果&#xff0c;或给图像中每个像素叠加常数改变图像的亮度。 在MATLAB图像处理工具箱中提供的函数imadd()可实现两幅图像的相加或者一幅图像和常量的相加。 程序代码 I1 i…

C++学习记录——이십유 C++11(2)

文章目录 1、类的新功能1、移动构造和移动赋值2、default、delete 2、可变参数模板3、STL容器的emplace 1、类的新功能 1、移动构造和移动赋值 逐成员按字节拷贝就是浅拷贝。一个类中&#xff0c;如果达成默认移动构造的要求&#xff0c;那么传右值就会使用移动构造了&#xf…

022-从零搭建微服务-短信服务(二)

写在最前 如果这个项目让你有所收获&#xff0c;记得 Star 关注哦&#xff0c;这对我是非常不错的鼓励与支持。 源码地址&#xff08;后端&#xff09;&#xff1a;https://gitee.com/csps/mingyue 源码地址&#xff08;前端&#xff09;&#xff1a;https://gitee.com/csps…

Docker镜像的私有定制之nginx

一、背景 机器上已有nginx的可执行文件&#xff0c;但它是基于官方源码进行修改过的&#xff0c;可模块的源码一时找不到。另外&#xff0c;每次都基于源码去构建&#xff0c;对于Nginx部署也是麻烦。 所以&#xff0c;我们想要改为docker容器化部署nginx。 操作系统是centos…

STL-常用容器-map/ multimap容器(二叉树-红黑树)

1 map基本概念 简介&#xff1a; Map是一种关联容器&#xff0c;它通过将键和值成对存储&#xff0c;实现了快速的键值查找。在Map中&#xff0c;每个键都是唯一的&#xff0c;而值可以重复。Map容器内部使用平衡二叉树&#xff08;通常是红黑树&#xff09;的数据结构来实现高…

HodlSoftware-免费在线PDF工具箱 加解密PDF 集成隐私保护功能

HodlSoftware是什么 HodlSoftware是一款免费在线PDF工具箱&#xff0c;集合编辑 PDF 的简单功能&#xff0c;可以对PDF进行加解密、优化压缩PDF、PDF 合并、PDF旋转、PDF页面移除和分割PDF等操作&#xff0c;而且工具集成隐私保护功能&#xff0c;文件只在浏览器本地完成&…

OpenCV基础知识(8)— 图形检测

前言&#xff1a;Hello大家好&#xff0c;我是小哥谈。图形检测是计算机视觉的一项重要功能。通过图形检测可以分析图像中可能存在的形状&#xff0c;然后对这些形状进行描绘&#xff0c;例如搜索并绘制图像的边缘&#xff0c;定位图像的位置&#xff0c;判断图像中有没有直线、…