简体   繁体   English

Vue函数未更新数据变量

[英]Vue function not updating data variable

I have set up a page using Vue and Greensock draggable to try and make a rectangle svg object draggable on the screen. 我已经设置了使用Vue和Greensock可拖动的页面,以尝试使矩形svg对象在屏幕上可拖动。 I would like to know when the object has been dragged so I set a data variable hasDragged: false. 我想知道何时拖动对象,所以我设置了一个数据变量hasDragged:false。

Using addEventListener on dragstart I set up a function that will update that variable to true when it detects that it has been dragged however it only updates the variable within the function and not the data variable that I need. 我在dragstart上使用addEventListener设置了一个函数,当它检测到已拖动该变量时会将其更新为true,但是它仅更新函数中的变量,而不更新我需要的数据变量。 The function is within another function in the updated lifecycle hook so I wondering if it is an issue with not being able to update this.hasDragged from within the second function. 该功能在更新的生命周期挂钩中的另一个功能内,因此我想知道是否无法从第二个功能内更新this.hasDragged是一个问题。

I have tried many versions of the draggable addEventListener, trying to pass this through the functions, assign variables to this within each function, assigning the variable as a constant and a few other things. 我尝试了可拖动的addEventListener的许多版本,尝试通过函数传递此变量,在每个函数中为此分配变量,将变量分配为常量和其他一些东西。

new Vue({
      el: "#app",
      data: {
        hasDragged: false
      },
updated: function(hasDragged) {
        var petDrag = Draggable.create(".icon",{
                bounds:"#container"
              })[0];
              petDrag.addEventListener("dragstart", dragStart);     
              function dragStart () {            
              this.hasDragged = true; 
        }

The expected result is that the hasDragged variable at the Vue data level will be updated to true when the svg object is dragged. The actual result is that only the variable within the second function is updated to true but the Vue data variable remains false.

this inside function is not Vue instance. this内部函数不是Vue实例。 You can use arrow function for this: 您可以为此使用箭头功能:

new Vue({
  el: "#app",
  data: {
    hasDragged: false
  },
  updated: function () {
    var petDrag = Draggable.create(".icon", {
      bounds: "#container"
    })[0];
    petDrag.addEventListener("dragstart", () => {
      this.hasDragged = true
    });
  }
})

I'm a bit late to the party here but I just wanted to complement ittus' answer. 我在这里参加聚会有点晚,但我只是想补充ittus的答案。

All GSAP constructors have a very complete set of event callbacks and in any one of those you can specify the scope inside that particular callback, which means you can set what this will be in there without directly adding an anonymous function (not that there is anything wrong with it). 所有GSAP构造有一个非常完整的事件回调,并在那些中的任何一个,你可以指定特定的回调内的范围,这意味着你可以设置什么this将是有没有直接加入一个匿名函数(我不是说有什么错误)。 So in this case the code could be added in the Draggable constructor like this (I'm using $refs to get the actual DOM element in the code): 因此,在这种情况下,可以像这样将代码添加到Draggable构造函数中(我使用$refs获取代码中的实际DOM元素):

data: function () {
  return {
    blueDragged: false
  };
},
methods: {
  blueDragStarted: function () {
    this.blueDragged = true;
  },
},
mounted: function () {
  Draggable.create(this.$refs.blueElement, {
    type: "x, y",
    onDragStart: this.blueDragStarted,
    onDragStartScope: this // Vue component instance
  });
},

In this sample we take advantage of the context where the Draggable instance is being created. 在此示例中,我们利用了创建Draggable实例的上下文。 In that context this refers to the component instance and we pass that as a reference, ensuring that we have access to the component's state in the callback. 在这种情况下, this是指组件实例,我们将其作为参考传递,以确保我们可以在回调中访问组件的状态。

Again, there is actually nothing wrong with ittus' answer, just felt like complementing it with all the possibilities GSAP has to offer in this regard. 同样,ittus的回答实际上没有任何问题,只是感觉像是在GSAP在这方面提供的所有可能的补充。

You can see GSAP Draggable's documentation here: 您可以在此处查看GSAP Draggable的文档:

https://greensock.com/docs/Utilities/Draggable https://greensock.com/docs/Utilities/Draggable

Scroll down a bit to the section titled Config object properties 向下滚动到标题为Config object properties的部分

Live demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM