Home Web Front-end Vue.js How to implement search box in vuejs

How to implement search box in vuejs

Sep 24, 2021 pm 02:46 PM
vue.js search bar

How to implement the search box in vuejs: 1. Create components of the logo and search box parts; 2. Create a new empty Vue object; 3. Use "bus.$emit("change",index);" Just trigger the event and pass parameters.

How to implement search box in vuejs

The operating environment of this article: Windows 7 system, vue version 2.9.6, DELL G3 computer.

How to implement the search box in vuejs?

Implementing a simple search box based on Vue.js

I saw the exercises on github. After reading the code, practice it again yourself. Put the original address first. : https://github.com/lavyun/vue-demo-search

The main knowledge used is very simple, simple knowledge of vuejs2.0 is enough. The source code uses .vue to build and ES6, webpack packaging and so on. I am still relatively inexperienced, so I will first use a simple .js to write.

Look at the effect first

There are two components here, one component is the logo part and the other is the search box part.

html

html is very simple, just refer to two components.

1

2

3

4

5

6

7

8

9

<p id="app">

 <logo-picture></logo-picture>

 <search-panel></search-panel>

</p>

 

//js还要实例#app

var app = new Vue({

 el: "#app"

})

Copy after login

logo

Let’s analyze it first. First, an displays the image of the search engine. It should be responsive here. If you choose different search engine icons below, you will need to change them accordingly. So . When clicked on the back inverted triangle, the drop-down list will be displayed.
Then there is the drop-down box. If you want to have a transition effect, you need to wrap it in a If you want to have a hover effect, use data-driven thinking, which is to compare whether index and hoverindex are equal, and if they are equal, add class.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

Vue.component(&#39;logo-picture&#39;,{

 template :&#39; \

 <p 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>\

 </p>&#39;,

 data: function() {

 return {

 items: [{src:&#39;../src/assets/360_logo.png&#39;},{src:&#39;../src/assets/baidu_logo.png&#39;},{src:&#39;../src/assets/sougou_logo.png&#39;}],

 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;

 

 }

});

Copy after login

Drop-down box

Input needs to be bound in two directions, so v-model="keyword" must be bound, and keyboard events must be bound. @keyup, if you press enter, you will search, and if you go down or up, you will select the given return information list.
The details box below is similar to the drop-down list.
If you search, you mainly use $http.jsonp, and also use ES6 syntax? The fallback seems to be Promise's .then().

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

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;

 })

 }

})

Copy after login

The problem of communication between two brother components

If you change the search engine, should be replaced with the corresponding search engine. Here we need to create a new empty Vue object as the middle, because the two components are not in a parent-child relationship.

1

2

3

4

5

6

7

8

9

10

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;

})

Copy after login

Pay attention to this problem here. This in $on points to bus, so you must save this to reference search-panel.

Recommended learning: "vue tutorial"

The above is the detailed content of How to implement search box in vuejs. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In-depth discussion of how vite parses .env files In-depth discussion of how vite parses .env files Jan 24, 2023 am 05:30 AM

When using the Vue framework to develop front-end projects, we will deploy multiple environments when deploying. Often the interface domain names called by development, testing and online environments are different. How can we make the distinction? That is using environment variables and patterns.

Detailed graphic explanation of how to integrate the Ace code editor in a Vue project Detailed graphic explanation of how to integrate the Ace code editor in a Vue project Apr 24, 2023 am 10:52 AM

Ace is an embeddable code editor written in JavaScript. It matches the functionality and performance of native editors like Sublime, Vim, and TextMate. It can be easily embedded into any web page and JavaScript application. Ace is maintained as the main editor for the Cloud9 IDE and is the successor to the Mozilla Skywriter (Bespin) project.

Let's talk in depth about reactive() in vue3 Let's talk in depth about reactive() in vue3 Jan 06, 2023 pm 09:21 PM

Foreword: In the development of vue3, reactive provides a method to implement responsive data. This is a frequently used API in daily development. In this article, the author will explore its internal operating mechanism.

Explore how to write unit tests in Vue3 Explore how to write unit tests in Vue3 Apr 25, 2023 pm 07:41 PM

Vue.js has become a very popular framework in front-end development today. As Vue.js continues to evolve, unit testing is becoming more and more important. Today we’ll explore how to write unit tests in Vue.js 3 and provide some best practices and common problems and solutions.

A simple comparison of JSX syntax and template syntax in Vue (analysis of advantages and disadvantages) A simple comparison of JSX syntax and template syntax in Vue (analysis of advantages and disadvantages) Mar 23, 2023 pm 07:53 PM

In Vue.js, developers can use two different syntaxes to create user interfaces: JSX syntax and template syntax. Both syntaxes have their own advantages and disadvantages. Let’s discuss their differences, advantages and disadvantages.

A brief analysis of how vue implements file slicing upload A brief analysis of how vue implements file slicing upload Mar 24, 2023 pm 07:40 PM

In the actual development project process, sometimes it is necessary to upload relatively large files, and then the upload will be relatively slow, so the background may require the front-end to upload file slices. It is very simple. For example, 1 A gigabyte file stream is cut into several small file streams, and then the interface is requested to deliver the small file streams respectively.

Analyze the principle of Vue2 implementing composition API Analyze the principle of Vue2 implementing composition API Jan 13, 2023 am 08:30 AM

Since the release of Vue3, the word composition API has entered the field of vision of students who write Vue. I believe everyone has always heard how much better the composition API is than the previous options API. Now, due to the release of the @vue/composition-api plug-in, Vue2 Students can also get on the bus. Next, we will mainly use responsive ref and reactive to conduct an in-depth analysis of how this plug-in achieves this.

Let's talk about how to use the Amap API in vue3 Let's talk about how to use the Amap API in vue3 Mar 09, 2023 pm 07:22 PM

When we used Amap, the official recommended many cases and demos to us, but these cases all used native methods to access and did not provide demos of vue or react. Many people have written about vue2 access on the Internet. However, in this article, we will take a look at how vue3 uses the commonly used Amap API. I hope it will be helpful to everyone!

See all articles