Vue 监听元素前后变化值实例

 更新时间:2020年07月29日 15:30:59   作者:兔走丶  
这篇文章主要介绍了Vue 监听元素前后变化值实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

我就废话不多说了,大家还是直接看代码吧~

export default {
 data() {
 return {
  item: ''
 }
 },
 watch: {
 item(now, before){
  let remove = before.filter(x => now.indexOf(x) == -1);
  let add = now.filter(x => before.indexOf(x) == -1);
  /* 显示字符串或数组元素的增加和减少 */
  console.log(add, remove);
 }
 }
}

补充知识:Vuejs+Element监听-window.resize-el-menu响应式显示

效果

代码

template

<template>
 <div class="sidebar">
 <!-- 折叠按钮 -->
 <div class="collapse-btn" @click="collapseChage">
 <i class="el-icon-d-arrow-left" v-show="!collapse" title="收起">
 &nbsp;&nbsp;
 <small>收缩侧边栏</small>
 </i>
 <i class="el-icon-d-arrow-right" v-show="collapse" title="展开"></i>
 </div>
 <el-menu
 class="sidebar-el-menu"
 :default-active="onRoutes"
 :collapse="collapse"
 text-color="#8d9199"
 active-text-color="#20a0ff"
 unique-opened
 router
 >
 <template v-for="item in items">
 <template v-if="item.subs">
  <el-submenu :index="item.index" :key="item.index">
  <template slot="title">
  <i :class="item.icon"></i>
  <span slot="title">{{ item.title }}</span>
  </template>
  <template v-for="subItem in item.subs">
  <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index">
  <template slot="title">
   <i :class="subItem.icon"></i>
   {{ subItem.title }}
  </template>
  <el-menu-item
   v-for="(threeItem,i) in subItem.subs"
   :key="i"
   :index="threeItem.index"
  >{{ threeItem.title }}</el-menu-item>
  </el-submenu>
  <el-menu-item v-else :index="subItem.index" :key="subItem.index">
  <i :class="subItem.icon"></i>
  {{ subItem.title }}
  </el-menu-item>
  </template>
  </el-submenu>
 </template>
 <template v-else>
  <el-menu-item :index="item.index" :key="item.index">
  <i :class="item.icon"></i>
  <span slot="title">{{ item.title }}</span>
  </el-menu-item>
 </template>
 </template>
 </el-menu>
 <div>
 <i class="el-icon-d-arrow-right" v-show="collapse" title="展开"></i>
 </div>
 </div>
</template>

javascript

<script>
import bus from "./bus";
import { menu } from "../../data/menu";

export default {
 data() {
 return {
 collapse: false,
 items: menu,
 screenWidth: 1000
 };
 },
 computed: {
 onRoutes() {
 return this.$route.path.replace("/", "");
 }
 },
 created() {
 // 通过 Event Bus 进行组件间通信,来折叠侧边栏
 bus.$on("collapse", msg => {
 this.collapse = msg;
 });
 },
 mounted() {
 // if (document.body.clientWidth < 1500) {
 // this.collapseChage();
 // }
 const that = this;
 window.addEventListener("resize", function() {
 return (() => {
 window.screenWidth = document.body.clientWidth;
 that.screenWidth = window.screenWidth;
 })();
 });
 },
 watch: {
 screenWidth(val) {
 if (!this.timer) {
 this.screenWidth = val;
 this.timer = true;
 let that = this;
 setTimeout(function() {
  // that.screenWidth = that.$store.state.canvasWidth
  console.log(that.screenWidth);
  that.auto();
  that.timer = false;
 }, 400);
 }
 }
 },
 methods: {
 // 侧边栏折叠
 collapseChage() {
 this.collapse = !this.collapse;
 bus.$emit("collapse", this.collapse);
 },
 auto() {
 if (this.screenWidth < 1200) {
 console.log("收起来");
 this.collapse = true;
 bus.$emit("collapse", true);
 } else {
 console.log("展开");
 this.collapse = false;
 bus.$emit("collapse", false);
 }
 }
 }
};
</script>

css

<style scoped>
.sidebar {
 z-index: 1024;
 display: block;
 position: fixed;
 left: 0;
 top: 70px;
 bottom: 0;
 overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
 width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
 width: 200px;
}
.sidebar > ul {
 height: 100%; /*写给不支持calc()的浏览器*/
 height: calc(100% - 52px);
 top: 30px;
 background-color: rgb(235, 239, 243);
 border-top: 1px solid #d6d6d6;
}
.sidebar > ul > li,
.sidebar > ul > li div {
 background-color: rgb(235, 239, 243);
}
.sidebar > ul > li > ul {
 background-color: rgb(235, 239, 243);
}
.el-menu {
 background-color: rgb(235, 239, 243);
}
i {
 margin-right: 10px;
}
.collapse-btn {
 height: 30px;
 width: 100%;
 cursor: pointer;
 line-height: 30px;
 position: absolute;
 top: 0;
 left: 0;
 background-color: #f4f6fa;
 color: #fff;
 text-align: center;
 overflow: hidden;
 box-sizing: border-box;
 box-shadow: 0 5px 10px #ddd;
}
.collapse-btn i {
 color: #8d9199;
 padding: 1px;
 cursor: pointer;
 overflow: hidden;
 text-overflow: ellipsis;
}
/* .collapse-btn:before{
 content: "";
 display: block;
 height: 0;
 border-top: 1px dotted #909399;
 position: absolute;
 left: 15px;
 right: 15px;
 top: 18px;
 } */
</style>

##注意⚠️

此开发框架是github 名为 lin-xin 的 vue-manage-system

因公司项目需要兼容iPad,故而修改

详细代码点击这里

以上这篇Vue 监听元素前后变化值实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • vue使用less报错:Inline JavaScript is not enabled问题

    vue使用less报错:Inline JavaScript is not ena

    这篇文章主要介绍了vue使用less报错:Inline JavaScript is not enabled问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • 对vue2.0中.vue文件页面跳转之.$router.push的用法详解

    对vue2.0中.vue文件页面跳转之.$router.push的用法详解

    今天小编就为大家分享一篇对vue2.0中.vue文件页面跳转之.$router.push的用法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • vue动态添加背景图简单示例

    vue动态添加背景图简单示例

    这篇文章主要给大家介绍了关于vue动态添加背景图的相关资料,在一些场景下我们需要使用户可以进行自定义背景图片,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • Vue.js自定义指令的基本使用详情

    Vue.js自定义指令的基本使用详情

    这篇文章主要介绍了Vue.js自定义指令的基本使用详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值需要的小伙伴可以参考一下
    2022-05-05
  • vue+iview+less 实现换肤功能

    vue+iview+less 实现换肤功能

    这篇文章主要介绍了vue+iview+less 实现换肤功能,项目搭建用的vue—cli,css框架选择的iview,具体操作流程大家跟随脚本之家小编一起看看吧
    2018-08-08
  • 在Vue3项目中使用Jest配置生成测试报告的示例详解

    在Vue3项目中使用Jest配置生成测试报告的示例详解

    在Vue 3项目中使用Jest进行单元测试是一种常见的做法,它可以帮助我们验证代码的正确性和稳定性,本文将介绍如何在Vue 3项目中配置Jest,以生成测试报告,需要的朋友可以参考下
    2023-09-09
  • Vue检测屏幕变化来改变不同的charts样式实例

    Vue检测屏幕变化来改变不同的charts样式实例

    这篇文章主要介绍了Vue检测屏幕变化来改变不同的charts样式实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • 前端vuex中dispatch的使用方法总结

    前端vuex中dispatch的使用方法总结

    这篇文章主要给大家介绍了关于前端vuex中dispatch使用方法的相关资料,vuex的dispatch方法用于触发一个action,以便更新state,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-04-04
  • 详解Vue-axios 设置请求头问题

    详解Vue-axios 设置请求头问题

    这篇文章主要介绍了Vue-axios 设置请求头问题,文中给大家提到了axios设置请求头内容的方法,需要的朋友可以参考下
    2018-12-12
  • Vue前端左侧菜单右侧内容的网站界面制作过程

    Vue前端左侧菜单右侧内容的网站界面制作过程

    这篇文章主要介绍了使用Vue和ElementUI制作一个带有左侧菜单和右侧内容区的网站页面的过程,文中通过CSS样式和深度作用符,实现了页面的美化和功能的完善,需要的朋友可以参考下
    2025-02-02

最新评论