debugging-help.mdc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. ---
  2. alwaysApply: false
  3. ---
  4. ### 调试和问题排查
  5. #### 常见问题和解决方案
  6. **1. 页面跳转问题**
  7. ```javascript
  8. // 问题:页面跳转失败
  9. // 原因:路径错误、页面未在 pages.json 中注册
  10. // 解决方案:
  11. // 1. 检查路径是否正确(以 / 开头)
  12. uni.navigateTo({
  13. url: '/pages/order/detail?id=123' // 正确
  14. // url: 'pages/order/detail' // 错误:缺少前导斜杠
  15. })
  16. // 2. 确保页面已在 pages.json 中注册
  17. {
  18. "pages": [
  19. {
  20. "path": "pages/order/detail",
  21. "style": {
  22. "navigationBarTitleText": "订单详情"
  23. }
  24. }
  25. ]
  26. }
  27. // 3. 检查跳转层级限制(小程序最多10层)
  28. ```
  29. **2. 网络请求问题**
  30. ```javascript
  31. // 问题:请求失败 fail statusCode:600
  32. // 原因:域名未配置、SSL证书问题
  33. // 调试方法:
  34. // 1. 检查 manifest.json 中的域名配置
  35. "h5": {
  36. "devServer": {
  37. "proxy": {
  38. "/api": {
  39. "target": "https://your-api.com",
  40. "changeOrigin": true
  41. }
  42. }
  43. }
  44. }
  45. // 2. 小程序开发时设置不校验域名
  46. // 微信开发者工具 -> 设置 -> 项目设置 -> 不校验合法域名
  47. // 3. 添加请求日志
  48. uni.request({
  49. url: 'https://api.example.com/data',
  50. success: (res) => {
  51. console.log('请求成功:', res)
  52. },
  53. fail: (err) => {
  54. console.error('请求失败:', err)
  55. console.error('错误码:', err.statusCode)
  56. console.error('错误信息:', err.errMsg)
  57. }
  58. })
  59. ```
  60. **3. 样式问题**
  61. ```scss
  62. // 问题:样式在不同平台表现不一致
  63. // 解决方案:使用条件编译
  64. .container {
  65. padding: 32rpx;
  66. // H5 特定样式
  67. /* #ifdef H5 */
  68. min-height: calc(100vh - 44px);
  69. /* #endif */
  70. // 小程序特定样式
  71. /* #ifdef MP */
  72. min-height: 100vh;
  73. /* #endif */
  74. // App 特定样式
  75. /* #ifdef APP-PLUS */
  76. padding-top: var(--status-bar-height);
  77. /* #endif */
  78. }
  79. // 问题:rpx 计算不准确
  80. // 解决方案:注意设计稿基准
  81. // 设计稿 750px -> 1rpx = 1px
  82. // 设计稿 375px -> 1rpx = 0.5px
  83. ```
  84. **4. 生命周期问题**
  85. ```javascript
  86. // 问题:数据加载时机不对
  87. // 解决方案:了解生命周期差异
  88. export default {
  89. // 页面加载时触发(只触发一次)
  90. onLoad(options) {
  91. console.log('页面参数:', options)
  92. // 适合:初始化数据、获取路由参数
  93. },
  94. // 页面显示时触发(每次显示都触发)
  95. onShow() {
  96. console.log('页面显示')
  97. // 适合:刷新数据、更新状态
  98. },
  99. // 页面初次渲染完成(只触发一次)
  100. onReady() {
  101. console.log('页面渲染完成')
  102. // 适合:获取节点信息、设置导航栏
  103. }
  104. }
  105. ```
  106. #### 调试工具和技巧
  107. **1. 控制台调试**
  108. ```javascript
  109. // 条件编译调试信息
  110. // #ifdef H5
  111. console.log('H5环境调试信息')
  112. // #endif
  113. // #ifdef MP-WEIXIN
  114. console.log('微信小程序调试信息')
  115. // #endif
  116. // 使用 uni.getSystemInfo 获取环境信息
  117. uni.getSystemInfo({
  118. success: (res) => {
  119. console.log('平台信息:', {
  120. platform: res.platform,
  121. system: res.system,
  122. version: res.version,
  123. screenWidth: res.screenWidth,
  124. screenHeight: res.screenHeight
  125. })
  126. }
  127. })
  128. ```
  129. **2. 网络调试**
  130. ```javascript
  131. // 请求拦截器(用于调试)
  132. const originalRequest = uni.request
  133. uni.request = function(options) {
  134. console.log('请求发起:', {
  135. url: options.url,
  136. method: options.method,
  137. data: options.data,
  138. header: options.header
  139. })
  140. const originalSuccess = options.success
  141. options.success = function(res) {
  142. console.log('请求响应:', {
  143. url: options.url,
  144. statusCode: res.statusCode,
  145. data: res.data
  146. })
  147. originalSuccess && originalSuccess(res)
  148. }
  149. const originalFail = options.fail
  150. options.fail = function(err) {
  151. console.error('请求失败:', {
  152. url: options.url,
  153. error: err
  154. })
  155. originalFail && originalFail(err)
  156. }
  157. return originalRequest.call(this, options)
  158. }
  159. ```
  160. **3. 性能调试**
  161. ```javascript
  162. // 页面性能监控
  163. export default {
  164. data() {
  165. return {
  166. startTime: 0
  167. }
  168. },
  169. onLoad() {
  170. this.startTime = Date.now()
  171. },
  172. onReady() {
  173. const loadTime = Date.now() - this.startTime
  174. console.log(`页面加载耗时: ${loadTime}ms`)
  175. // 上报性能数据
  176. if (loadTime > 2000) {
  177. console.warn('页面加载时间过长:', loadTime)
  178. }
  179. }
  180. }
  181. // 列表渲染性能优化
  182. // 使用 key 优化渲染
  183. // 避免在模板中使用复杂计算
  184. // 使用 v-show 替代频繁的 v-if
  185. ```
  186. **4. 错误监控**
  187. ```javascript
  188. // App.vue 全局错误处理
  189. export default {
  190. onError(err) {
  191. console.error('全局错误:', err)
  192. // 错误上报
  193. this.reportError(err)
  194. },
  195. onPageNotFound(res) {
  196. console.error('页面不存在:', res)
  197. // 重定向到首页或错误页
  198. uni.redirectTo({
  199. url: '/pages/index/index'
  200. })
  201. },
  202. methods: {
  203. reportError(err) {
  204. // 发送错误信息到监控平台
  205. uni.request({
  206. url: 'https://api.example.com/error-report',
  207. method: 'POST',
  208. data: {
  209. error: err.message,
  210. stack: err.stack,
  211. userAgent: navigator.userAgent,
  212. timestamp: Date.now()
  213. }
  214. })
  215. }
  216. }
  217. }
  218. ```
  219. #### 平台特定调试
  220. **1. 微信小程序调试**
  221. - 使用微信开发者工具的调试面板
  222. - 检查 Console、Network、Storage 等标签
  223. - 使用真机调试功能测试真实环境
  224. **2. H5 调试**
  225. - 使用浏览器开发者工具
  226. - 检查移动端适配(响应式设计模式)
  227. - 注意跨域问题和代理配置
  228. **3. App 调试**
  229. - 使用 HBuilderX 的真机运行功能
  230. - 查看原生日志(Android Studio/Xcode)
  231. - 测试原生能力(相机、位置、推送等)
  232. #### 常用调试代码片段
  233. **查看页面栈**
  234. ```javascript
  235. const pages = getCurrentPages()
  236. console.log('当前页面栈:', pages.map(page => page.route))
  237. ```
  238. **获取节点信息**
  239. ```javascript
  240. const query = uni.createSelectorQuery()
  241. query.select('.my-element').boundingClientRect((rect) => {
  242. console.log('元素位置信息:', rect)
  243. })
  244. query.exec()
  245. ```
  246. **检查存储数据**
  247. ```javascript
  248. const storage = uni.getStorageInfoSync()
  249. console.log('存储信息:', {
  250. keys: storage.keys,
  251. currentSize: storage.currentSize,
  252. limitSize: storage.limitSize
  253. })
  254. ```### 调试和问题排查
  255. #### 常见问题和解决方案
  256. **1. 页面跳转问题**
  257. ```javascript
  258. // 问题:页面跳转失败
  259. // 原因:路径错误、页面未在 pages.json 中注册
  260. // 解决方案:
  261. // 1. 检查路径是否正确(以 / 开头)
  262. uni.navigateTo({
  263. url: '/pages/order/detail?id=123' // 正确
  264. // url: 'pages/order/detail' // 错误:缺少前导斜杠
  265. })
  266. // 2. 确保页面已在 pages.json 中注册
  267. {
  268. "pages": [
  269. {
  270. "path": "pages/order/detail",
  271. "style": {
  272. "navigationBarTitleText": "订单详情"
  273. }
  274. }
  275. ]
  276. }
  277. // 3. 检查跳转层级限制(小程序最多10层)
  278. ```
  279. **2. 网络请求问题**
  280. ```javascript
  281. // 问题:请求失败 fail statusCode:600
  282. // 原因:域名未配置、SSL证书问题
  283. // 调试方法:
  284. // 1. 检查 manifest.json 中的域名配置
  285. "h5": {
  286. "devServer": {
  287. "proxy": {
  288. "/api": {
  289. "target": "https://your-api.com",
  290. "changeOrigin": true
  291. }
  292. }
  293. }
  294. }
  295. // 2. 小程序开发时设置不校验域名
  296. // 微信开发者工具 -> 设置 -> 项目设置 -> 不校验合法域名
  297. // 3. 添加请求日志
  298. uni.request({
  299. url: 'https://api.example.com/data',
  300. success: (res) => {
  301. console.log('请求成功:', res)
  302. },
  303. fail: (err) => {
  304. console.error('请求失败:', err)
  305. console.error('错误码:', err.statusCode)
  306. console.error('错误信息:', err.errMsg)
  307. }
  308. })
  309. ```
  310. **3. 样式问题**
  311. ```scss
  312. // 问题:样式在不同平台表现不一致
  313. // 解决方案:使用条件编译
  314. .container {
  315. padding: 32rpx;
  316. // H5 特定样式
  317. /* #ifdef H5 */
  318. min-height: calc(100vh - 44px);
  319. /* #endif */
  320. // 小程序特定样式
  321. /* #ifdef MP */
  322. min-height: 100vh;
  323. /* #endif */
  324. // App 特定样式
  325. /* #ifdef APP-PLUS */
  326. padding-top: var(--status-bar-height);
  327. /* #endif */
  328. }
  329. // 问题:rpx 计算不准确
  330. // 解决方案:注意设计稿基准
  331. // 设计稿 750px -> 1rpx = 1px
  332. // 设计稿 375px -> 1rpx = 0.5px
  333. ```
  334. **4. 生命周期问题**
  335. ```javascript
  336. // 问题:数据加载时机不对
  337. // 解决方案:了解生命周期差异
  338. export default {
  339. // 页面加载时触发(只触发一次)
  340. onLoad(options) {
  341. console.log('页面参数:', options)
  342. // 适合:初始化数据、获取路由参数
  343. },
  344. // 页面显示时触发(每次显示都触发)
  345. onShow() {
  346. console.log('页面显示')
  347. // 适合:刷新数据、更新状态
  348. },
  349. // 页面初次渲染完成(只触发一次)
  350. onReady() {
  351. console.log('页面渲染完成')
  352. // 适合:获取节点信息、设置导航栏
  353. }
  354. }
  355. ```
  356. #### 调试工具和技巧
  357. **1. 控制台调试**
  358. ```javascript
  359. // 条件编译调试信息
  360. // #ifdef H5
  361. console.log('H5环境调试信息')
  362. // #endif
  363. // #ifdef MP-WEIXIN
  364. console.log('微信小程序调试信息')
  365. // #endif
  366. // 使用 uni.getSystemInfo 获取环境信息
  367. uni.getSystemInfo({
  368. success: (res) => {
  369. console.log('平台信息:', {
  370. platform: res.platform,
  371. system: res.system,
  372. version: res.version,
  373. screenWidth: res.screenWidth,
  374. screenHeight: res.screenHeight
  375. })
  376. }
  377. })
  378. ```
  379. **2. 网络调试**
  380. ```javascript
  381. // 请求拦截器(用于调试)
  382. const originalRequest = uni.request
  383. uni.request = function(options) {
  384. console.log('请求发起:', {
  385. url: options.url,
  386. method: options.method,
  387. data: options.data,
  388. header: options.header
  389. })
  390. const originalSuccess = options.success
  391. options.success = function(res) {
  392. console.log('请求响应:', {
  393. url: options.url,
  394. statusCode: res.statusCode,
  395. data: res.data
  396. })
  397. originalSuccess && originalSuccess(res)
  398. }
  399. const originalFail = options.fail
  400. options.fail = function(err) {
  401. console.error('请求失败:', {
  402. url: options.url,
  403. error: err
  404. })
  405. originalFail && originalFail(err)
  406. }
  407. return originalRequest.call(this, options)
  408. }
  409. ```
  410. **3. 性能调试**
  411. ```javascript
  412. // 页面性能监控
  413. export default {
  414. data() {
  415. return {
  416. startTime: 0
  417. }
  418. },
  419. onLoad() {
  420. this.startTime = Date.now()
  421. },
  422. onReady() {
  423. const loadTime = Date.now() - this.startTime
  424. console.log(`页面加载耗时: ${loadTime}ms`)
  425. // 上报性能数据
  426. if (loadTime > 2000) {
  427. console.warn('页面加载时间过长:', loadTime)
  428. }
  429. }
  430. }
  431. // 列表渲染性能优化
  432. // 使用 key 优化渲染
  433. // 避免在模板中使用复杂计算
  434. // 使用 v-show 替代频繁的 v-if
  435. ```
  436. **4. 错误监控**
  437. ```javascript
  438. // App.vue 全局错误处理
  439. export default {
  440. onError(err) {
  441. console.error('全局错误:', err)
  442. // 错误上报
  443. this.reportError(err)
  444. },
  445. onPageNotFound(res) {
  446. console.error('页面不存在:', res)
  447. // 重定向到首页或错误页
  448. uni.redirectTo({
  449. url: '/pages/index/index'
  450. })
  451. },
  452. methods: {
  453. reportError(err) {
  454. // 发送错误信息到监控平台
  455. uni.request({
  456. url: 'https://api.example.com/error-report',
  457. method: 'POST',
  458. data: {
  459. error: err.message,
  460. stack: err.stack,
  461. userAgent: navigator.userAgent,
  462. timestamp: Date.now()
  463. }
  464. })
  465. }
  466. }
  467. }
  468. ```
  469. #### 平台特定调试
  470. **1. 微信小程序调试**
  471. - 使用微信开发者工具的调试面板
  472. - 检查 Console、Network、Storage 等标签
  473. - 使用真机调试功能测试真实环境
  474. **2. H5 调试**
  475. - 使用浏览器开发者工具
  476. - 检查移动端适配(响应式设计模式)
  477. - 注意跨域问题和代理配置
  478. **3. App 调试**
  479. - 使用 HBuilderX 的真机运行功能
  480. - 查看原生日志(Android Studio/Xcode)
  481. - 测试原生能力(相机、位置、推送等)
  482. #### 常用调试代码片段
  483. **查看页面栈**
  484. ```javascript
  485. const pages = getCurrentPages()
  486. console.log('当前页面栈:', pages.map(page => page.route))
  487. ```
  488. **获取节点信息**
  489. ```javascript
  490. const query = uni.createSelectorQuery()
  491. query.select('.my-element').boundingClientRect((rect) => {
  492. console.log('元素位置信息:', rect)
  493. })
  494. query.exec()
  495. ```
  496. **检查存储数据**
  497. ```javascript
  498. const storage = uni.getStorageInfoSync()
  499. console.log('存储信息:', {
  500. keys: storage.keys,
  501. currentSize: storage.currentSize,
  502. limitSize: storage.limitSize
  503. })
  504. ```