vue使用ant-design-vue实现搜索下拉框并进行跳转

 main.js配置 :基本配置,引入ant-design-vue和对应的css样式

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'

Vue.config.productionTip = false
Vue.use(antd);
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

 配置App.vue,加载路由

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

router/index.js :路由配置

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '../components/HelloWorld'
import HtmlVue from '../views/HtmlVue'
import JsVue from '../views/JsVue'
import PythonVue from '../views/PythonVue'

Vue.use(Router)

let routes = [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
  {
    path: '/js',
    name: 'Javascript',
    component: JsVue
  },
  {
    path: '/html',
    name: 'Html',
    component: HtmlVue
  },
  {
    path: '/python',
    name: 'Python',
    component: PythonVue
  },
  ];

let router = new Router({
  routes: routes
});
export default router;

添加HelloWorld、HtmlVue、JsVue、PythonVue对应的vue文件

<template>
  <div class="hello">
    <a-select
      show-search
      :value="value"
      placeholder="请输入关键词"
      style="width: 200px"
      :default-active-first-option="false"
      :filter-option="false"
      :not-found-content="null"
      @search="handleSearch"
      @change="handleChange"
    >
      <a-icon slot="suffixIcon" type="search"></a-icon>
      <a-select-option v-for="d in data" :key="d.value">
        {{ d.text }}
      </a-select-option>
    </a-select>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {
      data: [],//搜索事件
      value: undefined,
    };
  },
  methods: {
    // 输入框搜索,相当于watch监听该搜索框内容,触发的事件函数
    handleSearch(keyword) {
      //模拟往服务器发送请求
      this.ajax(keyword);
    },
    // 点击某个搜索结果时触发的事件
    handleChange(value) {
      console.log(value);
      let path = "/"+value;
      //跳转到不同的路由
      this.$router.push(path)
    },
    //模拟发送请求返回数据
    ajax(keyword){
      var that=this;
      that.data = [];
      let temp = ["js","html","python"];
      let i = 0;
      temp.forEach((item) => {
        that.data.push({
          value: item,
          text:item+i,
        });
        i++;
      });
    }
  },
}
</script>
<template>
<h1>我是html</h1>
</template>

<script>
export default {
name: "HtmlVue"
}
</script>

<style scoped>

</style>
<template>
  <h1>我是js</h1>
</template>

<script>
export default {
name: "JsVue"
}
</script>

<style scoped>

</style>
<template>
  <h1>我是python</h1>
</template>

<script>
export default {
name: "PythonVue"
}
</script>

<style scoped>

</style>

展示效果图片展示:

 通过选择不同的选项就可以跳转到对应的路由

 

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值