This article mainly introduces the problem that arrow functions cannot be used in vue data. This article introduces it to you in great detail through source code analysis and has certain reference value. Friends who need it can refer to it
First of all, you need to To be clear, a() {} and b: () => {}
are different
let obj = { a() {}, // 相当于 a:function() {}, b: () => {} }
##1 VUE .js source code analysis
Note that only the core code is designed hereThis code is also the UMD implementation principle. This article is not the focus. Those who are interested can explore it by themselves.(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.Vue = factory()); }(this, (function (){ 'use strict'; console.log(this) //*undefined* })))
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.Vue = factory()); }(this, (function (){ 'use strict'; function getData(data,vm) { return data.call(vm, vm) } function initData(params) { data = vm._data = typeof data === 'function' ? getData(data, vm) : data || {}; } initData() })))
var obj = { a: () => { 'use strict'; console.log(this,'a') }, b() { console.log(this,'b') }, c() { // window let e = () => { console.log(this,'e') } return e } } obj.a() // window obj.b() // obj obj.c()() // obj
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.Vue = factory()); }(this, (function (){ 'use strict'; let vm = {} var data = () => { console.log(this);//undefined return { a: 1 } } function getData(data,vm) { return data.call(vm, vm) } function initData(params) { data = vm._data = typeof data === 'function' ? getData(data, vm) : data || {}; } initData() console.log(vm); })))
data() {}this points to the Vm instance, so he will update with the instance.
About the method of changing the secondary menu of the Vue iview-admin framework to the third-level menu
About How to use the vue.js carousel component
The above is the detailed content of Vue data cannot use arrow function problem analysis. For more information, please follow other related articles on the PHP Chinese website!