Uni-app login state management (vuex)

Applications remain logged common needs, how to keep the user logged in when the paper on the use of uni-app framework.

Namely: the initial application is not logged into the state -------> Log ----------> close the application, open it again ---------> is already logged

Traditional applications remain logged way is by reading the cookie to determine whether it is logged in, but does not support uni-app to read and write to the cookie. Because the main frame is uni-app VUE, it can be used to sign vuex state management.

 Global variables and methods defined on vuex need to be defined in the specified directory files.

That is the new name in the project directory to the directory store, create global variables and methods for defining js file directory, as follows:

  index.js file contents as shown below:

Want this js file defined variables and methods can be used in each page and take effect, you need to import this file and js main.js declaration in a file in the project directory, as shown below:

After the end of the statement you can monitor global variables and corresponding methods of the page, showing in detail below in a login page.

In the method of introducing vuex first page, as follows:

Then, using the computed attribute calculation method mapState monitoring global variables, global methods for using mapMutations monitoring method, as follows:

After a successful login Jump to define the function of individual centers, as follows:

Network A successful request, calling the callback function in the success of the method, and the data to the login method returns the value of the callback function, as follows:

The login method index.js file will then pass over user data stored in the local cache.

app app did not listen to the other by a periodic function APP.vue file when entering the application, the definition of the method, the user data is extracted from the local cache in the log loading function, so that they do not clear the local cache, each open application Login time when the state will, does not need to repeatedly initiate network logon request, as follows:

Guess you like

Origin www.cnblogs.com/edward-life/p/11181139.html