Home  >  Article  >  Web Front-end  >  Can vue do synchronization?

Can vue do synchronization?

藏色散人
藏色散人Original
2023-01-29 11:33:541678browse

Vue can do synchronization. The method for Vue to achieve synchronization: 1. Create a vue sample file; 2. Through "data(){return { userInfo: {id: '',username: '',password:'',avatar: '',},}}methods:{getUserInfo: function () {let _this = this;this.axios({...} )" to achieve synchronization.

Can vue do synchronization?

##The operating environment of this tutorial :Windows10 system, Vue version 3, DELL G3 computer

Can vue be synchronized?

Implementation of synchronization method in Vue

Scenario: When implementing the login function, a request is sent to the background through the user name of the form. The front-end thinks that the processing is completed and continues execution, but before the background data is returned, the user obtained by the front-end The data is empty.

Implementation: Wait for the request method to return data before continuing to execute, and implement the synchronization method

Original code
data() {
          	return {

                userInfo: {
                    id: '',
                    username: '',
                    password: '',
                    avatar: '',
                },
            }}methods:{
	getUserInfo: function () {
    	let _this = this;
    	this.axios({
        	url: "http://localhost:8088/verifyLogin",
        	headers: {
            	'Content-Type': 'application/json;charset=utf-8'
        	},
        	method: "post",
        	params: {
            	'userName': _this.form.username        	}
    	}).then(function (resp) {
        	_this.userInfo = resp.data;
        	console.log('11111111');
    	})
	},
    
    
    onSubmit(formName) {
        let _this = this;
        this.getUserInfo();
		// 为表单绑定验证功能
        this.$refs[formName].validate((valid) => {
            if (valid) {
                console.log("22222222");  
                console.log(_this.userInfo);
            } else {
            }
        });
   }}

Console Print

Can vue do synchronization?

Can vue do synchronization?

Found the problem: 1 is output on side 2, and is still empty after assigning value from data

Solution Method: Use async/await to achieve synchronization

data() {
          	return {

                userInfo: {
                    id: '',
                    username: '',
                    password: '',
                    avatar: '',
                },
            }}methods:{
	async getUserInfo(params) {
    	let _this = this;
    	let isSuccess = false;
    	await this.axios({
        	url: "http://localhost:8088/verifyLogin",
        	headers: {
            	'Content-Type': 'application/json;charset=utf-8'
        	},
        	method: "post",
        	params: {
            	'userName': _this.form.username        	}
    	}).then(function (resp) {
        	_this.userInfo = resp.data;
            console.log("11111111");
        	isSuccess = true;
    	});
    	return isSuccess;
	},

    onSubmit(formName) {
        let _this = this;
        this.getUserInfo(_this.form.username).then(function (result) {
            if (result) {
                // do sth.
                 // 为表单绑定验证功能
                _this.$refs[formName].validate((valid) => {
                    if (valid) {
                            console.log("22222222");  
                			console.log(_this.userInfo);
                        }
                    } else {
                    }
                });
            } else {
                // do other sth.
            }
        })
       
    }}
Changed results


Can vue do synchronization?

Recommended learning: "

vue video tutorial"

The above is the detailed content of Can vue do synchronization?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn