1

I have a vue component called <PlanView/>, and I'm rendering this component conditionally:

<div v-if="show_plan" id="mainplan">
  <PlanView/>
</div>
<div class="icon" v-else>
  <font-awesome-icon icon="fa-solid fa-angles-right" @click="openPlan"/>
</div>
openPlan() {
    this.show_plan = true;
},

but I want the functionality to be called even if the component is not rendered, can you please advise me how can I do that? thanks in advance.

4
  • 2
    What do you mean by calling the functionality without rendering component? Commented Jan 9, 2023 at 14:42
  • @Nitheesh 'calling the functionality' in my case is to call the sub-components without parsing them on the GUI. Commented Jan 9, 2023 at 14:44
  • 1
    what does this "functionality" refer to? I mean reading your code, I can understand that it will show the icon if show_plan === false and show the PlantView if show_plan === true, and that if you click the icon it will show the component, but what exactly do you want here? can you explain more? Commented Jan 9, 2023 at 14:48
  • Could you please describe calling the sub-components more detailed? Your code is not full enough to understand your problem. Commented Jan 9, 2023 at 14:49

2 Answers 2

1

If you want the component to be renedered and not displayed, you can hide the visibility of the template inside the component rather than hiding the complete compoenent.

Pass a prop to PlanView to decide the template is to be rendered or not

<PlanView :showPlan="show_plan"/>

Accept the prop inside PlanView component like

defineProps({
    showPlan: {
        type: Boolean,
        required: false,
        default: false
    }
})

Render the template of PlanView only if the prop is satisfied. So the template of PlanView will be looking like

<template>
    <!-- Div or some wraper element -->
    <div v-if="showPlan"> 
        <!-- Rest of your template here -->
    </div>
</template>

OR

Simply use v-show on the wrapper so that the element will be loaded, but will not be displayed in the UI when the condition is false

<PlanView v-show="show_plan"/>
Sign up to request clarification or add additional context in comments.

1 Comment

for this use case, I think it's better to just use <PlantView v-show="show_plan"> imo
1

You can simply use v-show instead of v-if to provide the same functionality as the answer from @Nitheesh suggests.

<div v-show="show_plan" id="mainplan">
  <PlanView/>
</div>
<div v-show="!show_plan" class="icon">
  <font-awesome-icon icon="fa-solid fa-angles-right" @click="openPlan"/>
</div>

But I am not sure this is what you means by by calling the functionality.

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.