鍍金池/ 問(wèn)答/HTML/ vue中使用loadsh實(shí)現(xiàn)實(shí)時(shí)檢索,使用箭頭函數(shù)報(bào)錯(cuò)

vue中使用loadsh實(shí)現(xiàn)實(shí)時(shí)檢索,使用箭頭函數(shù)報(bào)錯(cuò)

使用lodash插件實(shí)現(xiàn)input的實(shí)時(shí)檢索,報(bào)錯(cuò)

頁(yè)面代碼:

 <div class="search">
                <input type="text" v-model="condition" placeholder="搜索社區(qū)" v-on:input="lodashSearch">
                <span class="search-icon" @click.stop="getSearchCommunity">
                    <i class="iconfont icon-w_search_normal"></i>
                </span>
            </div>

使用npm下載ladash依賴后,引入lodash

import _ from 'lodash'

methods方法:

    //實(shí)時(shí)檢索
    
            lodashSearch: _.debounce(() => {
                this.getSearchCommunity(); // 添加debounce,防止頁(yè)面卡死
            }, 400),

報(bào)錯(cuò):

clipboard.png

后面不使用箭頭函數(shù),則程序正常,不報(bào)錯(cuò)

    lodashSearch: _.debounce(function(){
                console.log(1);
                this.getSearchCommunity(); // 添加debounce,防止頁(yè)面卡死
            }, 500),

但是我之前使用lodash做resize事件監(jiān)聽(tīng)時(shí),使用的是箭頭函數(shù),然后程序正常啊,
代碼如下:

 mounted: function () {
      let _ = require('lodash');
      // 插件地址:https://www.lodashjs.com/
      //http://www.css88.com/doc/lodash/#_debouncefunc-wait0-options
      window.onresize = _.debounce(() => {
        this.initPage(); // 添加debounce,防止頁(yè)面卡死      
      }, 400);
    },

請(qǐng)問(wèn)對(duì)于lodash,為什么使用箭頭函數(shù)有的地方正常,有的報(bào)錯(cuò)呢?

根據(jù)好心人的答案,去看了下文檔,文檔中寫(xiě):

clipboard.png
因此methods中不能使用箭頭函數(shù)

回答
編輯回答
誮惜顏

認(rèn)真看看文檔吧 https://cn.vuejs.org/v2/api/#...

2018年1月22日 04:15