In Vue, save the user logon status instance code and Log On As A vue user.

Source: Internet
Author: User
Tags sessionstorage

In Vue, save the user logon status instance code and Log On As A vue user.

First, we assume that the logon component (register. vue) is a sub-component of the App. vue component and enters the logon component through a route.

After you click "Log on" in the logon component, a user name, my App, will be uploaded to the background. in the vue component, you need to get the user name and change the above "Login registration" to "User Name ".

To ensure that the user name does not disappear after refreshing, sessionStorage is used here.

The Code is as follows:

In register. vue, click Login to trigger the signIn method.

   signIn(){    this.formData = $(".form").serialize();    var that = this;    this.$http.get("/api/user", this.formData)     .then(response => {      that.userName = response.data.data.user.userName;      that.userHead = response.data.data.userHead;      that.$emit('userSignIn', that.userName);     })     .catch(error => {      console.log(error);     });   }

Here, to test my direct mock data, the actual situation should be this. $ http. post.

The point here is that

 that.$emit('userSignIn', that.userName);

Pass a value to the parent component (App. vue)

App. vue code HTML

<keep-alive>   <router-view @userSignIn="userSignIn"></router-view></keep-alive>

App. vue code JS

Export default {data () {return {userName: sessionStorage. userName }}, methods: {// sub-component (register) transfers the user name to userSignIn (userName) {sessionStorage. userName = userName; this. userName = sessionStorage. userName ;}}}

In this way, the parent component can use the user name to maintain the logon status, and because sessionStorage is used, the page can also be refreshed.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.