-1

i would want to access the props value from another props validator.

Here is my code:

<script lang="ts" setup>
    import { PropType } from 'vue';
    import { iStep } from '@/interfaces/iStep.ts';
    import Step from './Step.vue'

    interface Props {
        steps: string[]|iStep[] ;
        currentStep: number;
    }

    
    const props = defineProps({
        steps: {
            type: [Array as PropType<iStep[]> , Array as PropType<string[]>],
            required: true
        },
        currentStep: {
            type: Number,
            validator: (value: number) => {
                return value >= 1 && value < this?.steps.length;
            }
        }
    });
    
</script>

In the validator this is always undefined and if i try to call it via props const i have an error defineProps() in <script setup> cannot reference locally declared variables because it will be hoisted outside of the setup() function.

I would want to restrict currentStep to be in range 0, props.steps.length.

Any idea how to achieve that ?

And did my steps props declaration type good ?

EDIT:

Access props value in another props' validator doesn't answer my question because in that case it use the option api instead of the api composition from vue (https://vuejs.org/guide/extras/composition-api-faq.html).

3
  • 2
    Does this answer your question? Access props value in another props' validator Commented Oct 31, 2023 at 13:35
  • No, i edit my quetion with the reason. Commented Oct 31, 2023 at 15:26
  • Option API or not, the answer is the same …. You can't access another prop in a validator from another prop. A way to solve your problem is to put your 2 props in the same object. That way, you can access it in your validator. Commented Oct 31, 2023 at 16:11

1 Answer 1

0

I would suggest putting the validator in a separate function and not mix it with values from a parent component:

const validator = (value: number) => value >= 1 && value < props.steps.length;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.