<div class="fullstock" :class="{ full: flag5 }"> <stock class="stock" ref="stock2"></stock> <div class="iconfont icon-expand-alt icon" @click="fullscreen5" v-if="!flag5" ></div> <div class="iconfont icon-compress-alt icon" @click="smallscreen5" v-else ></div>data() { return { flag1: false, flag2: false, flag3: false, flag4: false, flag5: false, flag6: false, }; }, components: { hot, seller, trend, Map, rank, stock, }, methods: { changetheme() { this.$store.commit("tabtheme"); }, fullscreen1() { this.flag = true; this.$nextTick(() => { this.$refs.seller2.changesize(); }); }, fullscreen2() { this.flag1 = true; this.$nextTick(() => { this.$refs.trend2.screenAdapter(); }); }, fullscreen3() { this.flag2 = true; this.$nextTick(() => { this.$refs.rank2.screenAdapter(); }); }, fullscreen4() { this.flag3 = true; this.$nextTick(() => { this.$refs.hot2.screenAdapter(); }); }, fullscreen5() { this.flag4 = true; this.$nextTick(() => { this.$refs.stock2.screenAdapter(); }); }, fullscreen6() { this.flag5 = true; this.$nextTick(() => { this.$refs.map2.screenAdapter(); }); },优化后代码
时间: 2024-03-26 21:37:02 浏览: 61
<div class="top">我的网页设计作业</div>
<div class="fullstock" :class="{ full: flag5 }">
<stock class="stock" ref="stock2"></stock>
<div class="iconfont icon-expand-alt icon" @click="fullscreen5" v-if="!flag5"></div>
<div class="iconfont icon-compress-alt icon" @click="smallscreen5" v-else></div>
data() {
return {
flags: [false, false, false, false, false, false],
};
},
components: {
hot,
seller,
trend,
Map,
rank,
stock,
},
methods: {
changetheme() {
this.$store.commit("tabtheme");
},
fullscreen(index) {
this.flags[index] = true;
this.$nextTick(() => {
switch (index) {
case 0:
this.$refs.seller2.changesize();
break;
case 1:
this.$refs.trend2.screenAdapter();
break;
case 2:
this.$refs.rank2.screenAdapter();
break;
case 3:
this.$refs.hot2.screenAdapter();
break;
case 4:
this.$refs.stock2.screenAdapter();
break;
case 5:
this.$refs.map2.screenAdapter();
break;
default:
break;
}
});
},
smallscreen(index) {
this.flags[index] = false;
},
},
修改后的代码将六个全屏方法合并成了一个方法,通过传入不同的 index 参数来判断要全屏的组件,并且使用了一个 flags 数组来表示每个组件是否全屏。这样可以减少代码重复,并且更加方便维护。
阅读全文