uniapp u-tabs表单如何默认选中

首先先了解该组件;该组件,是一个tabs标签组件,在标签多的时候,可以配置为左右滑动,标签少的时候,可以禁止滑动。 该组件的一个特点是配置为滚动模式时,激活的tab会自动移动到组件的中间位置。

基本使用:

  • 通过设置scrollable(默认为true),配置tabs组件的内容是否可以左右拖动,一般4个标签以下时,无需拖动,设置为false,5个标签以上,建议可以左右拖动。
  • tabs标签的切换,需要绑定current值,在change事件回调中可以得到index,将其赋值给current即可。

说明

scrollable参数很重要,如果您的tabs项只有几个,且不想tabs导航栏可以被左右滑动的话,请一定要设置scrollablefalse,因为它默认为true

先看一个示例代码

复制代码
<template>
    <u-tabs :list="list1" @click="click"></u-tabs>
</template>

<script>
	export default {
		data() {
			return {
                list1: [{
                    name: '关注',
                }, {
                    name: '推荐',
                }, {
                    name: '电影'
                }, {
                    name: '科技'
                }, {
                    name: '音乐'
                }, {
                    name: '美食'
                }, {
                    name: '文化'
                }, {
                    name: '财经'
                }, {
                    name: '手工'
                }]
			}
		},
		methods: {
            click(item) {
                console.log('item', item);
            }
		}
	}
</script>

效果如下

但如果想默认选中其中一个该如何设置呢?

只需要添加一个关键属性;

首先在return中声明一个变量:activeIndex:3 ;然后在点击事件里触发

this.activeIndex = item.index;

注:绑定属性的关键字为current 而不是active;如此便可实现

下面是效果

部分重要代码如下:

html 复制代码
		<u-tabs :list="list1" :current="activeIndex" @click="click"></u-tabs>
javascript 复制代码
				activeIndex: 0, // 默认选中第2个标签页
javascript 复制代码
async click(item) {
				this.activeIndex = item.index;
}

希望对你有所帮助

相关推荐
予你@。1 天前
UniApp + Vue3 实现 Tab 点击滚动定位(微信小程序)
微信小程序·小程序·uni-app
游戏开发爱好者81 天前
完整教程:App上架苹果App Store全流程指南
android·ios·小程序·https·uni-app·iphone·webview
予你@。2 天前
uni-app progress 组件使用详解
uni-app
iOS阿玮2 天前
春节提审高峰来袭!App Store 审核时长显著延长。
uni-app·app·apple
2501_916007472 天前
ios上架 App 流程,证书生成、从描述文件创建、打包、安装验证到上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106323 天前
iPhone 文件管理,如何进行应用沙盒文件查看
android·ios·小程序·https·uni-app·iphone·webview
2501_915921433 天前
Fastlane 结合 AppUploader 来实现 CI 集成自动化上架
android·运维·ci/cd·小程序·uni-app·自动化·iphone
云游云记3 天前
vue2 vue3 uniapp (微信小程序) v-model双向绑定
微信小程序·uni-app·vue
2501_915921433 天前
iOS 抓包怎么绕过 SSL Pinning 证书限制,抓取app上的包
android·网络协议·ios·小程序·uni-app·iphone·ssl
予你@。4 天前
uni-app(Vue3)实现自定义 Tab 切换滑块效果(微信小程序)
vue.js·微信小程序·uni-app