没有
- 发布:2024-07-09 11:18
- 更新:2024-07-19 11:52
- 阅读:332
可以复现问题的示例。打个压缩包发给我吧。。
-
HWww (作者)
我代码都发给你了,你随便找个项目,复制我发给你的hook useGetWindowInfo 就是这个,随便在哪里用就看的出来了,发了三个不同的写法给你,都是类型检测错误,麻烦你看哈
2024-07-10 11:01
-
-



HWww (作者)
type windowInfo = {
height : String,
top : String
}
const useGetWindowInfo = () : windowInfo => {
const windowInfo : windowInfo = {
height: "0px",
top: "0px"
}
//#ifdef MP-WEIXIN
const { top, height } = uni.getMenuButtonBoundingClientRect();
windowInfo.height = height + "px";
windowInfo.top = top + "px";
//#endif
//#ifndef MP-WEIXIN
const info = uni.getWindowInfo();
windowInfo.height = "50px";
windowInfo.top = info.statusBarHeight + "px";
//#endif
}
export default useGetWindowInfo
2024-07-09 17:15
HWww (作者)
以下是一个hooks 获取系统信息
const useGetWindowInfo = () : any => {
const top = ref<String>("0px");
const height = ref<String>("0px");
//#ifdef MP-WEIXIN
const { top, height } = uni.getMenuButtonBoundingClientRect();
height.value = height + "px";
top.value = top + "px";
//#endif
//#ifndef MP-WEIXIN
const info = uni.getWindowInfo();
height.value = "50px";
top.value = info.statusBarHeight + "px";
//#endif
return {
height,
top
}
}
export default useGetWindowInfo
以下是用这个hooks
<template>
<view class="index">
<view class="index-navigation" :style="{
height:height,
top:top
}">
</view>
</template>
<script lang="uts" setup>
import useGetWindowInfo from "../../hooks/useGetWindowInfo.uts"
const navigationTop = ref<String>("0px");
const navigationHeight = ref<String>("0px");
const { height, top } = useGetWindowInfo()
onMounted(() => {
// const { height, top } = useGetWindowInfo();
// navigationTop.value = top;
// navigationHeight.value = height;
// console.log(process.env.NODE_ENV);
</script>
<style lang="scss" scoped>
.index {
width: 100%;
background-color: rgba(245, 245, 245, 1);
position: relative;
min-height: 800rpx;
</style>
报错
17:21:38.430 error: Not enough information to infer type variable B
17:21:38.430 at pages/index/index.uvue:3:41
17:21:38.430 1 | <template>
17:21:38.430 2 | <view class="index">
17:21:38.430 3 | <view class="index-navigation" :style="{
17:21:38.430 | ^
17:21:38.430 4 | height:height,
17:21:38.430 5 | top:top
17:21:38.430 error: Not enough information to infer type variable T
17:21:38.430 at pages/index/index.uvue:3:41
17:21:38.430 1 | <template>
17:21:38.430 2 | <view class="index">
17:21:38.430 3 | <view class="index-navigation" :style="{
17:21:38.430 | ^
17:21:38.430 4 | height:height,
17:21:38.430 5 | top:top
2024-07-09 17:24
HWww (作者)
发给你两个回复都会出现bug,第一个bug详情忘了发给你了是这样的
17:25:30.495 error: Variable expected
17:25:30.495 at hooks/useGetWindowInfo.uts:18:12
17:25:30.495 16 |
17:25:30.495 17 | const info = uni.getWindowInfo();
17:25:30.495 18 | windowInfo.height = "50px";
17:25:30.495 | ^
17:25:30.495 19 | windowInfo.top = info.statusBarHeight + "px";
17:25:30.495 20 |
17:25:30.495 error: Variable expected
17:25:30.496 at hooks/useGetWindowInfo.uts:19:12
17:25:30.496 17 | const info = uni.getWindowInfo();
17:25:30.496 18 | windowInfo.height = "50px";
17:25:30.496 19 | windowInfo.top = info.statusBarHeight + "px";
2024-07-09 17:26
HWww (作者)
这样写也有问题,你们确定这是ts?各种ts写法都尝试过了
type windowInfo = {
height : String,
top : String
}
const useGetWindowInfo = () : windowInfo => {
let statusBarHeight : String;
let statusBarTop : String;
//#ifdef MP-WEIXIN
const { top, height } = uni.getMenuButtonBoundingClientRect();
statusBarHeight = height + "px";
statusBarTop = top + "px";
//#endif
//#ifndef MP-WEIXIN
const info = uni.getWindowInfo();
statusBarHeight = "50px";
statusBarTop = info.statusBarHeight + "px";
//#endif
}
export default useGetWindowInfo
以下是用这个hooks的代码
<template>
<view class="index">
<view class="index-navigation" :style="{
height:navigationHeight,
top:navigationTop
}">
</view>
</template>
<script lang="uts" setup>
import useGetWindowInfo from "../../hooks/useGetWindowInfo.uts"
const navigationTop = ref<String>("0px");
const navigationHeight = ref<String>("0px");
onMounted(() => {
const { height, top } = useGetWindowInfo();
navigationTop.value = top;
navigationHeight.value = height;
</script>
<style lang="scss" scoped>
.index {
width: 100%;
background-color: rgba(245, 245, 245, 1);
position: relative;
min-height: 800rpx;
</style>
以下是报错
17:33:35.510 error: Cannot access class 'uni.UNIFB0641F.windowInfo'. Check your module classpath for missing or conflicting dependencies
17:33:35.510 at pages/index/index.uvue:17:26
17:33:35.510 15 | const navigationHeight = ref<String>("0px");
17:33:35.510 16 | onMounted(() => {
17:33:35.510 17 | const { height, top } = useGetWindowInfo();
17:33:35.510 | ^
17:33:35.510 18 | navigationTop.value = top;
17:33:35.510 19 | navigationHeight.value = height;
17:33:35.510 error: Unresolved reference: top
17:33:35.510 at pages/index/index.uvue:17:18
17:33:35.510 15 | const navigationHeight = ref<String>("0px");
17:33:35.510 16 | onMounted(() => {
17:33:35.510 17 | const { height, top } = useGetWindowInfo();
17:33:35.510 | ^
17:33:35.510 18 | navigationTop.value = top;
17:33:35.510 19 | navigationHeight.value = height;
2024-07-09 17:35