闪修小程序03-首页搜索组件

实现效果:搜索组件

1. 创建搜索区域样式

编辑pages/index/index.vue

<template>
	<view>
		<!-- 下面是搜索框 -->
		<view
			style="width: 750rpx;height: 90rpx;background-color: #00414B;display: flex; align-items: center;justify-content:space-around;">
			<view style="width: 600rpx; background-color: #fff;border-radius: 15rpx;">
				<input type="text" v-model="search_value"
					style="height: 50rpx; border-radius: 15rpx;box-sizing: border-box;padding-left: 30rpx;font-size: 23rpx;color:red;"
					maxlength="40" placeholder="搜索商品名,如:iphone12 pro max" />
			</view>
			<view
				style="width:120rpx;height: 50rpx;background-color:#ff4777; text-align: center;line-height: 50rpx;color: #fff;border-radius: 25rpx;"
				@click="search">搜索
			</view>
		</view>
		<!-- 上面是搜索框 -->
	</view>
</template>

2. data属性声明

data() {
  return {
    search_value: '' // 搜索框绑定的输入值
    }
}

3. 点击事件

methods: {	
    search() { // 搜索函数
        console.log("搜索的关键词是......", this.search_value);
        if (this.search_value.trim() == '') {
            uni.showToast({
                title: '请输入产品关键词进行搜索',
                icon: 'none',
                duration: 1000
            });
            return;
        }
        uni.setStorage({
            key: 'search_value',
            data: this.search_value
        });
        uni.navigateTo({
            url: '/pages/search_goods/search_goods?id=' + this.search_value
        });
    
    } // 搜索函数END
}

4. 创建搜索结果页

pages目录上右键创建页面search_goods

课后脑补知识点(flex 布局 & box 模型 & uni.showToast & uni.setStorage & uni.navigateTo)


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。
My Show My Code