【菜比前端之不起眼小bug差点要我小命】Uniapp小程序发布小程序模拟器正常,但是真机无法打开问题

项目场景及描述:

提示:这里简述项目相关背景及问题描述:

Uniapp小程序发布小程序在模拟器上运行正常,但是扫码打开体验版却有明显bug导致页面无法正常进行,如果早先发布的版本没问题,打开体验版前把旧的体验版删除,扫码新体验版就可复现bug。

这种bug最不容易发现,但是往往会导致新用户无法打开小程序,导致小程序疯狂被差评!!!
差评?这对前端程序员是极大的侮辱啊!!!

最近刚优化完两小程序,就在这里总结下,能卡住小程序进不去首页或导致页面不能正常加载的bug,总结以下了几点…


项目环境

开发平台:uniapp
技术栈:vue3 + pinia + js + vite3 + i18n
组件库:uview-plus
主要发布平台:微信小程序、支付宝小程序、h5、Android、IOS

Bug示例

1、如果项目使用了统一的状态管理器并安装了缓存插件
比如安装了 piniapinia-plugin-persistedstate
那么你需要在首页避免的是在 Data 中直接引用Store内定义的数据,特别是需要通过接口赋值的Store数据,然后又在首页中使用了store内定义的初始化方法,这就容易导致出现这个问题。

// data
const currentCity = ref(useCommonStore.currentCity)

// function
function init(){
	useCommonStore.getCityList().then(() => {
		cityMap.map((item, index) => {
			let areaItem = useCommonStore.cityList.filter(h => Number(h.areaId) === item)[0]
			if (areaItem != undefined) {
				endCityList.push(areaItem)
			}
		})
	})
	
}

// onLoad
onLoad(()=>{
	init()
})

如上代码所示,因为运行机制的问题,currentCity在外层定义了属性值,但是实际在项目运行中,onLoad是在页面Dom加载完成后执行的生命周期,也就是Vue的运行机制自上而下的,在外部定义的CurrentCity这属性的值是动态的,不是静态的,因而在页面加载完成后,onLoad执行阶段如果没有在此对CurrentCity赋值,那么这个bug就会导致页面的加载出现问题。

非常小的一个细节,但是很致命。


解决方案:

提示:这里填写该问题的具体解决方案:

解决方案也很简单,就是将动态数据放到正确的地方执行就好。

// data
const currentCity = ref('')

// function
function init(){
	useCommonStore.getCityList().then(() => {
		currentCity.value  = useCommonStore.currentCity
		cityMap.map((item, index) => {
			let areaItem = useCommonStore.cityList.filter(h => Number(h.areaId) === item)[0]
			if (areaItem != undefined) {
				endCityList.push(areaItem)
			}
		})
	})
	
}

// onLoad
onLoad(()=>{
	init()
})

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的搜索框示例,包含自定义搜索引擎功能: HTML代码: ``` <div class="search-container"> <input type="text" id="search-input" placeholder="Search..."> <button class="search-button" onclick="search()">Search</button> <div class="search-engines"> <button class="engine-button" onclick="selectEngine()">Google</button> <button class="engine-button" onclick="selectEngine()">Bing</button> <button class="engine-button" onclick="selectEngine()">Yahoo</button> <button class="engine-button" onclick="selectCustomEngine()">Add</button> <div id="custom-engine" class="hidden"> <input type="text" id="engine-name" placeholder="Enter engine name"> <input type="text" id="engine-url" placeholder="Enter engine search url"> <button class="add-button" onclick="addEngine()">Add Engine</button> </div> </div> </div> ``` CSS代码: ``` .search-container { display: flex; align-items: center; } #search-input { flex: 1; padding: 6px; font-size: 16px; border: none; border-radius: 4px; background-color: #f1f1f1; } .search-button { margin-left: 6px; padding: 6px 12px; font-size: 16px; border: none; border-radius: 4px; background-color: #4CAF50; color: #fff; cursor: pointer; } .search-engines { position: relative; } .engine-button { margin-left: 6px; padding: 6px 12px; font-size: 16px; border: none; border-radius: 4px; background-color: #f1f1f1; cursor: pointer; } #custom-engine { position: absolute; top: 100%; left: 0; margin-top: 6px; padding: 6px; border-radius: 4px; background-color: #fff; box-shadow: 0 2px 4px rgba(0,0,0,0.2); } .hidden { display: none; } ``` JS代码: ``` var searchUrl = 'https://www.google.com/search?q='; function search() { var searchInput = document.getElementById('search-input'); var query = searchInput.value; window.location.href = searchUrl + encodeURIComponent(query); } function selectEngine() { // TODO: set searchUrl based on selected engine // example: searchUrl = 'https://www.bing.com/search?q='; } function selectCustomEngine() { var customEngine = document.getElementById('custom-engine'); customEngine.classList.toggle('hidden'); } function addEngine() { var engineName = document.getElementById('engine-name').value; var engineUrl = document.getElementById('engine-url').value; if (engineName && engineUrl) { var engines = document.getElementsByClassName('search-engines')[0]; var button = document.createElement('button'); button.className = 'engine-button'; button.textContent = engineName; button.onclick = function() { searchUrl = engineUrl; selectEngine(); } engines.insertBefore(button, engines.lastChild); var customEngine = document.getElementById('custom-engine'); customEngine.classList.add('hidden'); } } ``` 上面的代码展示了一个简单的搜索框,包含一个文本输入框、搜索按钮和选择搜索引擎的按钮。点击搜索按钮会将查询字符串添加到搜索引擎的URL中,并打开新页面进行搜索。选择搜索引擎时,可以通过设置全局变量searchUrl来确定所选引擎的URL。添加自定义搜索引擎时,用户可以在弹出的表单中输入引擎名称和搜索URL,并将其添加为选项之一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值