Home > Article > Web Front-end > Can vue do synchronization?
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.
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 methoddata() { 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 { } }); }}
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
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!