uni-app在頁面之前如何發送和傳遞數組?如果直接發送和傳遞數組,接收到的消息如下顯示。不能進一步獲取對象值。
要想能夠接收到數組對象的參數。可以先將數組轉化為JSON字符串,傳遞到頁面后在解析為JavaScript對象。設頁面1傳遞數據到頁面2.則,頁面1的關鍵代碼:
1 /** 2 * 跳轉到下一個頁面,並傳遞參數 3 */ 4 toNext: function() { 5 // #ifdef H5 6 let items = encodeURIComponent(JSON.stringify(this.person)); 7 // #endif 8 // #ifdef MP-QQ||MP-WEIXIN 9 let items = JSON.stringify(this.person); 10 // #endif 11 uni.navigateTo({ 12 url: 'page4?person=' + items13 }) 14 }
頁面2接收數據,關鍵代碼:
1 onLoad: function(options) { 2 // #ifdef H5 3 let obj = options.person.replace("\"([^\"]*)\"", "$1"); 4 let person = JSON.parse(obj); 5 // #endif 6 // #ifdef MP-QQ||MP-WEIXIN||APP-NVUE||APP-PLUS 7 let person = JSON.parse(options.person); 8 // #endif 9 console.log(person); 10 },
此時,新的頁面能夠的都person對象。