使用uniapp写一个只有年份的选择器
-
创建一个新的uniapp项目,选择uni-app。
-
在pages文件夹中创建一个新的页面,命名为yearPicker。
-
在yearPicker页面中添加一个选择器组件,代码如下:
<template>
<view class="content">
<picker mode="date" :start="startYear" :end="endYear" @change="onChange"></picker>
</view>
</template>
<script>
export default {
data() {
return {
startYear: new Date().getFullYear() - 50, // 从50年前开始选择
endYear: new Date().getFullYear() + 50, // 选择到50年后
}
},
methods: {
onChange(event) {
const year = event.detail.value.getFullYear() // 获取选择的年份
console.log(year)
}
}
}
</script>
- 在App.vue文件中添加一个链接到yearPicker页面的按钮,代码如下:
<template>
<view class="content">
<button @click="toYearPicker">打开年份选择器</button>
</view>
</template>
<script>
export default {
methods: {
toYearPicker() {
uni.navigateTo({
url: '/pages/yearPicker/yearPicker',
})
}
}
}
</script>
- 运行项目,点击按钮进入yearPicker页面,选择年份后在控制台输出选择的年份。
原文地址: https://www.cveoy.top/t/topic/4SC 著作权归作者所有。请勿转载和采集!