场景:pc端需要头部搜索,并支持回车搜索。如下:
html中:
<template>
<el-button
class="role-name-select"
type="primary"
@click="newItems()"
@keyup.enter.native="searchEnterFun"
>查询</el-button>
</template>
js中:
<script>
created() {
this.searchEnterFun();
},
methods: {
// 回车键监听
searchEnterFun(){
document.onkeyup = e =>{
// 为了更加严谨所以添加了路由路径的判断
if (e.keyCode === 13 && this.$route.path === '/menu/role') {
//回车后执行搜索方法
this.newItems();
}
}
},
// 查询方法
newItems(pages) {
}
},
</script>