uniApp学习(8)搜索框的创建和自动获取焦点

1、跳转到搜索页面功能

点击搜索跳转,显示热门搜索和,搜索历史
在这里插入图片描述
结果如下所示
在这里插入图片描述
1、创建搜索页search.vue
配置pages.json页面配置app-plus导航搜索页面(这个只针对h5和APP有用)

{
			"path": "pages/search/search",
			"style": {
				"navigationBarTitleText": "",
				"app-plus": {
					"bounce": "none", //关闭回弹效果
					"titleNView": {
						"softinputNavBar":"none",
						 "autoBackButton": false, //隐藏左侧后退按钮(APP) 
						"searchInput": {
							"align": "left",
							"placeholder": "搜索你想要的内容",
							"borderRadius": "30rpx",
							"backgroundColor": "#f0f1f2"
						},
						"buttons": [{
							"float": "right",
							"text": "取消",
							"fontSize": "16",
							"color": "#2A333B"
						}]
					}
				}
			}

效果如下所示
在这里插入图片描述
2、小程序从index.vue跳转到search.vue的方法

在首页index.vue中的添加 @click.native=“navTo(’/pages/search/search’)”

<template>
	<view >
		<!-- #ifdef MP -->
		<!-- 搜索框在小程序中显示 -->
		<search-input @click.native="navTo('/pages/search/search')"></search-input>
		<!-- #endif -->
	</view>
</template>

3、App跳转到search.vue的方法
onNavigationBarSearchInputClicked这个是App端监听输入框被点击时候触发的方法,直接在index.vue下面添加,与methods同级

// this.navTo因为mixin.js在main.js中全局引入
		// 采用this 因为是在当前的Vue实例里面的
		onNavigationBarSearchInputClicked() {
			// console.log("-------")
			// uni.navigateTo({
			// 	url:'/pages/search/search'
			// })
		     this.navTo('/pages/search/search')
		
},

navTo是全局引入的方法

navTo(url, options={}){
			if(!url){
				return;
			}
			if(options.login && !this.$store.getters.hasLogin){
				url = '/pages/auth/login';
			}
			uni.navigateTo({
				url
			})
		},

4、在在category.vue 分类页面进入搜索页面的方法
在这里插入图片描述
onNavigationBarButtonTap监听原生导航按钮点击的方法

// 监听原生导航按钮事件
onNavigationBarButtonTap(e) {
	// 搜索按钮
	if(e.index === 0) {
		// 在 /common/mixin/mixin.js 中定义了
		this.navTo('/pages/search/search')
	}
},`

5、category.vue 标签名进入搜索页面,methods 选项的 clickLabel 实现跳转
在这里插入图片描述

// 点击标签,跳转搜索页面
clickLabel(item) {
// 注意:labelId放到第1个位置,后面解析时要用,顺序一定不能乱
// activeIndex 选中分类下标,方便父组件重新选择分类时,默认选中当前分类
	const params = { labelId: item.id, name: item.name, activeIndex:this.activeIndex}
		uni.navigateTo({
			url: `/pages/search/search?params=${ JSON.stringify(params) }`
		})
}

2、在search.vue加载页面onload中接收参数,如果传递过来有值,说明来自

在这里插入图片描述
没有值说明来自index输入框或者查询按钮,则需要获取焦点

onLoad(option) {
			// #ifdef APP-PLUS
			//此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用    plus.webview.currentWebview()无效,非v3编译模式使用this.$mp.page.$getAppWebview()
			currentWebview = this.$scope.$getAppWebview();
			// #endif

			// 如果有参数则不用自动获取焦点
			if (option.param) {
				console.log("option", JSON.parse(option.param))
				this.param = JSON.parse(option.param)
				this.doSearch()
			}
			// 没有参数则自动获取焦点 
			else {
				// #ifdef APP-PLUS
				console.log("自动获取焦点")

				currentWebview.setTitleNViewSearchInputFocus(true)
				// #endif
				// #ifdef MP
				// 小程序获取焦点
				this.focus = true
				// #endif
			}
		},

3、兼容小程序搜索框

  1. 创建官方提供的模板项目,点击HuilderX菜单栏的:文件》新建》项目,打开新建项目窗口,如下操作:
  2. 在这里插入图片描述
  3. 拷贝 hello-uni-app 项目中的 /components/uni-search-bar/uni-search-bar.vue 搜索组件到我们项目中。
    在这里插入图片描述
  4. 将 uni-search-bar.vue 图标相关的注释掉,替换 text 标签以类名方式引入对应图标,可在 css 中将边框去
    除和高度调整。
    在这里插入图片描述
    在 search.vue 中引用 uni-search-bar.vue 组件
    注意:
    如果组件是在 /components 目录下,且 /components 的子目录名和组件名相同时,可以不需要
    import 导入它。
    如上面 uni-search-bar.vue 目录结构即可省略 import ,直接在模板代码中使用这个组件即可。
    doSearch(obj)方法,添加 obj 参数,接收一个输入框的数据对象 {value: ‘xxxx’}

下面的就是小程序显示的搜索框和取消

<template>
<view class="search-container">
<!-- #ifdef MP -->
		<!-- 小程序搜索框, 不要少了 ref="searchBar" 后面要用 -->
		<!-- 注意:navBack() 不要少了括号,不然后退不了(mixin.js定义了) -->
		<uni-search-bar ref="searchBar" radius="100" placeholder="搜索你想要的内容"
		clearButton="auto"
		cancelButton="always" @confirm="doSearch" @cancel="navBack()" />
<!-- #endif -->
</view>
</template>

在这里插入图片描述
小程序uni-search-bar 采用this.$emit方式向父组件传递输入框的值

this.$emit("confirm", {
					value: this.searchVal
})

@confirm="doSearch"调用父组件doSearch的方法

<script>
	// 当前页面实例变量,声明外面提高性能
	let currentWebview = null
	export default {
		data() {
			return {
				params: null, // 其他页面跳转到此页面带上的参数,
				content: null, //搜索内容
			}
		},
		//...,
		methods() {
			// 搜索
			doSearch(obj) {
				// obj有数据,则获取
				this.content = obj && obj.value ? obj.value : this.content
				console.log(this.content)
				uni.showLoading()
			},
		},
	}
</script>

4、小程序搜索框自动获取焦点

  1. 小程序进入搜索页面,搜索框自动获取焦点。
    实现此功能,要在 uni-search-bar.vue 的 props 声明 focus 属性,并添加针对 focus 添加一个监听器来设
    置焦点
	props: {
			focus: { // 是否获取焦点 +++
				type: Boolean,
				default: false
			},
			
		},

		watch: {
			// 设置焦点 ++++
			focus: {
				handler(newVal) {
				
					if (newVal) {
						// 获取焦点,
						this.searchClick()
					}
				},
				immediate: true
			},

		},
  1. 修改 search.vue 页面中的传递获取焦点属性: focus = “focus”,data 选项中声明 focus 属性, onLoad赋值中赋值true真机预览才有效果…
    当onload 没有参数则ifdef MP中小程序判断,设置focus为true触发焦点
<template>
</template>
<!-- focus 是否获取焦点 -->
<uni-search-bar :focus="focus" radius="100" placeholder="搜索你想要的内容" clearButton="auto" cancelButton="always" @confirm="doSearch"
 @cancel="navBack()" />

<script>
	data() {
			return {
				params: null, // 其他页面跳转到此页面带上的参数,
				content: null, //搜索内容
				focus: false, //搜索框获取焦点
			}
		},

		onLoad(option) { // option接收其他页面传递过来的参数
			// #ifdef APP-PLUS
			currentWebview = this.$mp.page.$getAppWebview()
			// #endif

			// 获取其他页面跳转过来带的参数,
			if (option.params) {
				// 转换对象
				this.params = JSON.parse(option.params)
				console.log('params', this.params)
				// 有参数,则开始搜索
				this.doSearch()
			} else {
				// #ifdef APP-PLUS
				//点击搜索框获取焦点, 因为分类页会跳转到此页面后不用获取焦点,所以不在pages.json
				中开启自动获取焦点
				currentWebview.setTitleNViewSearchInputFocus(true)
				热门与历史关键字提示组件
				1. 需求: 进入搜索页面后, 显示热门搜索和历史搜索关键字, 带历史搜索带清空按钮, 如下图
				2. 创建 / pages / search / components / keyword.vue 关键词页面, 其中模板代码如下
				// #endif

				// #ifdef MP
				// 页面加载即获取焦点,真机测试才有效 +++
				this.focus = true
				// #endif
			}
		},
</script>

<style>
</style>

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以在input组件上添加autofocus属性来自动获取焦点,例如: ``` <template> <view> <input type="text" autofocus placeholder="请输入内容" /> </view> </template> ``` 这样在页面加载时,该input组件就会自动获取焦点,用户可以直接输入内容。 ### 回答2: Uniapp是一款跨平台的开发框架,在移动端应用开发广受欢迎。在Uniapp的input组件可以实现自动获取焦点的功能,方便用户在输入内容时能够直接开始输入而不需要手动点击输入框。 要想实现input自动获取焦点,需要在input组件加入一个属性autofocus,该属性值为true时会自动获取焦点,为false时则不会。可以在组件直接写明该属性即可实现自动获取焦点。 例如,代码如下: ``` <template> <div> <input type="text" :autofocus="true" placeholder="请输入内容" /> </div> </template> ``` 在上述代码,input组件的autofocus属性设置为true,当页面打开时该组件会自动获取焦点,用户可以直接开始输入内容。需要注意的是,该属性只有在输入框出现时才会生效,如果输入框在页面被隐藏或者没有被渲染出来,则无法实现该功能。 除了直接在组件设置属性值,也可以使用动态绑定来实现input自动获取焦点。例如,代码如下: ``` <template> <div> <input type="text" :autofocus="isFocus" placeholder="请输入内容" /> </div> </template> <script> export default { data() { return { isFocus: true }; } }; </script> ``` 在上述代码,isFocus属性的值为true,input组件的autofocus属性绑定该属性,当isFocus为true时,input组件会自动获取焦点,当isFocus为false时,输入框则不会自动获取焦点。 总之,在Uniapp实现input自动获取焦点非常简单,可以直接设置属性或者使用动态绑定,方便用户在输入内容时能够直接开始输入而不需要手动点击输入框。 ### 回答3: 在Uniapp,我们可以通过v-focus指令来实现input自动获取焦点的功能。 具体实现步骤如下: 1. 在需要自动获取焦点的input标签上添加v-focus指令,例如: ```html <template> <div> <input v-focus /> </div> </template> ``` 2. 在对应的Vue组件定义v-focus指令,实现自动获取焦点的逻辑,例如: ```javascript <script> export default { directives: { focus: { // 指令的定义 inserted: function (el) { el.focus() } } } } </script> ``` 在上面的例子,通过定义inserted钩子函数,当该指令被插入到input元素上时,就会自动触发该函数,从而实现input自动获取焦点的效果。 需要注意的是,使用v-focus指令的input元素在页面渲染后会自动获取焦点,如果需要在页面手动触发失焦事件,可以通过Vue的事件绑定来实现,例如: ```html <template> <div> <input v-focus @blur="onBlur" /> </div> </template> <script> export default { methods: { onBlur: function () { console.log('失去焦点') } }, directives: { focus: { // 指令的定义 inserted: function (el) { el.focus() } } } } </script> ``` 通过添加@blur事件绑定,当input元素失去焦点时就会触发onBlur方法。这样就可以在需要手动失焦的情况下使用v-focus指令了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值