首頁  >  文章  >  web前端  >  vuejs怎麼實作搜尋框

vuejs怎麼實作搜尋框

藏色散人
藏色散人原創
2021-09-24 14:46:215328瀏覽

vuejs實作搜尋框的方法:1、建立logo和搜尋框部分的元件;2、新建一個空的Vue物件;3、使用“bus.$emit("change",index);”觸發事件並傳遞參數即可。

vuejs怎麼實作搜尋框

本文操作環境:Windows7系統、vue2.9.6版,DELL G3電腦。

vuejs怎麼實作搜尋框?

基於Vue.js實作簡單搜尋框

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

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

先看效果

這裡有兩個元件,一個元件是logo部分的,一個是搜尋框部分的。

html

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

<p id="app">
 <logo-picture></logo-picture>
 <search-panel></search-panel>
</p>

//js还要实例#app
var app = new Vue({
 el: "#app"
})

logo

先來分析,首先一個6ed09268cbdd0015bce8dcbbdfa9bfe4顯示搜尋引擎的圖片,這裡要響應式的,下面選擇了不同的搜尋引擎圖示就要跟著換。所以e431342b2894783f54b33740f33c93c1。後面的倒三角點擊時顯示下拉列表914942ce123cf770f7ea92723ad87555 54bdf357c58b8a65c66d7c19c8e4d114。
然後是下拉框。如果想要有過渡效果,那個就要包裹在7bf3d91ef4f4c2c2d44ead37da9cac3c下拉清單差不多。
搜尋的話主要是運用$http.jsonp,還有ES6的語法?回掉好像是Promise的.then()。

Vue.component(&#39;search-panel&#39;,{
 template:&#39;\
 <p 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">&times;</span>\
 <button @click="searchInput()" class="search-btn">搜一下</button>\
 <p 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>\
 </p>\
 </p>&#39;,
 data: function() {
 return {
 search: &#39;&#39;,
 myData: [],
 flag: 0,
 now: -1,
 logoData: [
 {
  &#39;name&#39;: "360搜索",
  searchSrc: "https://www.so.com/s?ie=utf-8&shb=1&src=360sou_newhome&q="
 },
 {
  &#39;name&#39;: "百度搜索",
  searchSrc: "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd="
 },
 {
  &#39;name&#39;: "搜狗搜索",
  searchSrc: "https://www.sogou.com/web?query="
 }
 ]
 }
 },
 methods: {
 get: function(event) {
 if(event.keyCode == 38 || event.keyCode == 40){ //向上向下
 return ;
 }
 this.$http.jsonp(&#39;https://sug.so.360.cn/suggest?word=&#39; + this.search + &#39;&encodein=utf-8&encodeout=utf-8&#39;).then(function(res) {
 this.myData = res.data.s;

 }, function() {

 });
 },
 //清除内容
 clearInput: function() {
 this.search = &#39;&#39;;
 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(&#39;change&#39;,function(index) {
 self.flag = index;
 })
 }
})

兩個兄弟組件通訊的問題

fd098b03a0046905b4f34a14f2cba3c4換了搜尋引擎的話,04a43090e6cbf1462a77fdb864c1f813要換成相應的搜索引擎。這裡要新建一個空的Vue物件做中間,因為兩個元件不是父子關係。

var bus = new Vue();

//logo-picture里触发事件,并传递参数
bus.$emit("change",index);

//search-panel里监听事件是否发生
var self = this;
bus.$on(&#39;change&#39;,function(index) {
 self.flag = index;
})

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

推薦學習:《vue教程

以上是vuejs怎麼實作搜尋框的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn