Vue 3 Composition API: Render Props and v-if even if value is False
P粉786432579
P粉786432579 2023-11-05 14:48:16
0
2
482

I've run into a problem here that I feel like I don't really understand. I include a child component that is passed a prop called "active" which can be set to true or false. The idea is that if "true" is passed, then part of the component will be displayed, if "false" is passed, it will not be displayed.

From my understanding, I should be able to just use the prop name, like this:

<template>
   <div v-if="active">这是active的值:{{active}}</div>
</template>

The problem is that if I directly set the v-if in the above statement to true or false, then it works as expected. If I pass it in as a prop, whether true or false, it always displays.

Valid (display nothing):

<template>
   <div v-if="false">这是active的值:{{active}}</div>
</template>

Invalid (no matter what the value of active is, the content in the div will be displayed):

//-File1---------------------------------------

<template>
   <myComponent active=false />
</template>

//-File2---------------------------------------

<template>
   <div v-if="active">这是active的值:{{active}}</div>
</template>

<script>
    export default{
        props:['active']
    }
</script>

Why is this? I confirmed it by showing the value of "active" and it was passed the value false, but it still renders despite the value being false. Am I missing something here? I've tried with quotes, without quotes, using ref to pass it a local value and using it:

import { ref } from 'vue';

export default{
    props:['active']
    setup(props,ctx){
        const active = ref(props.active);
        return {
            active
        }
    }
}

This didn't work either.

P粉786432579
P粉786432579

reply all(2)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!