vue的method方法如何将vm实例的this传进子函数中

在以下代码环境中应该如何修改seen值。已经试过这些方法
1、首先seen是可以用实例调用,代码中用this.seen表示。但是在下面情况下this指向已经发生改变,用this.seen无法改变
2、在函数外部将this赋值给that的方法也不行 不过我的用法可能有问题 请大神指点
3、用箭头函数。会报错Cannot read property 'seen' of undefined

<div>{{seen}}</div>


export default {

  data() {

    return {

      seen: '数据'

    }

  },

  components: {

  },

  methods: {

    a:function() {

       bulabulabula

       xmlhttp.onreadystatechange = function (that) {

          if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {

            xxx.seen = true 

            }

       }

    }

  }

}

查了好多资料都没找到方法求大神指点

至尊宝的传说
浏览 350回答 1
1回答

凤凰求蛊

export default {&nbsp; data() {&nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; seen: '数据'&nbsp; &nbsp; }&nbsp; },&nbsp; components: {&nbsp; },&nbsp; methods: {&nbsp; &nbsp; a:function() {&nbsp; &nbsp; &nbsp; &nbsp;const that = this;&nbsp; &nbsp; &nbsp; &nbsp;xmlhttp.onreadystatechange = function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;that.seen = true&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;//箭头函数&nbsp; &nbsp; &nbsp; &nbsp;//xmlhttp.onreadystatechange = () => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//&nbsp; &nbsp; this.seen = true&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //}&nbsp; &nbsp; &nbsp; &nbsp;//}&nbsp; &nbsp; }&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript