The issue of passing a prop to a child component in Vue 3 has not been resolved as expected

Within my code, I have a simple file structured like this:

<script setup>
import { ref } from 'vue'
import TheHeader from '@/components/_headerbar/TheHeader.vue'
import TheSidebar from '@/components/_sidebar/TheSidebar.vue'

const sidebarState = ref(false)

const onSidebarToggle = () => {
  sidebarState.value = !sidebarState.value
}
</script>

<template>
  <QLayout view="hHh lpR fFf"> 
    <TheHeader @toggle-sidebar="onSidebarToggle" />
    <TheSidebar :sidebar-state="sidebarState.value" />

    <QPageContainer>
      <RouterView v-slot="{ Component }">
        <component :is="Component" />
      </RouterView>
    </QPageContainer>
  </QLayout>
</template>

In this situation, the variable sidebarState updates correctly each time the event toggle-sidebar is triggered. However, for some reason, the prop that receives its value does not update and I'm unsure why.

Specifically, the issue lies within the TheSidebar.vue file:

<script setup>
const props = defineProps({
  sidebarState: {
    type: Boolean,
    default: true
  }
})
</script>

<template>
  <QDrawer
    :model-value="props.sidebarState"
    show-if-above
    side="left"
    bordered
  >
    content
  </QDrawer>
</template>

Upon debugging, it becomes apparent that the sidebarState prop in the TheSidebar.vue file never changes, even though the data prop sidebarState in TheHeader.vue adjusts perfectly fine. This discrepancy leaves me puzzled about what is going wrong.

I am at a loss as to what mistake I may be making. Any insights would be greatly appreciated.

Answer №1

Avoid using the .value attribute in templates when referencing top-level properties. Instead, refer to this helpful link that explains how values are automatically unwrapped for you (remember to switch from "Options" to "Composition" on the docs page for the link to work correctly).

By removing the .value attribute, your code should function as intended.

<TheSidebar :sidebar-state="sidebarState" />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing Nuxt 3 server as a passthrough API along with FormData for concealing external endpoints

I'm currently grappling with understanding the Nuxt/server API and am struggling to figure out how to properly send a POST request with form-data (specifically files) to the Nuxt server in order to forward it to an external service: Within my pages.v ...

Converting HTML elements into Vue.js Components

In my Vue.js application, I am utilizing a d3.js plugin to generate a intricate visualization. I am interested in implementing a customized vue directive to the components that were incorporated by the d3 plugin. It seems that the $compile feature, which ...

Ways to dynamically attach the main Vue component based on a condition?

In the context of using vue, is there a way to verify if the designated element for mounting the vue instance actually exists? I often encounter scenarios where this element may not be present and would like to prevent error messages such as: [Vue warn]: ...

Having trouble with integrating jsPlumb with Nuxtjs and encountering the error "document is not defined"?

I've seen some similar questions, but none of the solutions have worked for me so here's my issue. I'm attempting to integrate jsPlumb into my Nuxt.js/Vue.js application. Here are the steps I've taken: npm i jsplumb --save Create a ne ...

Error when trying to add style-loader in Vue.js 2 due to webpack installation issue

My task is to set up the style-loader in order to load import 'bootstrap-icons/font/bootstrap-icons.css'. However, when I run npm install style-loader, I encounter the following error message: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to re ...

Quick Tip: Enhancing your build with static assets and implementing cache busting techniques

I am facing an issue with my large static files that need to be hashed for HTTP caching in Vite. When I place them in the public directory, Vite only copies them without appending a hash to the filenames. However, if I put them in the assets directory, Vit ...

Flying around in every essential element within a Vue template

Recently, I made the switch to Typescript for Vue and decided to enable the Volar extension. However, after doing so, I noticed that every HTML intrinsic element (such as section and img) is now being flagged as an error: JSX element implicitly has type &a ...

tips for optimizing javascript file caching

https://i.stack.imgur.com/UhWD1.pngMy web application was created using "pug" technology about 9-8 years ago, and more recently, pages have been added in an innovative framework (vue.js). However, whenever there is a transition between an old pug page and ...

What steps can be taken to resolve the deprecated error for isVueInstance in vue.js2?

Utilizing vue-jest for test cases in vue.js2 involves working with a component named Register.vue. The test cases are written in Register.spec.js, and when running npm t, everything is functioning correctly. However, there are some errors being encountered ...

Instructions for showcasing a 404 error page in the event that a back-end GET request to an API fails due to the absence of a user. This guide will detail the process of separating the

I am currently working on an application that combines JavaScript with Vue.js on the front-end and PHP with Laravel on the back-end. When a GET request is made from the front-end to the back-end at URL /getSummoner/{summonerName}, another GET request is t ...

Using VueJS3 in conjunction with leaflet results in an issue

Looking to integrate interactive maps into my VueJS3 application. Successfully implemented the following code: <template> <div id="mapContainer"></div> </template> <script> import 'leaflet/dist/leaflet.css&a ...

Navigate to the Vuex Store to gain entry into the module

I have a module called ProfileData with the following structure: export const ProfileData = { state: { ajaxData: null; }, getters: {/*getters here*/}, mutations: {/*mutations here*/}, actions: {/*actions here*/} } This module ...

The <core.pid> file in Node.js is specifically generated on a single node

Running a high-availability architecture with two identical EC2 instances under a load balancer, I have a production workspace where the application is vue.js using 'nuxt' and daemonized with pm2. However, disk space on only one node keeps fillin ...

Avoiding substantial sections during execution of the command "Npm run build"

Encountering an issue when attempting to execute "npm run build" (!) Encountered chunks larger than 500 KiB after minification. Suggestions: - Implement dynamic import() for code-splitting the application - Utilize build.rollupOptions.output.ma ...

Grouping various event listeners within a v-for loop

My Desired Outcome In my Vue component, I am displaying a list of actions using v-for. Each action should have a corresponding @click event handler that triggers the action method within the component. I need help declaring these actions in my data() fun ...

Leveraging External Scripts with Vue.js and Laravel: A Comprehensive Guide

I am new to using vue js and laravel mix, and I am facing some confusion. Previously, I manually loaded scripts without compiling them. However, now that I want to integrate vue js into my project, adding the app.js script at the end of my blade template c ...

Modifying the Vue page background directly from a page: a step-by-step guide

UPDATE: I recently discovered that I need to modify the body background-color, but I am unsure of how to accomplish this from within the style of this page based on the wrapper's class. The class is determined by the data values. I aim to change the ...

Struggling with resolving a system error that popped up during the installation of Vue CLI in Node? You may have encountered the message "A system

After installing npm 9.2.0 and nodev16.16.0 on my Windows 7 system, I attempted to install vue cli but encountered an error message when trying to check the version of the vue cli. Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporati ...

"Need help passing an API key in the header of a Vue.js project? I recently encountered this issue while using a

How can I include an API key in the header of a Vue.js request? I am using DRF pagination. methods: { getPostData() { axios .get("http://192.168.43.126:8000/api/?page=" + this.currentPage, { headers: { &q ...

Encountered a Sass-loader error stating 'options has an unknown property 'indentedSyntax'' when attempting to upgrade Vuetify from version 1.5 to 2

I recently undertook the task of upgrading Vuetify in my Vue CLI 3 project from version 1.5 to 2. I diligently followed the provided guidelines, ensuring a complete installation. However, post-upgrade, whenever I run 'npm run serve', I am bombard ...