首页 > 解决方案 > vue-jalali-moment 过滤器在 v-for 上无法正常工作

问题描述

我想要一个带有 jalai 年份格式的选择,我使用 'vue-jalali-moment' 。

<template>
    <div>                                                       
        <select class="select-year" v-model="year" v-select2>
            <option v-for="year in years" :value="year">
                @{{ year | moment("jYYYY")}}
            </option>
        </select>
    </div>
</template>
<script>
    import select2 from 'select2';
    export default {
        name: "Profile",      
        data(){
            return {
                year: '',
            }
        },
        mounted() {           
            $('.select-year').select2({
                width: '100%',
                minimumResultsForSearch: -1,
            });           
        },     
        computed : {
            years () {
                const year = new Date().getFullYear()
                return Array.from({length: year - 1900}, (value, index) => 1901 + index)
            }
        }
    }
</script>

但我有一个问题,输出是错误的:

<select>
    <option value="1901" data-select2-id="15">1348</option>
    <option value="1902" data-select2-id="16">1348</option>
    <option value="1903" data-select2-id="17">1348</option>
    <option value="1904" data-select2-id="18">1348</option>
    .
    .
    .
    <option value="2017" data-select2-id="131">1348</option>
    <option value="2018" data-select2-id="132">1348</option>
    <option alue="2019" data-select2-id="133">1348</option>
</select>

它只为任何公历年生成 1348。
为什么不能正常工作?

标签: vue.js

解决方案


过滤器需要一个日期,然后您可以创建一个日期数组:

return Array.from({length: year - 1900}, (value, index) => new Date(1901 + index, 1, 1) )

推荐阅读