Home >Web Front-end >uni-app >Why can't uniapp relaunch take parameters?
In recent years, with the rapid development of mobile applications, uniapp, as a cross-platform mobile application development framework, has also received more and more attention and use. Among them, the relaunch (restart application) function is a very important function in the uniapp framework, which allows the application to restart after performing some specific operations to achieve some specific needs. However, with the deepening of use, many developers found that they encountered a more difficult problem when using uniapp relaunch, that is, they cannot bring parameters when restarting the application. This article will explore this problem and provide some solutions.
1. Problem manifestation
In actual development, we often need to use the uniapp relaunch function to jump to another page and carry some parameters to achieve some specific needs after restarting. However, when we try to use uni.navigateTo or uni.redirectTo to open a new page and carry parameters through the query field, we will find that the parameters obtained after restarting are all undefined. This is because when the application is restarted, uniapp does not save the parameter information we carry.
2. Cause of the problem
In order to understand this problem in depth, we need to understand how uniapp relaunch works. In uniapp, we can restart the application through the uni.reLaunch() function, which will close the current applet and restart it to the first page of the application. This process is similar to the process of opening the app again after we click the back button in the app to reach the desktop. During this process, uniapp will clear all the status of the application, including routing, cache and other information, so we cannot use query parameters and other methods to save information.
3. Solution
Since we cannot pass information through query parameters, what other solutions do we have?
We can save our parameter information through simple local storage technology. The parameters are stored in local storage in the form of JSON strings and read out when the application is restarted to realize the transfer of information. Of course, it should be noted that local storage can only store string type data, so we need to convert the parameters into JSON string form for storage.
In uniapp, we can use Vuex to manage the global state. This can be achieved by storing information in Vuex and reading it out when restarting the application. Transmission of information. It should be noted that in some cases, restarting the application will cause the Vuex state to be cleared, so it needs to be used with caution.
The Chinese open source community provides an open source framework of uniapp-relaunch-query that can solve this problem. This framework can save the route and its parameter information in the application cache when the application is restarted. When the application is started again, the route and its parameters in the application cache can be restored to realize the transfer of parameters.
4. Summary
The problem of not being able to bring parameters in uniapp relaunch is actually due to the fact that uniapp clears the application's status information when restarting the application, causing the parameter information to be unable to be passed. Solutions can use local storage, Vuex global state management, third-party frameworks and other methods. When using them, you need to pay attention to their respective advantages, disadvantages and applicable scenarios. Solving this problem requires developers to think innovatively and continue to try, thereby improving application development efficiency and user experience.
The above is the detailed content of Why can't uniapp relaunch take parameters?. For more information, please follow other related articles on the PHP Chinese website!