2023/6/19 (vue实战项目,解决左侧导航栏不显示,优化,filter函数、Less)

1、解决vue2+element-ui 的左侧导航栏不显示问题

方法:将全局引入改成按需引入

(1)npm install babel-plugin-component -D

(2)然后,在工程目录下找到“.babelrc”名的文件:

          更改内容为

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset',
    ["@babel/preset-env", { "modules": false }]
  ],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

    添加的内容是:


    ["@babel/preset-env", { "modules": false }]
  ],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]

(3)全局引入删除

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
 
Vue.use(ElementUI);

(4)修改后重启项目

import Vue from 'vue';
import {
  Pagination,Dialog,Autocomplete,Dropdown,DropdownMenu,DropdownItem,
  Menu,Submenu,MenuItem,MenuItemGroup,Input,InputNumber,
  Radio,RadioGroup,RadioButton,Checkbox,CheckboxButton,CheckboxGroup,
  Switch,Select,Option,OptionGroup,Button,ButtonGroup,
  Table,TableColumn,DatePicker,TimeSelect,TimePicker,Popover,
  Tooltip,Breadcrumb,BreadcrumbItem,Form,FormItem,Tabs,
  TabPane,Tag,Tree,Alert,Slider,Icon,
  Row,Col,Upload,Progress,Spinner,Badge,
  Card,Rate,Steps,Step,Carousel,CarouselItem,
  Collapse,CollapseItem,Cascader,ColorPicker,Transfer,
  Container,Header,Aside,Main,Footer,Timeline,
  TimelineItem,Link,Divider,Image,Calendar,
  Backtop,PageHeader,CascaderPanel,Loading,MessageBox,Message,Notification
} from 'element-ui';
 
Vue.use(Pagination);Vue.use(Dialog);Vue.use(Autocomplete);Vue.use(Dropdown);Vue.use(DropdownMenu);
Vue.use(DropdownItem);Vue.use(Menu);Vue.use(Submenu);Vue.use(MenuItem);Vue.use(MenuItemGroup);
Vue.use(Input);Vue.use(InputNumber);Vue.use(Radio);Vue.use(RadioGroup);Vue.use(RadioButton);
Vue.use(Checkbox);Vue.use(CheckboxButton);Vue.use(CheckboxGroup);Vue.use(Switch);Vue.use(Select);
Vue.use(Option);Vue.use(OptionGroup);Vue.use(Button);Vue.use(ButtonGroup);Vue.use(Table);Vue.use(TableColumn);
Vue.use(DatePicker);Vue.use(TimeSelect);Vue.use(TimePicker);Vue.use(Popover);Vue.use(Tooltip);Vue.use(Breadcrumb);
Vue.use(BreadcrumbItem);Vue.use(Form);Vue.use(FormItem);Vue.use(Tabs);Vue.use(TabPane);Vue.use(Tag);
Vue.use(Tree);Vue.use(Alert);Vue.use(Slider);Vue.use(Icon);Vue.use(Row);Vue.use(Col);
Vue.use(Upload);Vue.use(Progress);Vue.use(Spinner);Vue.use(Badge);Vue.use(Card);
Vue.use(Rate);Vue.use(Steps);Vue.use(Step);Vue.use(Carousel);Vue.use(CarouselItem);Vue.use(Collapse);
Vue.use(CollapseItem);Vue.use(Cascader);Vue.use(ColorPicker);Vue.use(Transfer);Vue.use(Container);
Vue.use(Header);Vue.use(Aside);Vue.use(Main);Vue.use(Footer);Vue.use(Timeline);
Vue.use(TimelineItem);Vue.use(Link);Vue.use(Divider);Vue.use(Image);Vue.use(Calendar);
Vue.use(Backtop);Vue.use(PageHeader);Vue.use(CascaderPanel);Vue.use(Loading.directive);
 
Vue.prototype.$loading = Loading.service;
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$notify = Notification;
Vue.prototype.$message = Message;

2、优化

(1)

:collapse="isCollapse" //是否展开子菜单
background-color="#545c64"//背景颜色
 text-color="#fff"         //字体颜色
active-text-color="#ffd04b"//选中字体颜色


 <el-menu-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>



(2)filter()函数:

        一、作用

                filter用于对数组进行过滤。
                它创建一个新数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

                注 意:filter()不会对空数组进行检测、不会改变原始数组

        二、语法

        Array.filter(function(currentValue, indedx, arr), thisValue)
  其中,函数 function 为必须,数组中的每个元素都会执行这个函数。且如果返回值为 true,则该元素被保留;
  函数的第一个参数 currentValue 也为必须,代表当前元素的值。

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
 
let res = nums.filter((num) => {
  return num > 5;
});
 
console.log(res);  // [6, 7, 8, 9, 10]
    computed: {
        //没有子菜单
        noChildren() {
            return this.menuData.filter(item => !item.children)
        },
        //有子菜单
        hasChildren() {
            return this.menuData.filter(item => item.children)
        }
    }

(3)Less: Less 快速入门 | Less.js 中文文档 - Less 中文网 (bootcss.com)

npm i less@4.1.2 less-loader@5.0.0

如果less版本报错:

less 查找版本 npm官网搜索Less    有4.1.2    再次下载

npm i less@4.1.2
npm i less-loader@5.0.0

发现5.0的版本报错,搜索Less-loader(不能一次下载最新,否则报错)

npm i less-loader@6.0.0

下载完成                        

<style lang="less" scpoe>      
.el-menu {
    height: 100vh;
    h3 {
        color: #fff;
    }
</style> 

(4)App.vue 添加

<style lang="less" scpoed> //清除默认样式
html,body {
  margin:0;
  padding:0
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design Vue 的侧边导航栏隐藏可以通过 `Layout` 和 `Menu` 组件以及一些 CSS 样式实现。以下是一个简单的示例: ```vue <template> <a-layout :style="{ minHeight: '100vh' }"> <a-layout-sider :style="{ display: collapsed ? 'none' : 'block' }"> <a-menu :default-selected-keys="['1']" mode="inline"> <a-menu-item key="1"> <a-icon type="user" /> <span slot="title">用户管理</span> </a-menu-item> <a-menu-item key="2"> <a-icon type="team" /> <span slot="title">团队管理</span> </a-menu-item> </a-menu> </a-layout-sider> <a-layout-content> <a-icon class="trigger" type="menu-fold" v-show="collapsed" @click="collapsed = false" /> <a-icon class="trigger" type="menu-unfold" v-show="!collapsed" @click="collapsed = true" /> Content </a-layout-content> </a-layout> </template> <script> export default { data() { return { collapsed: false, }; }, }; </script> <style> .trigger { font-size: 18px; line-height: 64px; padding: 0 24px; cursor: pointer; transition: color 0.3s; } .trigger:hover { color: #1890ff; } </style> ``` 在这个示例中,我们使用了 `Layout` 和 `Menu` 组件来实现侧边导航栏。`Layout` 组件用于布局,`LayoutSider` 组件用于设置侧边栏的位置和宽度。通过 `:style="{ display: collapsed ? 'none' : 'block' }"` 控制侧边栏显示和隐藏。其中,`:style` 是一个动态绑定的样式对象,根据 `collapsed` 的值来设置侧边栏显示和隐藏。通过两个 `a-icon` 组件来切换侧边栏显示和隐藏,通过 `v-show` 控制两个组件的显示和隐藏。 您可以根据自己的需求对侧边导航栏进行进一步的定制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值