3***@qq.com
3***@qq.com
  • 发布:2025-03-28 20:16
  • 更新:2025-03-31 11:28
  • 阅读:31

【报Bug】data里面的数据更新了,模板视图获取不到新的值

分类:uni-app

产品分类: uniapp/小程序/微信

PC开发环境操作系统: Mac

PC开发环境操作系统版本号: 13.4 (22F66)

HBuilderX类型: 正式

HBuilderX版本号: 4.24

第三方开发者工具版本号: 4.57

基础库版本号: 3.7.11

项目创建方式: HBuilderX

示例代码:
<template> <view class="bg-white" style="width: 100vw;height: 100vh;z-index: 999;"> <view class="cu-bar bg-cyan search" style="height: 52px;"> <!--搜索栏--> <view class="search-form radius"> <uni-easyinput v-model="keyword" ref="searchInput" @focus="inputFocus" @blur="inputBlur" confirmType="search"

inputBorder="false" :styles="{width: '100%', height: '100%'}" placeholder="搜索行程地点名称" @confirm="handleSearch"></uni-easyinput>
</view>
<view class="action">
<view class="row-between">
<view class="" @click="handleSearch(keyword)">
<text class="cuIcon-search line-white"></text>
<text class="text-df text-white">搜索</text>
</view>
<view class="margin-left" @click="cancelSearch">
<!-- <text class="cuIcon-close"></text>-->
<text class="text-df">取消</text>
</view>
</view>
</view>
</view>

<view class="" v-if="searchResult.list"> <!--搜索结果-->
{{searchResult.list.length}}
<scroll-view scroll-y="true" style="max-height: calc(100vh - 52px);" class="">
<u-cell-group>
<u-cell :arrow="false" :isLink="true" v-for="item in searchResult.list" :key="item.id" @click="onSelect(item)">
<view slot="title" class="flex justify-start align-center">
<view class=".cuIcon-location"/>
<um-texthighlight class="margin-left-xs text-df" :text="item.name" :keyword="keyword" color="blue"/>
</view>
<view slot="label" class="text-grey text-sm">
<view class="row-start margin-top-xs">
<text class="margin-left-lg">{{ item.pname }}</text>
<text class="margin-left-sm">{{ item.cityname }}</text>
<text class="margin-left-sm">{{ item.adname }}</text>
</view>
<view class="row-start margin-top-xs">
<view class="margin-left-lg">{{ item.address }}</view>
</view>
</view>
<view slot="right-icon">
<!-- <u-icon name="star" size="20" @clicl.stop="collectLocation(item)"/>-->
<text class="text-grey text-sm" @click.stop="collectLocation(item)" v-show="!inCollect(item)">收藏地址
</text>
</view>
</u-cell>
</u-cell-group>
</scroll-view>
</view>

<view class="" v-show="collect.show">
<view class="padding-sm">
<text class="text-black text-df">收藏过的地址</text>
</view>
<scroll-view scroll-y="true" style="height: calc(100vh - 92px);max-height: calc(100vh - 92px);" class="">
<u-cell-group>
<u-cell :arrow="false" :isLink="true" v-for="(item, index) in collect.list" @click="onSelect(item)">
<view slot="title" class="flex justify-start align-center">
<view class=".cuIcon-location"/>
<um-texthighlight class="margin-left-xs text-df" :text="item.name" :keyword="keyword" color="blue"/>
</view>
<view slot="label" class="text-grey text-sm">
<view class="row-start margin-top-xs">
<text class="margin-left-lg">{{ item.pname }}</text>
<text class="margin-left-sm">{{ item.cityname }}</text>
<text class="margin-left-sm">{{ item.adname }}</text>
</view>
<view class="row-start margin-top-xs">
<view class="margin-left-lg">{{ item.address }}</view>
</view>
</view>
<view slot="right-icon">
<text class="text-grey text-sm" @click.stop="unCollectLocation(index)">取消收藏</text>
</view>
</u-cell>
</u-cell-group>
</scroll-view>
</view>

</view>
</template>

<script>

import debounce from "../../uni_modules/uview-ui/libs/function/debounce";
import {getGaodePlaceSearchV2} from "../../api/common";
import UmTexthighlight from "../../uni_modules/um-texthighlight/components/um-texthighlight/um-texthighlight.vue";
import UniEasyinput from "../../uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.vue";
import UIcon from "../../uni_modules/uview-ui/components/u-icon/u-icon.vue";
import {getAppUserPreference, saveAppUserPreference} from "../../api/user";
import {info} from "../../utils/wxlog";

const PREFERENCE_SCENE_COLLECT_ADDRESS = 'app:trip:collect-address';

export default {
name: 'location-search',
props: [],
components: {UIcon, UniEasyinput, UmTexthighlight},
data() {
return {
keyword: undefined,
searchResult: {
show: false, // 展示搜索结果开关,仅在用户输入关键字请求后台数据后展示
list: undefined, // 搜索结果数据
},
collect: {
show: true, // 默认展示收藏地址
list: [],
},
}
},
onLoad(option) {
// console.log(option)
},
computed: {},
created() {
this.refreshCollect()
},
methods: {
/**

  • 搜索框获取焦点
    */
    inputFocus() {
    // console.log('搜索框获取焦点')
    // 隐藏收藏地址列表
    this.collect.show = false
    this.searchResult.show = true
    },
    /**
  • 搜索框失去焦点
    */
    inputBlur() {
    // console.log('搜索框失去焦点')
    // 显示收藏地址列表
    if (!this.keyword || this.keyword.length === 0) {
    this.collect.show = true
    this.searchResult.show = false
    }
    },
    /**
  • 初始化搜索框内容
    */
    initSearch(location) {
    // 自动填入关键字
    this.$set(this, 'keyword', location.name || '')
    this.clearSearchResult()
    // 自动获取输入框焦点 todo
    // 更新收藏地址
    this.refreshCollect()
    },
    /**
  • 取消搜索
    */
    cancelSearch() {
    this.$emit('cancel')
    },
    /**
  • 处理用户输入的搜索关键字
    */
    handleSearch(keyword) {
    this.keyword = keyword
    if (!keyword || keyword.length === 0) {
    // toast:请输入地点名称
    uni.$u.toast('请输入地点名称')
    return
    }
    uni.showLoading({
    title: '搜索中'
    });
    // 调后端接口查询地址
    getGaodePlaceSearchV2({
    keywords: keyword,
    }).then(({data}) => {
    this.searchResult.list = data.pois;
    this.searchResult.show = true
    this.collect.show = false
    uni.hideLoading();
    this.$forceUpdate()
    info('高德地点搜索后端返回第一个地点', this.searchResult.list[0])
    info('搜索展示开关状态', this.searchResult.show)
    info('收藏地址展示开关状态', this.collect.show)
    }).catch(err => {
    info('高德地点搜索后端报错', err.data)
    uni.$u.toast(err.data.msg)
    })
    },
    /**
  • 清空搜索结果
  • @param item
    */
    clearSearchResult() {
    this.searchResult.show = false
    this.collect.show = true
    this.searchResult.list = undefined
    },
    /**
  • 刷新收藏地址列表
    */
    refreshCollect() {
    getAppUserPreference(PREFERENCE_SCENE_COLLECT_ADDRESS, list => {
    this.collect.list = list
    console.log('收藏地址列表:', list)
    })
    },
    /**
  • 收藏地址
    */
    collectLocation(item) {
    if (!this.collect.list) {
    this.collect.list = []
    }
    this.collect.list.push(item)
    saveAppUserPreference(PREFERENCE_SCENE_COLLECT_ADDRESS, this.collect.list).then(({data}) => {
    // toast
    uni.$u.toast('收藏成功')
    this.refreshCollect()
    })
    },
    /**
  • 取消收藏
    */
    unCollectLocation(index) {
    this.collect.list.splice(index, 1)
    saveAppUserPreference(PREFERENCE_SCENE_COLLECT_ADDRESS, this.collect.list).then(({data}) => {
    // toast
    uni.$u.toast('取消成功')
    this.refreshCollect()
    })
    },
    /**
  • 判断某个地址在不在收藏列表
    */
    inCollect(location) {
    return this.collect.list.findIndex(item => item.id === location.id) > -1
    },
    /**
  • 选中搜索出来的地址
    */
    onSelect(item) {
    this.$emit('select', item)
    },
    },
    }
    </script>

<style lang="scss" scoped>

</style>

操作步骤:

微信小程序模拟器真机体验(包括正式发布都一样),真机执行。
测试手机设备:

华为 Mate20 型号代码:HMA-AL00 操作系统: HarmonyOS 4.0.0

预期结果:

显示搜索出来的地址

实际结果:

微信小程序模拟器运行正常,但在测试机器上不显示数据。

bug描述:

handleSearch方法为this.searchResult.list赋值成功了(从微信实时日志可知),但是真机上依然显示this.searchResult.list是空值。

ps:微信小程序模拟器内功能是正常显示的,就是真机不行。

测试手机设备:

华为 Mate20 型号代码:HMA-AL00 操作系统: HarmonyOS 4.0.0

2025-03-28 20:16 负责人:无 分享
已邀请:
DCloud_UNI_OttoJi

DCloud_UNI_OttoJi - 日常回复 uni-app/x 问题,如果艾特我没看到,请主动私信

如果是微信小程序模拟器内功能是正常显示的,就是真机不行。这个不应该,那需要排除一下环境问题。

你删减页面结构,只保留问题组件。你现在提供的源码内容太多,而且没有提供相对路径的逻辑,我无法直接运行测试。

要回复问题请先登录注册