<template> <div> <router-view /> <div class="title">购物车</div> <div class="shop_goods" v-for="(item, index) in shopcarList" :key="index"> <van-checkbox v-model="item.isChecked"></van-checkbox> <van-card :title="item.name" :thumb="item.img"> <template #price> <span>¥{{ item.price }}</span> </template> <template #num> <p class="count"> <span @click="del(index)">-</span> <span>{{ item.num }}</span> <span @click="add(index)">+</span> </p> </template> </van-card> </div> <div class="shopcar_total"> <van-submit-bar :price="priceTotal" @submit="onSubmit"> <template #button> <span class="total"> 去结算 </span> </template> <van-checkbox v-model="isAllChecked">全选</van-checkbox> </van-submit-bar> </div> </div> </template> <script> export default { data() { return { stepper: 1, shopcarList: [], }; }, methods: { onSubmit() { console.log(111); }, add(index) { // console.log(index); this.$set( this.shopcarList[index], this.shopcarList[index].num, this.shopcarList[index].num++ ); }, del(index) { if (this.shopcarList[index].num == 1) { this.$toast("不能再减了"); } else { this.$set( this.shopcarList[index], this.shopcarList[index].num, this.shopcarList[index].num-- ); } }, }, created() { this.shopcarList = JSON.parse(localStorage.getItem("shopcar")) || []; console.log(this.shopcarList); }, computed: { isAllChecked: { set(val) { this.shopcarList.forEach((ele) => { ele.isChecked = val; }); }, get() { return this.shopcarList.every((ele) => { return ele.isChecked == true; }); }, }, priceTotal() { let sum = 0; let newPrice = this.shopcarList.filter((ele) => { return ele.isChecked == true; }); sum = newPrice.reduce((pre, current) => { return (pre += current.price * current.num); }, 0); return sum * 100; }, }, }; </script>这个页面怎么优化
时间: 2024-02-16 09:27:49 浏览: 111
这个页面可以优化的地方有:
1. 使用vue的keep-alive标签,对于需要频繁切换的组件进行缓存,减少重复渲染。
2. 使用v-show代替v-if,因为v-if每次都会销毁和重新创建DOM元素,而v-show只是控制元素的显示和隐藏。
3. 对于频繁使用的方法,可以将其提取为公共方法,避免重复代码。
4. 对于localStorage的操作,可以将其封装为一个单独的模块,并进行数据的有效性验证,避免出现数据异常。
5. 对于复杂的计算属性,可以使用缓存机制,避免重复计算。
6. 对于数组的修改,可以使用Vue提供的set方法,确保数据的响应式更新。
7. 可以考虑使用分页或懒加载等技术,对于大量数据进行分批加载,减轻页面的渲染压力。
8. 对于一些不必要的DOM元素,可以使用v-if或v-show进行控制,避免无意义的渲染。
9. 对于一些常用的组件,可以考虑进行全局注册或按需加载,避免重复引入和渲染。
相关问题
<template> <img alt="Vue logo" src="./assets/logo.png"> <router-link to="/user">用户</router-link><br> <div> <MainPage></MainPage> </div> <div> <router-view></router-view> </div>
这是一个Vue组件的模板代码。代码中包含了一个带有`to="/user"`属性的`<router-link>`,用于导航到"/user"路径。下面还有两个`<div>`块,分别包含了`<MainPage>`组件和`<router-view>`组件。
`<MainPage>`组件会在这个位置渲染,而`<router-view>`会根据路由配置动态渲染对应的组件。
注意,这是一个模板代码,它需要在Vue组件中使用。确保在正确的位置使用这段代码,并且已经正确安装了Vue Router插件。
<template> <div class="container"> <!-- 第一题#Vue路由 --> <nav> <ul> <li> <router-link to="/function">奖学金评选资格功能</router-link> </li> <li> <router-link to="/instruction">奖学金评选资格功能说明</router-link> </li> </ul> </nav> <router-view></router-view> </div> </template>
这段代码是一个 Vue 组件的模板,它包含了一个容器 div,其中包含一个 nav 元素和一个 router-view 元素。nav 元素中包含一个无序列表,其中有两个列表项,每个列表项都是一个 router-link 组件,分别指向路径为 /function 和 /instruction 的路由。router-view 组件则用于渲染当前路由所匹配的组件。这段代码使用了 Vue 的路由功能,通过路由来实现页面的跳转和组件的切换。
阅读全文
相关推荐

















