关于获取高度的问题
问题描述:
为什么获取的高度为0?
相关代码:
<template>
<div class="shop">
<Header>
<div class="shop_header">
<div class="back iconfont"></div>
<Search class="shop_search" message="请输入商品名称搜索" />
</div>
</Header>
<ShopInfo :item="item" :hideBorder="true" v-show="item.imgUrl" class="shop_title" />
<div class="shop_content">123</div>
<div class="shop_floor"></div>
</div>
</template>
<script>
import { onMounted, reactive, toRefs } from "vue";
import { useRoute } from "vue-router";
import { GET } from "../../utils/requset";
import Header from "@/components/Header/Header";
import Search from "@/components/Search/Search";
import ShopInfo from "@/components/ShopInfo/ShopInfo";
const useShopInfoEffect = () => {
const route = useRoute();
const data = reactive({ item: {} });
const shopId = route.params.id;
const getItemData = async () => {
const result = await GET(`/shop/${shopId}`);
if (result?.errno === 0 && result?.data) {
// data.item = result.data;
switch (shopId) {
case "1":
data.item = result.data.a;
// data.item = result.data[1];
break;
case "2":
data.item = result.data.b;
break;
case "3":
data.item = result.data.c;
break;
case "4":
data.item = result.data.d;
break;
case "5":
data.item = result.data.e;
break;
}
}
};
const { item } = toRefs(data);
return { item, getItemData };
};
const a = () => {
console.log(document.querySelector(".shop_title").clientHeight);
};
export default {
name: "Shop",
components: { Header, Search, ShopInfo },
setup() {
const { item, getItemData } = useShopInfoEffect();
getItemData();
onMounted(a);
return { item };
},
};
</script>
<style lang="scss" scoped>
.shop {
display: flex;
flex-direction: column;
width: 100%;
.back {
font-size: 0.3rem;
text-align: center;
}
&_header {
display: flex;
align-items: center;
height: 100%;
padding: 0 0.12rem;
}
&_search {
flex: 1;
}
&_title {
// position: fixed;
background: #fff;
padding: 0.1rem 0.12rem 0;
}
&_content {
height: 10rem;
overflow-y: auto;
}
&_floor {
position: fixed;
bottom: 0;
width: 100%;
height: 0.5rem;
background: #ffffff;
box-shadow: 0 -1px 1px 0 #f1f1f1;
}
}
</style>
正在回答 回答被采纳积分+1
同学你好,由于老师没有header等组件,并且接口地址等与同学的不一致,所以我调整了同学的代码后,才进行的测试。测试结果是可以执行方法a的:
建议同学下载源码,将如下代码(调整后的同学代码),替换源码中shop.vue组件,并启动项目测试一下:
<template>
<div class="shop">
<Header>
<!-- 结构调整了一下 -->
<div class="shop_header">
<div class="back iconfont"></div>
<Search class="shop_search" message="请输入商品名称搜索" />
</div>
<ShopInfo
:item="item"
:hideBorder="true"
v-show="item.imgUrl"
class="shop_title"
ref="reference"
/>
</Header>
<div class="shop_content">123</div>
<div class="shop_floor"></div>
</div>
</template>
<script>
import { onUpdated, reactive, toRefs } from 'vue'
import { useRoute } from 'vue-router'
// 引入的内容 和路径 调整了一下
import { get } from '../../utils/request'
import ShopInfo from '../../components/ShopInfo'
const useShopInfoEffect = () => {
const route = useRoute()
const data = reactive({ item: {} })
const shopId = route.params.id
const getItemData = async () => {
// 调整接口
const result = await get(`/api/shop/${shopId}`)
if (result?.errno === 0 && result?.data) {
// 调整数据
data.item = result.data
// switch (shopId) {
// case "1":
// data.item = result.data.a;
// // data.item = result.data[1];
// break;
// case "2":
// data.item = result.data.b;
// break;
// case "3":
// data.item = result.data.c;
// break;
// case "4":
// data.item = result.data.d;
// break;
// case "5":
// data.item = result.data.e;
// break;
// }
}
}
const { item } = toRefs(data)
return { item, getItemData }
}
const a = () => {
alert('123')
// 获取页面中,存在的元素的clientHeight
console.log(document.querySelector('.shop_title').clientHeight)
}
export default {
name: 'Shop',
components: { ShopInfo }, /* 将不存在的组件去掉 */
setup() {
const { item, getItemData } = useShopInfoEffect()
getItemData()
onUpdated(a)
return { item }
},
}
</script>
<style lang="scss" scoped>
.shop {
display: flex;
flex-direction: column;
position: absolute;
top: 0.85rem;
left: 0;
right: 0;
width: 100%;
.back {
font-size: 0.3rem;
text-align: center;
}
&_header {
display: flex;
align-items: center;
height: 100%;
padding: 0 0.12rem;
}
&_search {
flex: 1;
}
&_title {
background: #fff;
padding: 0.1rem 0.12rem 0;
}
&_content {
height: 10rem;
overflow-y: auto;
}
&_floor {
position: fixed;
bottom: 0;
width: 100%;
height: 0.5rem;
background: #ffffff;
box-shadow: 0 -1px 1px 0 #f1f1f1;
}
}
</style>
然后对照源码,检查一下自己的代码。
祝学习愉快!
同学你好,解答如下:
1、div.shop_title初始时,并没有内容,此时高度是0。当异步获取到数据,并渲染到页面上之后,div.shop_title才会有高度。由于onMounted事件触发时,div.shop_title中并没有真实的内容,所以获取到的高度是0。
可以使用定时器延时测试一下:
因此,建议将onMounted换成onUpdated,即在页面发生更新后,再获取高度(获取到数据并渲染到页面上后,会触发onUpdated):
2、另外,要注意,div.shop_title的结构是利用数据渲染的,所以一定要保证数据是正确的,
祝学习愉快!
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星