vue2.0踩坑之旅

初入vuejs,描述一些踩坑經(jīng)歷

  • methods方法不能使用es6的寫法來實現(xiàn)
data: {
   name: "子龍",
   newName: "xiaohei" 
},
methods: {
    changeName: () => {
      console.log(2222);
      this.name = '小紅';
      this.newName = '小白';
    }
  }

這樣寫的話,會發(fā)現(xiàn)數(shù)據(jù)不會發(fā)生變化,即不會觸發(fā)雙向綁定的事件的發(fā)生,更加嚴重的是,要是我們使用的是單文件的組件方式的話,會直接對應(yīng)不到相應(yīng)的數(shù)據(jù)

<script>
module.exports = {
  name: 'app',
  data () {
    return {
      items: [1,2,3,4],
      nextNum: 4
    }
  },
  methods: {
    randomIndex: () => {
      return Math.floor(Math.random()*this.items.length);
    },
    add: () => {
      this.items.splice(this.randomIndex(), 0, ++this.nextNum);
    },
    remove:  () => {
      this.items.splice(this.randomIndex(), 1);
    }
  }
}
截圖 2016-11-24 11時16分14秒.jpg

目前還沒有找到具體的問題所在,可能是webpack打包的時候有點問題,所以建議在寫methods方法的時候使用最基本的方法格式,即:

data: {
   name: "子龍",
   newName: "xiaohei" 
},
methods: {
    changeName: function () {
      console.log(2222);
      this.name = '小紅';
      this.newName = '小白';
    }
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容