The Vue Cli3.0 event binding arrow function failed to get the this correctly

template code

methods:{
        onMouseDown:(e)=>{
            let count = 0;
            this.inDragging = true;//this
            window.addEventListener("mousemove",this.onDragging);
        },
}
Feb.24,2022

you can print the this pointing. Methods clearly written on the official website is not recommended to use the arrow function


reason: the pointing mechanism of the this of the arrow function

solution: do not use the arrow function

    onMouseDown(e) {
        let count = 0;
        this.inDragging = true; 
        window.addEventListener('mousemove',this.onDragging);
    },

The scope of this in the

arrow function is in parentheses,
solution:
1. Do not use the arrow function
2. Keep this outside the function


, .

methods:{
    onMouseDown(e){
        let count = 0;
        this.inDragging = true;//this
        window.addEventListener('mousemove',this.onDragging);
    }
}
Menu