当前位置:网站首页>Uni app through uni Navigateto failed to pass parameter (pass object)

Uni app through uni Navigateto failed to pass parameter (pass object)

2022-06-25 19:48:00 We are all lemonade

Use navigateTo() This method is passed to the object , Print the transfer parameters in the page you jump to , You will find that the data you get is not what you want . Such as :
1、 Jump

	//  details 
		myInInfoClick(obj) {
    
			// this.dataItem = obj;
			uni.navigateTo({
    
				url: './components/addIn?dataItem='+obj
			});
		},

2、 obtain

// onLoad  Indicates that the listening page is loaded , Its parameter is the data passed from the previous page 
	onLoad(option) {
    
		console.log(option);
	}

3、 Print
 Insert picture description here
4、 reason
url Reference time , Do not convert data and when parameters have special characters , Will be intercepted , As a result, the data is incomplete . therefore , I want to solve this problem , Need to use encodeURIComponent Decode the data .
5、 terms of settlement
Be careful : Use encodeURIComponent When coding , Special characters of parameters need to be converted , Translates into string type .
(1) code

uni.navigateTo({
    
// obj  Contains special characters   Use JSON.stringify() Convert special characters   And use encodeURlComponent() Encoding   Browser can read 
url:'./components/addIn?dataItem='+encodeURlComponent(JSON.stringify(obj))
});

(2) decode

summary :
First put the data JSON.stringify turn string Format , Reuse encodeURIComponent To convert special characters of data . Jump to the page , Use decodeURIComponent Turn back special characters , Reuse JSON.parse Convert to object format .

原网站

版权声明
本文为[We are all lemonade]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190514313204.html