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

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响应式显示

效果

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

代码

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>

##注意"external nofollow" target="_blank" href="https://github.com/chengheai/daily-vue-demo/blob/master/src/pages/Side.vue">点击这里

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

标签:
Vue,监听,元素变化值

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“Vue 监听元素前后变化值实例”
暂无“Vue 监听元素前后变化值实例”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。