基於Vue.js實現簡單搜尋方塊_javascript技巧

來源:互聯網
上載者:User

在github上看到的練習,看個遍代碼後自己再練一遍,先放原址:https://github.com/lavyun/vue-demo-search

主要用到的知識很簡單,簡單的vuejs2.0的知識就夠了。源碼用了.vue構建和ES6,用了webpack打包等等。我資曆還淺,先用一個簡單的.js的寫。

先看效果

這裡有兩個組件,一個組件是logo部分的,一個是搜尋方塊部分的。

html

html很簡單,就是引用兩個組件。

<div id="app"> <logo-picture></logo-picture> <search-panel></search-panel></div>//js還要執行個體#appvar app = new Vue({ el: "#app"})

logo

先來分析,首先一個<img />顯示搜尋引擎的圖片,這裡要響應式的,下面選擇了不同的搜尋引擎表徵圖就要跟著換。所以<img :src="items[now].src" />。後面的倒三角點擊時顯示下拉式清單<span @click="toggleFlag"> </span>。
然後是下拉框。如果想要有過渡效果,那個就要包裹在<transition-group中,然後遍曆li,記住元素要指定唯一的key。
想要有hover效果的話,用資料驅動的思維,就是比較index 與hoverindex是否相等,如果相等就加class。

Vue.component('logo-picture',{ template :' \ <div class="main-logo">\  <img :src="items[now].src" @click="toggleFlag"/>\  <span @click="toggleFlag" class="logoList-arrow"> </span>\  <transition-group tag="ul" v-show="flagShow" class="logoList">\   <li v-for="(item,index) in items" :key="index" @click="changeFlag(index)" @mouseover="flagHover(index)" :class="{selectback: index == hoverindex}">\    <img :src="item.src" />\   </li>\  </transition>\ </div>', data: function() {  return {   items: [{src:'../src/assets/360_logo.png'},{src:'../src/assets/baidu_logo.png'},{src:'../src/assets/sougou_logo.png'}],   now: 0,   flagShow: false,   hoverindex: -1   } }, methods: {  //顯示隱藏圖片列表  toggleFlag: function() {   this.flagShow = !this.flagShow;  },  //改變搜尋引擎  changeFlag: function(index) {   this.now = index;   this.flagShow = false;   bus.$emit("change",index);  },  //li hover  flagHover: function(index) {   this.hoverindex = index;  }       }});

下拉框

input因為要雙向繫結,所以要v-model="keyword",還要綁定鍵盤事件@keyup,如果按enter就搜尋,向下向上就選中給定的返回資訊列表。
下面的詳情框與<logo-picture>下拉式清單差不多。
搜尋的話主要是運用$http.jsonp,還有ES6的文法?回掉好像是Promise的.then()。

Vue.component('search-panel',{ template:'\ <div class="search-input">\  <input v-model="search" @keyup="get($event)" @keydown.enter="searchInput()" @keydown.down="selectDown()" @keydown.up.prevent="selectUp()"/>\  <span @click="clearInput()" class="search-reset">×</span>\  <button @click="searchInput()" class="search-btn">搜一下</button>\  <div class="search-select">\   <transition-group tag="ul" mode="out-in">\    <li v-for="(value,index) in myData" :class="{selectback:index==now}" :key="index" @click="searchThis" @mouseover="selectHover(index)" class="search-select-option search-select-list">\     {{value}}\    </li>\   </transition-group>\  </div>\ </div>', data: function() {  return {   search: '',   myData: [],   flag: 0,   now: -1,   logoData: [    {     'name': "360搜尋",     searchSrc: "https://www.so.com/s?ie=utf-8&shb=1&src=360sou_newhome&q="    },    {     'name': "百度搜尋",     searchSrc: "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd="    },    {     'name': "搜狗搜尋",     searchSrc: "https://www.sogou.com/web?query="    }   ]  } }, methods: {  get: function(event) {   if(event.keyCode == 38 || event.keyCode == 40){ //向上向下    return ;   }   this.$http.jsonp('https://sug.so.360.cn/suggest?word=' + this.search + '&encodein=utf-8&encodeout=utf-8').then(function(res) {    this.myData = res.data.s;   }, function() {   });  },  //清除內容  clearInput: function() {   this.search = '';   this.get();  },  //搜尋  searchInput: function() {   alert(this.flag)   window.open(this.logoData[this.flag].searchSrc+this.search);  },  //搜尋的內容  searchThis: function(index) {   this.search = this.myData[index];   this.searchInput();  },  //li hover  selectHover: function(index) {   this.search = this.myData[index];   this.now = index;  },  //向下  selectDown: function() {   this.now++;   if(this.now == this.myData.length) {    this.now = 0;   }   this.search = this.myData[this.now];  },  //向上  selectUp: function() {   this.now--;   if(this.now == -1) {    this.now = this.myData.length - 1;   }   this.search = this.myData[this.now];  } }, created: function() { //通訊  var self = this;  bus.$on('change',function(index) {   self.flag = index;  }) }})

兩個兄弟組件通訊的問題

<logo-picture>換了搜尋引擎的話,<search-panel>要換成相應的搜尋引擎。這裡要建立一個空的Vue對象做中間,因為兩個組件不是父子關係。

var bus = new Vue();//logo-picture裡觸發事件,並傳遞參數bus.$emit("change",index);//search-panel裡監聽事件是否發生var self = this;bus.$on('change',function(index) { self.flag = index;})

這裡要注意this問題,$on裡this指向bus,所以要儲存this才能引用search-panel.

本文已被整理到了《Vue.js前端組件學習教程》,歡迎大家學習閱讀。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.