This is"> Vue 3 Composition API: Render Props and v-if even if value is False-PHP Chinese Network Q&A
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
598

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:

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):

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

//-File1---------------------------------------  //-File2---------------------------------------  

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)
P粉976488015

On your export defaults,

props: { active: { type: Boolean, default: false } }

On your component template,

When using components, bind the active element to false

    P粉327903045

    This is because your prop is a string passed from the parent component (same as the default behavior of other HTML attributes). In order to pass a prop as a boolean, you need to use thev-bindsyntax or the:shorthand sofalsewill be parsed as aJavaScript expressioninstead of string:

    or

      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!