基于vue实现多引擎搜索及关键字提示

yizhihongxing

实现基于Vue的多引擎搜索及关键字提示主要包括以下步骤:

  1. 构建基础页面结构

在HTML文件中,构造基础的页面结构,包括搜索框和搜索按钮等必要的组件。同时,引入Vue.js库和相关依赖。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>基于Vue的多引擎搜索及关键字提示</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  </head>
  <body>
    <div id="app">
      <h1>多引擎搜索及关键字提示</h1>
      <input type="text" v-model="searchText" v-on:input="search">
      <button v-on:click="search">搜索</button>
      <ul>
        <li v-for="engine in engines">
          <a v-bind:href="engine.searchUrl + encodeURIComponent(searchText)" target="_blank">{{ engine.name }}</a>
        </li>
      </ul>
    </div>
  </body>
</html>
  1. 定义搜索引擎数据

在Vue实例中,定义搜索引擎数据,包括引擎名称、搜索URL等相关信息,以数组形式存储。

var app = new Vue({
  el: '#app',
  data: {
    searchText: '',
    engines: [
      {
        name: '百度',
        searchUrl: 'https://www.baidu.com/s?wd='
      },
      {
        name: 'Google',
        searchUrl: 'https://www.google.com/search?q='
      }
    ]
  },
  methods: {
    search: function() {
      // 实现搜索逻辑
    }
  }
})
  1. 实现搜索逻辑

在Vue实例中,实现搜索逻辑,根据用户输入的搜索关键字以及选择的搜索引擎,生成对应的搜索URL,跳转到相应的搜索结果页面。此外,还可以利用第三方API获取搜索关键字的自动提示。

var app = new Vue({
  el: '#app',
  data: {
    searchText: '',
    engines: [
      {
        name: '百度',
        searchUrl: 'https://www.baidu.com/s?wd='
      },
      {
        name: 'Google',
        searchUrl: 'https://www.google.com/search?q='
      }
    ],
    suggestions: []
  },
  methods: {
    search: function() {
      if (this.searchText) {
        this.engines.forEach(function(engine) {
          window.open(engine.searchUrl + encodeURIComponent(this.searchText));
        }.bind(this));
      }
    },
    getSuggestions: function() {
      if (this.searchText) {
        var url = 'https://suggest.taobao.com/sug?code=utf-8&q=' + encodeURIComponent(this.searchText);
        axios.get(url)
          .then(function(response) {
            this.suggestions = response.data.result;
          }.bind(this))
          .catch(function(error) {
            console.log(error);
          });
      } else {
        this.suggestions = [];
      }
    }
  }
})
  1. 添加关键字提示功能

利用第三方API获取搜索关键字的自动提示,实现关键字提示功能。当用户输入搜索关键字时,通过AJAX异步加载关键字列表,并显示在页面上。

<div id="app">
  <h1>多引擎搜索及关键字提示</h1>
  <input type="text" v-model="searchText" v-on:input="getSuggestions" v-on:keyup.enter="search">
  <button v-on:click="search">搜索</button>
  <ul>
    <li v-for="suggestion in suggestions">
      <a v-bind:href="'https://www.baidu.com/s?wd=' + encodeURIComponent(suggestion[0])" target="_blank">{{ suggestion[0] }}</a>
    </li>
  </ul>
</div>

var app = new Vue({
  el: '#app',
  data: {
    searchText: '',
    engines: [
      {
        name: '百度',
        searchUrl: 'https://www.baidu.com/s?wd='
      },
      {
        name: 'Google',
        searchUrl: 'https://www.google.com/search?q='
      }
    ],
    suggestions: []
  },
  methods: {
    search: function() {
      if (this.searchText) {
        this.engines.forEach(function(engine) {
          window.open(engine.searchUrl + encodeURIComponent(this.searchText));
        }.bind(this));
      }
    },
    getSuggestions: function() {
      if (this.searchText) {
        var url = 'https://suggest.taobao.com/sug?code=utf-8&q=' + encodeURIComponent(this.searchText);
        axios.get(url)
          .then(function(response) {
            this.suggestions = response.data.result;
          }.bind(this))
          .catch(function(error) {
            console.log(error);
          });
      } else {
        this.suggestions = [];
      }
    }
  }
})
  1. 示例说明

下面是两个示例说明:

示例1:实现搜狗搜索引擎的支持

在Vue实例的数据部分添加搜狗搜索引擎的配置信息:

data: {
  searchText: '',
  engines: [
    {
      name: '百度',
      searchUrl: 'https://www.baidu.com/s?wd='
    },
    {
      name: 'Google',
      searchUrl: 'https://www.google.com/search?q='
    },
    {
      name: '搜狗',
      searchUrl: 'https://www.sogou.com/web?query='
    }
  ],
  suggestions: []
},

修改搜索逻辑部分,将搜索引擎根据用户选择的不同进行不同的跳转:

search: function() {
  if (this.searchText) {
    this.engines.forEach(function(engine) {
      if (engine.name === '搜狗') {
        window.open(engine.searchUrl + encodeURIComponent(this.searchText));
      } else {
        location.href = engine.searchUrl + encodeURIComponent(this.searchText);
      }
    }.bind(this));
  }
}

示例2:改变自动提示的来源为百度API

调用百度API获取关键字自动提示信息,并将结果展示在页面上。需要在getSuggestions方法里修改API的URL和参数:

getSuggestions: function() {
  if (this.searchText) {
    var url = 'http://suggestion.baidu.com/su';
    var params = {
      wd: this.searchText,
      cb: 'callback'
    };
    axios.jsonp(url, {params: params})
      .then(function(response) {
        this.suggestions = response.s;
      }.bind(this))
      .catch(function(error) {
        console.log(error);
      });
  } else {
    this.suggestions = [];
  }
}

同时,修改页面结构,展示百度API返回的结果:

<ul>
  <li v-for="suggestion in suggestions">
    <a v-bind:href="'https://www.baidu.com/s?wd=' + encodeURIComponent(suggestion)" target="_blank">{{ suggestion }}</a>
  </li>
</ul>

以上就是基于Vue实现多引擎搜索及关键字提示的完整攻略。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:基于vue实现多引擎搜索及关键字提示 - Python技术站

(0)
上一篇 2023年5月27日
下一篇 2023年5月27日

相关文章

  • vue3中reactive数据被重新赋值后无法双向绑定的解决

    在Vue3中,数据响应式的实现由Vue2中的Object.defineProperty改为了ES6的Proxy,使得Vue3的响应式系统在性能和功能上都有了提升。但是,在Vue3中,当我们直接给响应式对象中的属性进行重新赋值时,这个属性就会脱离响应式系统,从而失去了双向绑定的功能,即无法自动更新页面。下面给出两个示例,分别说明这个问题的存在及解决方式。 问题…

    Vue 2023年5月27日
    00
  • Vue3 组件库的环境配置搭建过程

    当我们想要使用 Vue3 开发组件库时,需要进行环境配置。 环境配置过程 以下是 Vue3 组件库的环境配置的完整攻略。 安装 Vue CLI Vue CLI 是一个工具,用于快速创建 Vue 应用程序。使用它可以很方便地创建一个新的 Vue 组件库项目。我们可以通过 npm 安装 Vue CLI: npm install -g @vue/cli 生成 Vu…

    Vue 2023年5月28日
    00
  • vue中jsonp的使用方法

    当前主流的前端框架之一就是Vue.js。在使用Vue.js时,有时我们需要使用JSONP(JSON with Padding)来解决跨域请求的问题。以下是关于Vue中JSONP的使用方法的完整攻略。 什么是JSONP? JSONP是一种跨域的请求方式。它通过动态添加<script>标签来实现跨域请求数据的方法。JSONP的实现过程如下: 在客户端…

    Vue 2023年5月28日
    00
  • Vue.js中使用iView日期选择器并设置开始时间结束时间校验功能

    下面是详细的“Vue.js中使用iView日期选择器并设置开始时间结束时间校验功能”的攻略: 1. 安装iView 要使用iView日期选择器,首先需要在Vue.js项目中安装iView UI库。可以使用npm进行安装: npm install iview –save 安装完成后,在Vue.js项目的main.js文件中引入: import iView f…

    Vue 2023年5月28日
    00
  • Vue.js每天必学之计算属性computed与$watch

    下面是详细讲解 Vue.js 中计算属性 computed 与 $watch 的攻略。 计算属性 computed 概念 计算属性是一种带有缓存功能的属性,它依赖于其它属性值,并且其返回值会根据这些被依赖的值变化而改变。计算属性除了需要响应式依赖,还可以拥有自己的缓存,只要它所依赖的属于没有发生改变,那么多次读取该计算属性时,只会进行一次计算。 用法 我们可…

    Vue 2023年5月29日
    00
  • Vue编译器AST抽象语法树源码分析

    一、概述 Vue是一个流行的JavaScript框架,通过它可以方便地构建可伸缩的单页面应用程序,能够帮助我们快速构建高效且易于维护的Web应用系统。在Vue中,模板被转换成一个抽象语法树(AST),这个AST是通过Vue编译器(parser)将模板代码转换而来的。AST通过获取DOM节点,识别出语言特定的标记和元素,自动创建了一个基于对象表示代码的结构树。…

    Vue 2023年5月27日
    00
  • vue webpack打包优化操作技巧

    下面是关于Vue Webpack打包优化的完整攻略。 为什么需要打包优化? Vue Webpack打包过程通常比较复杂,对于大型项目而言,打包过程中可能会遇到各种各样的问题。我们需要对Webpack打包进行优化,以提高项目的性能和稳定性。通常需要考虑以下几个方面: 减少打包体积 减少打包时间 提高页面加载速度 稳定可靠性 打包优化操作技巧 1. 优化load…

    Vue 2023年5月28日
    00
  • 详解vue移动端项目代码拆分记录

    详解vue移动端项目代码拆分记录 背景 随着vue项目的不断壮大,代码量越来越大,单一入口文件会变得越来越复杂,代码的维护和管理也越来越困难,因此我们需要对vue项目进行代码拆分。 代码拆分方案 我们可以将一个大的vue项目拆分成多个子项目,然后通过webpack构建工具自动化集成,这样项目结构更为清晰,维护和管理也更加轻松。 以下是代码拆分方案的具体实现步…

    Vue 2023年5月27日
    00
合作推广
合作推广
分享本页
返回顶部