v-show和v-if的区别以及显示隐藏不生效的奇怪现象以及点击索引错位问题的解释

文章探讨了在Vue.js应用中使用v-show指令时遇到的显示隐藏问题,特别是当它在uView框架的自定义组件上失效。解决方案包括理解v-show的工作原理,即通过控制display属性,以及在遇到问题时如何手动设置display:none。同时提到了使用v-if可能导致的点击事件索引错位问题,推荐使用v-show作为替代。

基本概念没什么好讲的。有时候会遇到莫名其妙不显示的问题,这都是因为对这两个概念理解不透彻造成的。

v-show的本质

v-show的本质就是通过调用css的display:none来实现的,这点非常重要,出问题可以在浏览器调试页面手动设置display:none来验证到底v-show有没有生效。

显示隐藏不生效的问题

下面的代码是uview的一个组件,用于显示九宫格。其实就是循环遍历生成很多选项按钮,但是这里的v-show就是没有效果。这时候我手写style="display:none !important"也没有效果。正常情况下应该是有效果的,没效果是这个自定义组件的问题。

        <u-grid
            :border="false"
            @click="onClickAgentOptions"
            col="4"
        >
          <u-grid-item
              v-for="(option,index) in agentPerms"
              :key="index"
              v-show:option.show
          >

其实开源框架不会这么low。作者已经提供了custom-style属性:写成下面这样就可以了。parseShow处理返回none或者inline。自定义组件默认display就是inline(这点也非常重要)。
当v-show没效果的时候就需要考虑用display:none来手动实现了。
当v-show没效果的时候就需要考虑用display:none来手动实现了。
当v-show没效果的时候就需要考虑用display:none来手动实现了。

          <u-grid-item
              v-for="(option,index) in basicOptions"
              :key="index"
              :custom-style="{display: parseShow(option)}"
          >

点击索引错位问题(v-if)

使用v-if的时候会存在一个非常严重的问题。就是如果通过v-if隐藏列表中的某项,就会导致按钮索引错乱的问题。解决办法就是必须用v-show。而使用v-show会遇到不生效的问题,解决办法就是上小节。

/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2025 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "stdio.h" #include "stdint.h" #include "string.h" #include "math.h" // 添加数学库用于浮点运算 #include "oled.h" #include "ds18b20.h" #include "delay.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ #define CURRENT_SENSOR_ADDR 0x02 #define CURRENT_READ_CMD_LEN 8 /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ char current_raw_str[64] = "Raw: -- -- -- -- -- -- --"; // 显示原始7字节数据 uint8_t uart3_rx_buffer[9]; // 存储9个字节 uint8_t uart3_rx_index = 0; // 当前接收索引 uint8_t uart3_receive_complete = 0; // 接收完成标志 uint8_t uart3_rx_byte; // 单字节临时变量 //电流传感器相关变量 uint8_t current_read_cmd[CURRENT_READ_CMD_LEN] = {0x02, 0x03, 0x00, 0x56, 0x00, 0x01, 0x64, 0x29}; uint8_t current_rx_buffer[7] = {0}; // 电流传感器返回数据缓冲区 uint8_t current_rx_index = 0; uint8_t current_receive_complete = 0; float current_value = 0.0f; char current_str[32] = "Current: --.- A"; uint32_t last_current_read_time = 0; // 电压相关 float voltage_value = 0.0f; char voltage_str[32] = "Voltage: --.-- V"; char voltage_raw_str[64] = "VRaw: -- -- -- -- -- -- --"; uint8_t voltage_read_cmd[8] = {0x01, 0x03, 0x00, 0x56, 0x00, 0x01, 0x64, 0x1A}; // 地址0x01,CRC已计算 #define VOLTAGE_READ_CMD_LEN 8 static uint8_t voltage_rx_index = 0; static uint8_t voltage_temp_buffer[7]; #define FRAME_SIZE 9 // 帧长度 = 帧头(1) + 数据(7) + 校验(1) = 9 uint8_t rx_byte; uint8_t rx_buffer[FRAME_SIZE] = {0}; uint8_t rx_index = 0; uint8_t frame_received = 0; uint8_t start_received = 0; // 是否接收到帧头FF float display_value = 0.0f; char display_str[50] = "Waiting for data..."; float current_temperature = 0.0f; uint8_t temp_update_flag = 0; // 标志位,表示需要刷新显示 float gas_concentration_uart3 = 0.0f; float gas_concentration_uart1 = 0.0f; uint8_t gas_update_flag = 0; // 分辨率映射表 const float res_table[3] = {1.0f, 0.1f, 0.01f}; char gas_str_uart3[32] = "VOC: --.- ppm"; char gas_str_uart1[32] = "CO : --.- ppm"; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ // CRC16 函数(Modbus标准,多项式:0x8005,初始值:0xFFFF) static uint16_t CalculateCRC16(uint8_t *data, uint8_t length) { uint16_t crc = 0xFFFF; for (int i = 0; i < length; i++) { crc ^= data[i]; for (int j = 0; j < 8; j++) { if (crc & 0x0001) { crc = (crc >> 1) ^ 0xA001; } else { crc >>= 1; } } } return crc; } /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ void Display_Temperature(uint8_t x, uint8_t y) { static float last_temp = -999.0f; static uint32_t last_update = 0; float current_temp; uint32_t now = HAL_GetTick(); // if (now - last_update < 500) { // return; // } if (DS18B20_Init() == 0) { current_temp = DS18B20_GetTemperture(); } else { current_temp = 999.9f; } // if (fabs(current_temp - last_temp) < 0.01f) { // last_update = now; // return; // } char temp_str[20]; if (current_temp == 999.9f) { strcpy(temp_str, "Err"); } else { sprintf(temp_str, "%.2f'C", current_temp); } // 标题 + 温度 OLED_ShowString(x, y, (uint8_t*)temp_str); last_temp = current_temp; last_update = now; } uint8_t update_display_flag = 0; // 显示刷新标志 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if (htim->Instance == TIM2) { OLED_Clear(); // OLED_ShowCHinese(0, 0, 0); // 标题汉字“温” // OLED_ShowCHinese(17, 0, 1); // 汉字“度” OLED_ShowString(0,0,(uint8_t*)"temp:"); Display_Temperature(38, 0); // 第一行:温度 OLED_ShowString(0, 1, (uint8_t*)gas_str_uart3); // 第二行:CO 浓度 OLED_ShowString(0, 2, (uint8_t*)gas_str_uart1); // 第三行:VOC 浓度 OLED_ShowString(0, 3, (uint8_t*)current_str); // 第四行:电流值 OLED_ShowString(0, 4, (uint8_t*)voltage_str); // 新增:电压值 OLED_ShowString(0, 5, (uint8_t*)voltage_raw_str); // 第五行:原始7字节 } if (htim->Instance == TIM6) { // 每秒发送一次电流读取命令 static int toggle = 0; if (toggle == 0) { // 发送电流请求(设备0x02) HAL_UART_Transmit(&huart1, current_read_cmd, CURRENT_READ_CMD_LEN, 100); } else { // 发送电压请求(设备0x01) HAL_UART_Transmit(&huart1, voltage_read_cmd, VOLTAGE_READ_CMD_LEN, 100); } toggle = 1 - toggle; // 切换下次发送 } } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART1_UART_Init(); MX_USART3_UART_Init(); MX_TIM3_Init(); MX_TIM2_Init(); MX_TIM6_Init(); /* USER CODE BEGIN 2 */ delay_init(); OLED_Init(); HAL_TIM_Base_Start_IT(&htim2); // 启动 TIM2 中断 HAL_TIM_Base_Start_IT(&htim6); // 启动 TIM6 中断 HAL_UART_Receive_IT(&huart1, &rx_byte, 1); HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { // HAL_UART_Transmit(&huart1, current_read_cmd, CURRENT_READ_CMD_LEN, 10); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { static uint8_t current_rx_index = 0; static uint8_t current_temp_buffer[7]; // 临时存储接收到的7字节 if (huart->Instance == USART1) { uint8_t byte = rx_byte; // 先判断属于哪一个设备(根据地址字段) if (byte == 0x02 || (voltage_rx_index == 0 && byte == 0x01)) { // 开始新帧:记录起始地址 if (byte == 0x02) { // 属于电流传感器帧 current_temp_buffer[current_rx_index++] = byte; } else if (byte == 0x01) { // 属于电压传感器帧 voltage_temp_buffer[voltage_rx_index++] = byte; } } else { // 不是以合法地址开头?可能是错位或噪声 current_rx_index = 0; voltage_rx_index = 0; HAL_UART_Receive_IT(&huart1, &rx_byte, 1); return; } // 继续接收直到满7字节 if (current_rx_index > 0 && current_rx_index < 7) { current_temp_buffer[current_rx_index++] = byte; if (current_rx_index < 7) { HAL_UART_Receive_IT(&huart1, &rx_byte, 1); return; } } else if (voltage_rx_index > 0 && voltage_rx_index < 7) { voltage_temp_buffer[voltage_rx_index++] = byte; if (voltage_rx_index < 7) { HAL_UART_Receive_IT(&huart1, &rx_byte, 1); return; } } // === 当前帧完整接收完成,进行分类处理 === if (current_rx_index == 7) { uint8_t *buf = current_temp_buffer; // 检查是否为合法电流响应帧 if (buf[0] == 0x02 && buf[1] == 0x03 && buf[2] == 0x02) { uint16_t received_crc = (buf[6] << 8) | buf[5]; uint16_t calculated_crc = CalculateCRC16(buf, 5); if (received_crc == calculated_crc) { uint16_t raw_current = (buf[3] << 8) | buf[4]; current_value = raw_current * 250.0f / 10000.0f; // 假设满量程10000对应250A sprintf(current_str, "Current: %.2f A", current_value); current_receive_complete = 1; sprintf(current_raw_str, "CRaw:%02X%02X%02X%02X%02X%02X%02X", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); } } current_rx_index = 0; } if (voltage_rx_index == 7) { uint8_t *buf = voltage_temp_buffer; // 检查是否为合法电压响应帧 if (buf[0] == 0x01 && buf[1] == 0x03 && buf[2] == 0x02) { uint16_t received_crc = (buf[6] << 8) | buf[5]; uint16_t calculated_crc = CalculateCRC16(buf, 5); // if (received_crc == calculated_crc) // { uint16_t raw_voltage = (buf[3] << 8) | buf[4]; // 假设电压量程 0~1000V,对应 0~10000 数值 float V0 = 0.0f; float Vf = 1000.0f; voltage_value = raw_voltage * Vf / 10000.0f; sprintf(voltage_str, "Voltage:%.2f V", voltage_value); // 注意字符串长度,OLED_ShowString 要能显示 sprintf(voltage_raw_str, "VRaw:%02X%02X%02X%02X%02X%02X%02X", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); // } } voltage_rx_index = 0; } // 重启接收下一个字节 HAL_UART_Receive_IT(&huart1, &rx_byte, 1); } // 其他 UART 处理... if (huart->Instance == USART3) { uart3_rx_buffer[uart3_rx_index++] = uart3_rx_byte; if (uart3_rx_index >= 9) { uint16_t raw_value = (uart3_rx_buffer[2] << 8) | uart3_rx_buffer[3]; gas_concentration_uart3 = raw_value * 0.1f; sprintf(gas_str_uart3, "VOC:%.1f ppm", gas_concentration_uart3); uart3_receive_complete = 1; uart3_rx_index = 0; gas_update_flag = 1; } HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1); } } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ 现在电压传感器已经能正常接收单片机的数据,接收到指令后会返回七位数据,但是单片机oled上却没有数据显示,我一开始以为是校验的问题所以注释掉了CRC校验,但是依然不显示数据,帮我找找问题所在
10-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值