您的位置:首页 > 文旅 > 美景 > 【Vue】- ref获取DOM元素和购物车案例分析

【Vue】- ref获取DOM元素和购物车案例分析

2024/12/23 16:36:04 来源:https://blog.csdn.net/sinat_24542619/article/details/142279095  浏览:    关键词:【Vue】- ref获取DOM元素和购物车案例分析

文章目录

  • 知识回顾
  • 前言
  • 源码分析
    • 1. ref
    • 2. 购物车案例分析
    • 3. 购物车计算、全选
  • 拓展知识
    • 数据持久化localStorage
  • 总结


知识回顾

前言

元素上使用 ref属性关联响应式数据,获取DOM元素

步骤

● 创建 ref => const hRef = ref(null)
● 模板中建立关联 => <h1 ref="hRef">我是标题</h1>=> hRef.value

源码分析

1. ref

const inp = ref<null | HTMLInputElement>()
const img = ref<null | HTMLImageElement>()

vue

<script setup lang="ts">
import { onMounted, ref } from 'vue'const words = ref('')
const inp = ref<null | HTMLInputElement>()
const img = ref<null | HTMLImageElement>()
onMounted(() => {console.log('onMounted')inp.value?.focus()img.valueconsole.log(img.value)console.log(inp.value)
})
</script><template><div class="container" id="app"><div class="search-container"><img ref="img" src="https://" alt="" /><div class="search-box"><input ref="inp" type="text" v-model="words" id="inp" /><button>搜索一下</button></div></div></div>
</template><style>
html,
body {height: 100%;
}.search-container {position: absolute;top: 30%;left: 50%;transform: translate(-50%, -50%);text-align: center;
}.search-container .search-box {display: flex;
}.search-container img {margin-bottom: 30px;
}.search-container .search-box input {width: 512px;height: 16px;padding: 12px 16px;font-size: 16px;margin: 0;vertical-align: top;outline: 0;box-shadow: none;border-radius: 10px 0 0 10px;border: 2px solid #c4c7ce;background: #fff;color: #222;overflow: hidden;box-sizing: content-box;-webkit-tap-highlight-color: transparent;
}.search-container .search-box button {cursor: pointer;width: 112px;height: 44px;line-height: 41px;line-height: 42px;background-color: #ad2a27;border-radius: 0 10px 10px 0;font-size: 17px;box-shadow: none;font-weight: 400;border: 0;outline: 0;letter-spacing: normal;color: white;
}body {background: no-repeat center / cover;background-color: #edf0f5;
}
</style>

2. 购物车案例分析

列表渲染、购物车增加删除
css

.app-container {padding-bottom: 300px;width: 800px;margin: 0 auto;
}
@media screen and (max-width: 800px) {.app-container {width: 600px;}
}
.app-container .banner-box {border-radius: 20px;overflow: hidden;margin-bottom: 10px;
}
.app-container .banner-box img {width: 100%;
}
.app-container .nav-box {background: #ddedec;height: 60px;border-radius: 10px;padding-left: 20px;display: flex;align-items: center;
}
.app-container .nav-box .my-nav {display: inline-block;background: #5fca71;border-radius: 5px;width: 90px;height: 35px;color: white;text-align: center;line-height: 35px;margin-right: 10px;
}.breadcrumb {font-size: 16px;color: gray;
}
.table {width: 100%;text-align: left;border-radius: 2px 2px 0 0;border-collapse: separate;border-spacing: 0;
}
.th {color: rgba(0, 0, 0, 0.85);font-weight: 500;text-align: left;background: #fafafa;border-bottom: 1px solid #f0f0f0;transition: background 0.3s ease;
}
.th.num-th {flex: 1.5;
}
.th {text-align: center;
}
.th:nth-child(4),
.th:nth-child(5),
.th:nth-child(6),
.th:nth-child(7) {text-align: center;
}
.th.th-pic {flex: 1.3;
}
.th:nth-child(6) {flex: 1.3;
}.th,
.td {position: relative;padding: 16px 16px;overflow-wrap: break-word;flex: 1;
}
.pick-td {font-size: 14px;
}
.main,
.empty {border: 1px solid #f0f0f0;margin-top: 10px;
}
.tr {display: flex;cursor: pointer;border-bottom: 1px solid #ebeef5;
}
.tr.active {background-color: #f5f7fa;
}
.td {display: flex;justify-content: center;align-

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com