vue中input輸入框的模糊查詢實現


最近在使用vue寫webapp在,一些感覺比較有意思的分享一下。

1:input輸入框:

<input class="s-search-text" placeholder="猜你喜歡我們" id="s-search-text" ref="searchval" v-model="message" @keyup="search">

2:對input輸入框的keyup事件進行處理,通過每輸入一個字符觸發一次keyup事件,來對接口進行數據請求。

search () {
   let searchText = this.$refs.searchval.value
   if (searchText =='') {
       return
   } else {
       this.closeState = true
       this.searchState.showsug = true
       this.searchState.searchtext = this.$refs.searchval.value
       this.$emit('searchstate', this.searchState)
  }
  axios.get('http://localhost:3000/search/suggest?keywords=' + searchText, {}, {headers:{'Content-Type':'application/x-www-form-urlencoded'}})
      .then((res) => {
         if (res.data.code == 200) {
             this.$emit('search', res.data.result.allMatch)
         }
      })
      .catch((err) => {
         console.log(err)
       })
   },

  github地址:https://github.com/xu-jinkai/vue-music

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM