Utilizing Vue to retrieve a computed property from a child component

I am currently implementing a third-party Vue component called 'vue-tree-list', which can be found at this link.

Within the component, there is a computed property that analyzes the tree structure to determine the correct placement for inserting a new leaf/node.

In my parent component, I have done the following:

<template>
  <div class="py-8 px-5" style="min-height: calc(100vh - (112px + 2.75rem))">
    <div class="flex flex-col w-full">
      <button class="cursor-pointer relative flex flex-row items-center h-10 focus:outline-none ">
        <span class="text-sm tracking-wide truncate ml-6">Add Node</span>
      </button>
    </div>
    <VueTreeList
     @click="onClick"
     @change-name="onChangeName"
     @delete-node="onDel"
     @add-node="onClick"
     ref="tree"
     :model="data"
     default-tree-node-name="New Depot"
     default-leaf-node-name="New Driver"
     v-bind:default-expanded="false"
     >
      <template v-slot:leafNameDisplay="slotProps">
        <a class="text-orange-primary mr-4">
           <span>{{ slotProps.model.name }}</span>
        </a>
      </template>
      <span class="icon" slot="addTreeNodeIcon">📂</span>
      <span class="icon" @click.stop="test()" slot="addLeafNodeIcon">+</span>

       ^INSTEAD OF CALLING THE DEFAULT EVENT 'add-child' WHICH IMMEDIATELY INSERTS A NODE I DIVERTED IT INSTEAD SINCE I WANT THE USER TO INPUT THEIR DATA BEFORE INSERTING INSIDE THE TREE

      <span class="icon" slot="editNodeIcon">📃</span>
      <span class="icon" slot="delNodeIcon">✂️</span>
      <span class="icon" slot="leafNodeIcon">🍃</span>
      <span class="icon" slot="treeNodeIcon">📂</span>
    </VueTreeList>
      <Modal ref="modal" :title="modalTitle" :size="modalSize" :height="modalHeight">
        <div v-if="modalContent == 'new'">
          <DriverLookUp />
          <VehicleLookUp />
        </div>
      </Modal>
  </div>
  
</template>

<script>
import { VueTreeList, Tree, TreeNode } from 'vue-tree-list'
import { DriverLookUp, VehicleLookUp } from '@/components/forms/depot'

export default {
    components: {
      VueTreeList, Modal, DriverLookUp, VehicleLookUp
    },
      test(){
         this.$refs.tree.rootNode() <--- the computed method that I want to access 
      },
}...

The issue arises when the computed property encounters missing properties error even though it has already been rendered. Is there a way to trigger a child component's computed property?

Here is the link to the child component that I'm working with: link.

Answer №1

The computed property in that component searches upwards through its parent hierarchy to locate a component containing a prop named model with a value of "root". If it encounters a parent without this prop, it will result in the error you witnessed.

To work around this issue, you can explicitly define the model property in your component before accessing the computed property:

export default {
  props: {
      👇
    model: {
      type: Object,
      default: () => ({ name: 'root' }),
    },
  },
  methods: {
    test() {
      const rootNode = this.$refs.tree.rootNode
      console.log({ rootNode })
    },
  }
}

Check out the demo here.

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

Limiting the textfield to 100 characters while allowing special characters in React.js

I am working with a text field that can accept the following types of values: >35% <=60% =55% =100% My goal is to set the maximum limit of the numbers to 100. How can this be achieved? Below is the code for the textfield in question: <TextField ...

How can I prevent the Firefox pop-up stating "This web page is being redirected to a new location" from appearing with every requestPromise AJAX call?

My web app has extensive AJAX functionality. While initially working fine in my tests, today I encountered an issue with Firefox on both Mac and IE where a pop-up message "This web page is being redirected to a new location" appears for every PUT and DELET ...

What is the process for integrating custom fields into a product using Stripe, and how can a stock limit be implemented for each customized field?

Currently, as I develop an ecommerce website using Next.js and integrate Stripe for checkout, I've come across the feature of custom fields in Stripe. This feature allows me to add options such as small, medium, and large for clothing sizes. However, ...

Issues with the functionality of AngularJS checkboxes

I'm currently working on a basic AngularJS project to improve my skills. Below are the code snippets I've included. I have two sets of JSON data. One set contains a list of grocery items, while the other set includes the items selected by the us ...

What could have caused my javascript file to disappear from npm?

After creating a small library consisting of a .js file with commonly used functions, I placed it in the node_modules directory alongside my other packages. Everything seemed to be going well. A few days later, I decided to add a new package using npm ins ...

What is a straightforward method to display one Div while concealing all others?

Is there a simpler method to display one div and hide all others, with the first one shown by default? My current solution using jQuery works, but it feels lengthy. I believe there might be a more efficient way to achieve this. Here is the code snippet: ...

Navigating data in a Json array object

Can someone assist me in retrieving data from a JSON array file stored at the following link? Html <div> <div v-for="data in myJson.id " >{{ data }}</div> </div> js import json from '.././json/data.json&apo ...

JavaScript does not support clicking on an iframe

Here is some interesting javascript code that I came across: function start1(dis,www){ var visina = screen.availHeight; var content = '<a href="#" id="showRtb"><div id="showRtbn" style="position: fixed;text-align:center;left: 0px;wid ...

Node.js UDP broadcast to subnet not working properly in Linux

Here is a simplified Node JavaScript code snippet for testing purposes. Click here to view the code on GitHub This code creates a socket for each interface address, calculates the subnet broadcast address, and then sends data every second for 4 seconds t ...

Is there a smooth and effective method to implement a timeout restriction while loading a sluggish external file using JavaScript?

Incorporating content from an external PHP file on a different server using JavaScript can sometimes be problematic. There are instances where the other service may be slow to load or not load at all. Is there a method in JavaScript to attempt fetching th ...

Using Google Maps to add markers, then filtering them with marker clusters

Seeking assistance with my code. I have a script that downloads and processes a JSON file using $.ajax. Additionally, I utilize the moveToLocation() function for filtering, which works fine except for issues with markerclusters where my points remain clust ...

Using jQuery Datatables: Storing the received JSON data into a variable

I have incorporated 2 plugins for my project: Datatables (used for pagination) Defiant.js (providing JSON search capability) Describing my issue along with the code snippet... $(document).ready(function() { var myjson; //Initializing Datatable ...

What is the method for displaying the delete icon, a child component located within the Menu Item, upon hovering over it using Material UI CSS syntax?

My goal is to display the delete icon when hovering over a specific menu item that is being mapped using the map function. The desired functionality is for the delete icon to appear on the corresponding menu item when it is hovered over. I attempted to i ...

Angular's separate scope variables allow for isolated data manipulation within individual components

Struggling with one scope variable affecting multiple elements in a div in AngularJS. Looking for help as a beginner in handling this issue. For a clearer understanding, here's an example: JS: /* controller-home.js ********************************* ...

Can you identify this numerical category?

Can you identify this number type? console.log(0100); // output 64 console.log(050); // output 40 console.log(010); // output 8 In a hexadecimal system, it would be: 0100 = 256 050 = 80 010 = 16 ...

Animating Jquery to smoothly change opacity until it reaches 100% visibility

The script I have does several things, but the main issue lies in the last line where the opacity of the class doesn't seem to reach 100%.... $('.fa-briefcase').parent().on('click', function () { $("#colorscreen").remove(); ...

Interacting with an element within a jQuery dialog box created through a function click

I'm encountering an unusual issue with jQuery and Javascript across all browsers (IE, FF, Chrome). On my (asp.net) webpage, I have the following structure: A header with numerous dynamically generated buttons at PreRender A hidden div containing but ...

The for each loop fails to return the value

In my Vue component, I have a conditional loop that iterates through train stations with 'New York' as the destination or on the route. <tr v-for="departure in filterTrainStations" :key="departure.name"> <td>{{ departure. ...

iOS devices will not scroll horizontally if there is a div that scrolls vertically within a div that scrolls horizontally

Picture a div that scrolls horizontally, housing two vertical-scrolling divs. You'll need to scroll left and right to navigate, then up and down inside the inner divs to read the content. /* RESET TO MINIMUM */ body, html { height: 100%; mar ...

Struggling with the nodejs peepcode tutorial - need some help getting it to run

After purchasing and following the latest nodejs peepcode tutorial, I have hit a roadblock at the initial step. I have spent hours trying to debug my code, but tracing errors in nodejs seems like solving a riddle to me. My app structure is as follows: e ...