vue实现侧边折叠菜单栏手风琴效果

简介: 该文章介绍了如何使用Vue.js实现具有手风琴效果的侧边折叠菜单栏,包括HTML结构设计、CSS样式设置以及JavaScript交互逻辑的编写。

在我们做管理后台亦或是产品流程时,总是需要一个菜单栏或者导航栏来架起我们的产品架构,那么,如何用vue实现侧边折叠导航栏呢?接下来将一一介绍。

                                   请先看效果演示图。

步骤1:先架构整个页面的布局,撰写html、css和js
HTML代码

<template>
    <div id="main">
        <div class="content">
            <!-- 左边导航栏 -->
            <div class="admin_nav col-lg-3 col-xl-2" v-show="fullWidth">
                <div class = "admin_nav_site">
                    <div class="title">
                        数据中心后台管理
                    </div>
                    <ul v-for="(item,index) in navList" :key="index">
                        <li @click="handleToTitle(index,item)" @mouseenter="changeTitleColor(index)" @mouseleave="removeTitleColor(index)"
                         ref="title" :class="{active:currentSort == index}" class="col-lg-12">
                            <div class="left_icon">
                                <i :class="'iconfont '+item.className"></i>
                            </div>
                            <div class="middle">
                                {
  {item.title}}
                            </div>
                            <div class="right_icon">
                                <i :class="'iconfont '+item.close"></i>
                            </div>
                        </li>
                        <li v-for="(it,idx) in item.secMenu" :key="idx" v-show="item.isSubShow" class="active1">
                            <router-link :to="it.path" tag="a">{
  {it.name}}</router-link>
                        </li>
                    </ul>
                </div>

            </div>
            <!-- 右边内容 -->
            <div class="admin_content col-12 col-md-12 col-lg-12 col-xl-10">
                <div class="admin_header">
                    <!-- 导航图标 -->
                    <div class="H5_item" v-show="!fullWidth">
                        <i class="iconfont icon-daohanglan" @click="make_menu" class-name="H5-menu"></i>
                    </div>
                    <div class="user">
                        <div class="userHead">
                            <span>
                                <img src="../../assets/image/userhead.svg">
                            </span>
                            <span>admin</span>
                        </div>
                        <div class="logOut">
                            <i class="iconfont icon-tuichu"></i>
                        </div>
                    </div>
                </div>
                <div class="admin_body">
                    <keep-alive>
                        <router-view />
                    </keep-alive>
                </div>
            </div>

        </div>


    </div>
</template>

JS代码

<script>
    import axios from 'axios';

    export default {
   
        name: 'navigation',
        data() {
   
            return {
   
                navList: [{
   
                        title: '用户管理',
                        isSubShow: false,
                        className: 'icon-guanliyuan1',
                        close: 'icon-close',
                        up: 'icon-zhedie',
                        secMenu: [
                            {
   
                            name: '工号管理',
                            path: '/home/navigation-one/user'
                            }, 
                            // {
   
                            // name: '角色管理',
                            // path: '/home/navigation-one/role'
                            // }, 
                            // {
   
                            // name: '权限管理',
                            // path: '/home/navigation-one/user'
                            // }, 
                        ]


                    },
                    {
   
                        title: '资源管理',
                        isSubShow: false,
                        className: 'icon-ziyuan',
                        close: 'icon-close',
                        up: 'icon-zhedie',
                        secMenu: [
                            {
   
                                name: '我的收藏',
                                path: '/home/navigation-one/user'
                            },
                            {
   
                                name: '我的项目',
                                path: '/home/navigation-one/role'
                            }
                        ]

                    },
                    {
   
                        title: '综合管理',
                        isSubShow: false,
                        className: 'icon-zonghe',
                        close: 'icon-close',
                        up: 'icon-zhedie',
                        secMenu: [{
   
                                name: '划配管理',
                                path: '/home/navigation-one/user'
                            },
                            {
   
                                name: '数据信息',
                                path: '/home/navigation-one/role'
                            }
                        ]

                    },
                    {
   
                        title: '系统消息',
                        isSubShow: false,
                        className: 'icon-xiaoxi',
                        close: 'icon-close',
                        up: 'icon-zhedie',
                        secMenu: [{
   
                                name: '实时动态',
                                path: '/home/navigation-one/user'
                            },
                            {
   
                                name: '过去动态',
                                path: '/home/navigation-one/role'
                            }
                        ]
                    }
                ],
                currentSort: 0,
                pagetype: "index",
                fullWidth: true,
                dismenu: true
            }
        },
        methods: {
   
            active(index) {
   
                this.currentSort = index;
            },
            changeTitleColor(index) {
   
                this.currentSort = index;
            },
            removeTitleColor(index) {
   
                this.currentSort = null;
            },
            handleToTitle(ind, item) {
   
                this.navList[ind].isSubShow = !this.navList[ind].isSubShow;
                if (this.navList[ind].isSubShow == true) {
   
                    this.navList[ind].close = 'icon-zhedie';
                } else {
   
                    this.navList[ind].close = 'icon-close';
                }
            }
        }

    }
</script>

CSS代码

    #main {
   
        background: #F0F6FF;
        font-size: 0.16rem;
    }

    #main .content {
   
        display: flex;
        flex-direction: row;
    }

    .content .admin_nav {
   
        min-height: 9.37rem;
        max-height: 1.3rem;
        padding: 0 !important;
        background: #364150;
        position: relative;

    }

    .content .admin_nav .admin_nav_site{
   
        height: 100vh;
    }

    .content .admin_nav .title {
   
        height: 1.39rem;
        line-height: 1.39rem;
        background: rgba(63, 73, 86, 1);
        color: #FFFFFF;
        text-align: center;
        font-size: 0.18rem;
        display: flex;
        justify-content: center;
    }

    .content .admin_nav .title div {
   
        padding: 0 0.03rem;
    }

    .content .admin_nav .title div:first-child i {
   
        font-size: 0.18rem;
    }

    .content .admin_nav ul .active {
   
        background: rgba(68, 81, 99, 1);
    }

    .content .admin_nav ul .active1 a.router-link-active {
   
        color: #fff;
    }

    .content .admin_nav ul li {
   
        height: 0.6rem;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        font-size: 0.18rem;
    }

    .content .admin_nav ul li a {
   
        color: #cecdc5;
        cursor: pointer;
    }

    .content .admin_nav ul li:first-child {
   
        font-size: 0.2rem;
        height: 0.64rem;
        display: flex;
        flex-direction: row;
        align-items: center;
        color: #BEC5C0;
        cursor: pointer;
    }

    .content .admin_nav ul li:first-child div {
   
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #BEC5C0;
    }

    .content .admin_nav ul li:first-child .left_icon {
   
        width: 22%;
    }

    .content .admin_nav ul li:first-child .left_icon i {
   
        color: #1296DB;
    }

    .content .admin_nav ul li:first-child .middle {
   
        width: 56%;
        display: flex;
        justify-content: flex-start;
    }

    .content .admin_nav ul li:first-child .right_icon {
   
        width: 22%;
    }

    .admin_nav .admin .middle:hover {
   
        color: #fff;
    }

    .admin_nav .admin .right_icon i:hover {
   
        color: #fff;
    }

    .admin_nav .admin .middle.router-link-active {
   
        color: #fff;
    }

    .content .admin_nav_position {
   
        position: absolute;
        z-index: 3000;
    }

    .content .admin_content {
   
        padding: 0 !important;
    }

    .content .admin_content .admin_header {
   
        width: 100%;
        height: 0.6rem;
        background: #364150;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
    }

    .content .admin_content .admin_header .H5_item {
   
        width: 0.4rem;
        height: 0.4rem;
        border-radius: 0.2rem;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-left: 0.2rem;
    }

    .content .admin_content .admin_header .H5_item i {
   
        font-size: 0.22rem;
        color: #fff;
    }

    .content .admin_content .admin_header .user {
   
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: flex-end;
    }

    .content .admin_content .admin_header .user .userHead {
   
        height: 0.6rem;
        display: flex;
        flex-direction: row;
        align-items: center;
        color: #fff;
        font-size: 0.18rem;
    }

    .content .admin_content .admin_header .user .userHead span {
   
        padding: 0 0.05rem;
    }

    .content .admin_content .admin_header .user .userHead span:nth-child(1) {
   
        position: relative;
    }

    .content .admin_content .admin_header .user .userHead span img {
   
        width: 0.4rem;
        height: 0.4rem;
        border-radius: 50%;
    }

    .content .admin_content .admin_header .user .userHead span input {
   
        width: 0.4rem;
        height: 0.4rem;
        position: absolute;
        overflow: hidden;
        opacity: 0;
        cursor: pointer;
    }

    .content .admin_content .admin_header .user .logOut {
   
        color: #fff;
        margin-left: 0.12rem;
        margin-right: 0.5rem;
    }

    .content .admin_content .admin_header .user .logOut i {
   
        color: #1296DB;
        font-size: 0.24rem;
        cursor: pointer;
    }

    .toggle-cart-enter-active {
   
        transition: all 0.4s linear;
    }

    .toggle-cart-leave-active {
   
        transition: all 0.4s linear;
    }

    .toggle-cart-enter {
   
        transform: translateX(-200%);
    }

    .toggle-cart-leave-active {
   
        transform: translateX(-200%);
    }

步骤2:撰写完基本网页布局和样式后,对部分细节进行剖析。

1、动态绑定路径

<router-link :to="it.path" tag="a">{
   {
   it.name}}</router-link>

2、一级菜单,标题及折叠符号

①通过@mouseenter和@mouseleave事件,当鼠标移入和移出时,改变标题区域背景颜色

②通过@click事件,当鼠标点击时,显示二级菜单列表

③利用:class动态绑定类名,触发active方法,修改折叠符号状态;绑定图标类名,通过遍历,修改对应图标

<ul v-for="(item,index) in navList" :key="index">
    <li @click="handleToTitle(index,item)" @mouseenter="changeTitleColor(index)" @mouseleave="removeTitleColor(index)" ref="title" :class="{active:currentSort == index}" class="col-lg-12">
        <div class="left_icon">
            <i :class="'iconfont '+item.className"></i>
        </div>
        <div class="middle">
            {
  {item.title}}
        </div>
        <div class="right_icon">
            <i :class="'iconfont '+item.close"></i>
        </div>
    </li>
    <li v-for="(it,idx) in item.secMenu" :key="idx" v-show="item.isSubShow" class="active1">
        <router-link :to="it.path" tag="a">{
  {it.name}}</router-link>
    </li>
</ul>
active(index) {
   
    this.currentSort = index;
},

changeTitleColor(index) {
   
    this.currentSort = index;
},

removeTitleColor(index) {
   
    this.currentSort = null;
},

handleToTitle(ind, item) {
   
    this.navList[ind].isSubShow = !this.navList[ind].isSubShow;
    if (this.navList[ind].isSubShow == true) {
   
        this.navList[ind].close = 'icon-zhedie';
    } else {
   
        this.navList[ind].close = 'icon-close';
    }
}

步骤3:菜单栏完成后,在网页端能进行查看,但是还需做一个手机端适配,这样就达到两端同步适应的效果。实现如下:

①对左边导航栏再撰写第二套代码,并绑定好对应的参数实现显示和隐藏

②运用vue已有的transition,绑定name值,实现动画效果绑定

HTML代码

<!-- 移动端导航栏 -->
<transition name="toggle-cart">
    <div class="admin_nav admin_nav_position col-8 col-md-5 col-lg-3 col-xl-3" v-show="!fullWidth&&!dismenu">
        <div class="title">
            <div v-show="!fullWidth">
                <i class="iconfont icon-daohanglan" @click="make_menu" class-name="H5-menu"></i>
            </div>
            <div>数据中心后台管理</div>
        </div>
        <ul v-for="(item,index) in navList" :key="index">
            <li @click="handleToTitle(index,item)" @mouseenter="changeTitleColor(index)" @mouseleave="removeTitleColor(index)" ref="title" :class="{active:currentSort == index}">
                <div class="left_icon">
                    <i :class="'iconfont '+item.className"></i>
                </div>
                <div class="middle">
                    {
  {item.title}}
                </div>
                <div class="right_icon">
                    <i :class="'iconfont '+item.close"></i>
                </div>
            </li>
            <li v-for="(it,idx) in item.secMenu" :key="idx" v-show="item.isSubShow" class="active1">
                <router-link :to="it.path" tag="a">{
  {it.name}}</router-link>
            </li>
        </ul>
    </div>
</transition>

JS代码

mounted() {
   
    window.onresize = () => {
    //监听屏幕变化
        this.page_width();
    };
    this.page_width();
},
methods: {
   
    make_menu() {
    //点击导航图标
        this.dismenu = !this.dismenu;
    },
    page_width() {
    //获取屏幕宽度
        var screenWidth = window.screen.width;
        if (screenWidth <= 1024) {
   
            this.fullWidth = false;
        } else {
   
            this.fullWidth = true;
        }
    }
}

最后来看下手机端效果:

附上全文代码:

<template>
    <div id="main">
        <div class="content">
            <!-- 左边导航栏 -->
            <div class="admin_nav col-lg-3 col-xl-2" v-show="fullWidth">
                <div class = "admin_nav_site">
                    <div class="title">
                        数据中心后台管理
                    </div>
                    <ul v-for="(item,index) in navList" :key="index">
                        <li @click="handleToTitle(index,item)" @mouseenter="changeTitleColor(index)" @mouseleave="removeTitleColor(index)"         
                        ref="title" :class="{active:currentSort == index}" class="col-lg-12">
                            <div class="left_icon">
                                <i :class="'iconfont '+item.className"></i>
                            </div>
                            <div class="middle">
                                {
   {
   item.title}}
                            </div>
                            <div class="right_icon">
                                <i :class="'iconfont '+item.close"></i>
                            </div>
                        </li>
                        <li v-for="(it,idx) in item.secMenu" :key="idx" v-show="item.isSubShow" class="active1">
                            <router-link :to="it.path" tag="a">{
   {
   it.name}}</router-link>
                        </li>
                    </ul>
                </div>

                <!-- 管理员端 -->
                <!-- <ul class="admin">
                    <li class="col-lg-12">
                        <div class="left_icon">
                            <i class="iconfont icon-guanliyuan1"></i>
                        </div>
                        <router-link to="/admin/administrator/secadmin" tag="div" class="middle">管理员端</router-link>
                        <div class="right_icon">
                            <i class="iconfont icon-jinru"></i>
                        </div>
                    </li>
                </ul> -->
            </div>
            <!-- 右边内容 -->
            <div class="admin_content col-12 col-md-12 col-lg-12 col-xl-10">
                <div class="admin_header">
                    <!-- 导航图标 -->
                    <div class="H5_item" v-show="!fullWidth">
                        <i class="iconfont icon-daohanglan" @click="make_menu" class-name="H5-menu"></i>
                    </div>
                    <div class="user">
                        <div class="userHead">
                            <span>
                                <img src="../../assets/image/userhead.svg">
                            </span>
                            <span>admin</span>
                        </div>
                        <div class="logOut">
                            <i class="iconfont icon-tuichu"></i>
                        </div>
                    </div>
                </div>
                <div class="admin_body">
                    <keep-alive>
                        <router-view />
                    </keep-alive>
                </div>
            </div>
            <!-- 移动端导航栏 -->
            <transition name="toggle-cart">
                <div class="admin_nav admin_nav_position col-8 col-md-5 col-lg-3 col-xl-3" v-show="!fullWidth&&!dismenu">
                    <div class="title">
                        <div v-show="!fullWidth">
                            <i class="iconfont icon-daohanglan" @click="make_menu" class-name="H5-menu"></i>
                        </div>
                        <div>数据中心后台管理</div>
                    </div>
                    <ul v-for="(item,index) in navList" :key="index">
                        <li @click="handleToTitle(index,item)" @mouseenter="changeTitleColor(index)" @mouseleave="removeTitleColor(index)"
                         ref="title" :class="{active:currentSort == index}">
                            <div class="left_icon">
                                <i :class="'iconfont '+item.className"></i>
                            </div>
                            <div class="middle">
                                {
   {
   item.title}}
                            </div>
                            <div class="right_icon">
                                <i :class="'iconfont '+item.close"></i>
                            </div>
                        </li>
                        <li v-for="(it,idx) in item.secMenu" :key="idx" v-show="item.isSubShow" class="active1">
                            <router-link :to="it.path" tag="a">{
   {
   it.name}}</router-link>
                        </li>
                    </ul>
                </div>
            </transition>
        </div>
    </div>
</template>

<script>
    import axios from 'axios';

    export default {
   
        name: 'navigation-1',
        data() {
   
            return {
   
                token: '',
                navList: [{
   
                        title: '用户管理',
                        isSubShow: false,
                        className: 'icon-guanliyuan1',
                        close: 'icon-close',
                        up: 'icon-zhedie',
                        secMenu: [
                            {
   
                            name: '工号管理',
                            path: '/home/navigation-one/user'
                            }, 
                            // {
   
                            // name: '角色管理',
                            // path: '/home/navigation-one/role'
                            // }, 
                            // {
   
                            // name: '权限管理',
                            // path: '/home/navigation-one/user'
                            // }, 
                        ]


                    },
                    {
   
                        title: '资源管理',
                        isSubShow: false,
                        className: 'icon-ziyuan',
                        close: 'icon-close',
                        up: 'icon-zhedie',
                        secMenu: [
                            {
   
                                name: '我的收藏',
                                path: '/home/navigation-one/user'
                            },
                            {
   
                                name: '我的项目',
                                path: '/home/navigation-one/role'
                            }
                        ]

                    },
                    {
   
                        title: '综合管理',
                        isSubShow: false,
                        className: 'icon-zonghe',
                        close: 'icon-close',
                        up: 'icon-zhedie',
                        secMenu: [{
   
                                name: '划配管理',
                                path: '/home/navigation-one/user'
                            },
                            {
   
                                name: '数据信息',
                                path: '/home/navigation-one/role'
                            }
                        ]

                    },
                    {
   
                        title: '系统消息',
                        isSubShow: false,
                        className: 'icon-xiaoxi',
                        close: 'icon-close',
                        up: 'icon-zhedie',
                        secMenu: [{
   
                                name: '实时动态',
                                path: '/home/navigation-one/user'
                            },
                            {
   
                                name: '过去动态',
                                path: '/home/navigation-one/role'
                            }
                        ]
                    }
                ],
                currentSort: 0,
                pagetype: "index",
                fullWidth: true,
                dismenu: true
            }
        },
        mounted() {
   
            window.onresize = () => {
    //监听屏幕变化
                this.page_width();
            };
            this.page_width();
        },
        methods: {
   
            active(index) {
   
                this.currentSort = index;
            },
            changeTitleColor(index) {
   
                this.currentSort = index;
            },
            removeTitleColor(index) {
   
                this.currentSort = null;
            },
            handleToTitle(ind, item) {
   
                this.navList[ind].isSubShow = !this.navList[ind].isSubShow;
                if (this.navList[ind].isSubShow == true) {
   
                    this.navList[ind].close = 'icon-zhedie';
                } else {
   
                    this.navList[ind].close = 'icon-close';
                }
            },
            make_menu() {
    //点击导航图标
                this.dismenu = !this.dismenu;
            },
            page_width() {
    //获取屏幕宽度
                var screenWidth = window.screen.width;
                if (screenWidth <= 1024) {
   
                    this.fullWidth = false;
                } else {
   
                    this.fullWidth = true;
                }
            }
        }

    }
</script>

<style scoped>
    #main {
   
        background: #F0F6FF;
        font-size: 0.16rem;
    }

    #main .content {
   
        display: flex;
        flex-direction: row;
    }

    .content .admin_nav {
   
        min-height: 9.37rem;
        max-height: 1.3rem;
        padding: 0 !important;
        background: #364150;
        position: relative;

    }

    .content .admin_nav .admin_nav_site{
   
        height: 100vh;
    }

    .content .admin_nav .title {
   
        height: 1.39rem;
        line-height: 1.39rem;
        background: rgba(63, 73, 86, 1);
        color: #FFFFFF;
        text-align: center;
        font-size: 0.18rem;
        display: flex;
        justify-content: center;
    }

    .content .admin_nav .title div {
   
        padding: 0 0.03rem;
    }

    .content .admin_nav .title div:first-child i {
   
        font-size: 0.18rem;
    }

    .content .admin_nav ul .active {
   
        background: rgba(68, 81, 99, 1);
    }

    .content .admin_nav ul .active1 a.router-link-active {
   
        color: #fff;
    }

    .content .admin_nav ul li {
   
        height: 0.6rem;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        font-size: 0.18rem;
    }

    .content .admin_nav ul li a {
   
        color: #cecdc5;
        cursor: pointer;
    }

    .content .admin_nav ul li:first-child {
   
        font-size: 0.2rem;
        height: 0.64rem;
        display: flex;
        flex-direction: row;
        align-items: center;
        color: #BEC5C0;
        cursor: pointer;
    }

    .content .admin_nav ul li:first-child div {
   
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #BEC5C0;
    }

    .content .admin_nav ul li:first-child .left_icon {
   
        width: 22%;
    }

    .content .admin_nav ul li:first-child .left_icon i {
   
        color: #1296DB;
    }

    .content .admin_nav ul li:first-child .middle {
   
        width: 56%;
        display: flex;
        justify-content: flex-start;
    }

    .content .admin_nav ul li:first-child .right_icon {
   
        width: 22%;
    }

    .admin_nav .admin .middle:hover {
   
        color: #fff;
    }

    .admin_nav .admin .right_icon i:hover {
   
        color: #fff;
    }

    .admin_nav .admin .middle.router-link-active {
   
        color: #fff;
    }

    .content .admin_nav_position {
   
        position: absolute;
        z-index: 3000;
    }

    .content .admin_content {
   
        padding: 0 !important;
    }

    .content .admin_content .admin_header {
   
        width: 100%;
        height: 0.6rem;
        background: #364150;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
    }

    .content .admin_content .admin_header .H5_item {
   
        width: 0.4rem;
        height: 0.4rem;
        border-radius: 0.2rem;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-left: 0.2rem;
    }

    .content .admin_content .admin_header .H5_item i {
   
        font-size: 0.22rem;
        color: #fff;
    }

    .content .admin_content .admin_header .user {
   
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: flex-end;
    }

    .content .admin_content .admin_header .user .userHead {
   
        height: 0.6rem;
        display: flex;
        flex-direction: row;
        align-items: center;
        color: #fff;
        font-size: 0.18rem;
    }

    .content .admin_content .admin_header .user .userHead span {
   
        padding: 0 0.05rem;
    }

    .content .admin_content .admin_header .user .userHead span:nth-child(1) {
   
        position: relative;
    }

    .content .admin_content .admin_header .user .userHead span img {
   
        width: 0.4rem;
        height: 0.4rem;
        border-radius: 50%;
    }

    .content .admin_content .admin_header .user .userHead span input {
   
        width: 0.4rem;
        height: 0.4rem;
        position: absolute;
        overflow: hidden;
        opacity: 0;
        cursor: pointer;
    }

    .content .admin_content .admin_header .user .logOut {
   
        color: #fff;
        margin-left: 0.12rem;
        margin-right: 0.5rem;
    }

    .content .admin_content .admin_header .user .logOut i {
   
        color: #1296DB;
        font-size: 0.24rem;
        cursor: pointer;
    }

    .toggle-cart-enter-active {
   
        transition: all 0.4s linear;
    }

    .toggle-cart-leave-active {
   
        transition: all 0.4s linear;
    }

    .toggle-cart-enter {
   
        transform: translateX(-200%);
    }

    .toggle-cart-leave-active {
   
        transform: translateX(-200%);
    }
</style>

注:

1、css样式中的rem换算成px为对应的rem值*100,如:font-size:0.24rem;=> font-size:24px;

2、文章中涉及到的iconfont类名是因为引用了阿里图标,如遇到不知道如何引用的可以翻阅博文→ [https://developer.aliyun.com/article/1613119)

3、关于router-link对应的跳转,如有小伙伴不太清楚如何使用vue-router配置路由的可以翻阅博文→[https://developer.aliyun.com/article/1613120)

Ending(〃'▽'〃)!

相关文章
|
6天前
|
JavaScript 前端开发
如何在 Vue 项目中配置 Tree Shaking?
通过以上针对 Webpack 或 Rollup 的配置方法,就可以在 Vue 项目中有效地启用 Tree Shaking,从而优化项目的打包体积,提高项目的性能和加载速度。在实际配置过程中,需要根据项目的具体情况和需求,对配置进行适当的调整和优化。
|
7天前
|
存储 缓存 JavaScript
在 Vue 中使用 computed 和 watch 时,性能问题探讨
本文探讨了在 Vue.js 中使用 computed 计算属性和 watch 监听器时可能遇到的性能问题,并提供了优化建议,帮助开发者提高应用性能。
|
7天前
|
存储 缓存 JavaScript
如何在大型 Vue 应用中有效地管理计算属性和侦听器
在大型 Vue 应用中,合理管理计算属性和侦听器是优化性能和维护性的关键。本文介绍了如何通过模块化、状态管理和避免冗余计算等方法,有效提升应用的响应性和可维护性。
|
7天前
|
存储 缓存 JavaScript
Vue 中 computed 和 watch 的差异
Vue 中的 `computed` 和 `watch` 都用于处理数据变化,但使用场景不同。`computed` 用于计算属性,依赖于其他数据自动更新;`watch` 用于监听数据变化,执行异步或复杂操作。
|
6天前
|
JavaScript 前端开发 UED
vue学习第二章
欢迎来到我的博客!我是一名自学了2年半前端的大一学生,熟悉JavaScript与Vue,目前正在向全栈方向发展。如果你从我的博客中有所收获,欢迎关注我,我将持续更新更多优质文章。你的支持是我最大的动力!🎉🎉🎉
|
8天前
|
存储 JavaScript 开发者
Vue 组件间通信的最佳实践
本文总结了 Vue.js 中组件间通信的多种方法,包括 props、事件、Vuex 状态管理等,帮助开发者选择最适合项目需求的通信方式,提高开发效率和代码可维护性。
|
6天前
|
JavaScript 前端开发 开发者
vue学习第一章
欢迎来到我的博客!我是瑞雨溪,一名热爱JavaScript和Vue的大一学生。自学前端2年半,熟悉JavaScript与Vue,正向全栈方向发展。博客内容涵盖Vue基础、列表展示及计数器案例等,希望能对你有所帮助。关注我,持续更新中!🎉🎉🎉
|
20天前
|
数据采集 监控 JavaScript
在 Vue 项目中使用预渲染技术
【10月更文挑战第23天】在 Vue 项目中使用预渲染技术是提升 SEO 效果的有效途径之一。通过选择合适的预渲染工具,正确配置和运行预渲染操作,结合其他 SEO 策略,可以实现更好的搜索引擎优化效果。同时,需要不断地监控和优化预渲染效果,以适应不断变化的搜索引擎环境和用户需求。
|
8天前
|
存储 JavaScript
Vue 组件间如何通信
Vue组件间通信是指在Vue应用中,不同组件之间传递数据和事件的方法。常用的方式有:props、自定义事件、$emit、$attrs、$refs、provide/inject、Vuex等。掌握这些方法可以实现父子组件、兄弟组件及跨级组件间的高效通信。
|
13天前
|
JavaScript
Vue基础知识总结 4:vue组件化开发
Vue基础知识总结 4:vue组件化开发