Is it possible to invoke a computed property within the props of a child component in Vue.js?

In the context of my child and parent components, I have a boolean prop in the child component with a default value of false. When calling the child component within the parent component and setting it based on a computed function that returns a boolean value, an error occurs. child component

<template>
  <div v-if="fruitDisplay">
    <span >{{ fruitName }}</span>
  </div>
</template>

<script>

export default {
  name: "childComponent",
  props: {
    fruitDisplay: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      fruitName: 'fruit selected !!!!'
    }
  }
}
</script>

<style scoped>

</style>

parent component

<template>
  <div id="selector">
    <div v-model="selected" v-for="(item, key) in fruitList" :value="key">
       <child-component :fruitDisplay='getCoconut(item)'></child-component>
    </div>
  </div>
  <br>
  <br>
  <span>Selected: {{ selected }}</span>
  
</div>
</template>

<script>

export default {
  name: "ParentComponent",
  data() {
    return {
      selected: '',
        fruitList: {
          "fruit1": "Apple",
          "fruit2": "Banana",
          "fruit3": "Coconut"
        }
    }
  },
  computed: {
    getCoconut(item) {
      return item === 'Coconut'
    }
  }
}
</script>

<style scoped>

</style>

I encountered the following error: TypeError: _vm.getCoconut is not a function in the browser console, and my page fails to display. The issue lies here:

<child-component :fruitDisplay='getCoconut(item)'></child-component>

How should I go about resolving this?

Answer №1

I'm a little confused about your goal, but I'll do my best to assist you. It appears that you want to create a selector that displays 'Fruit selected' when the fruit Coconut is chosen.

You cannot pass parameters to computed properties. However, you can create a method that checks if the currently selected fruit is Coconut and returns true or false.

If you're considering creating a custom selector, more information would be helpful to provide a tailored solution. For now, I've used a standard selector as an example.

<div>Selected: {{ selected }}</div>

<select v-model="selected">
  <option disabled>Please select one</option>
  <option v-for="(item, key) in fruitList" :key="key" :value="item">
    {{ item }}
  </option>
</select>

<FruitSelector :fruitdisplay="checkIfCoconut()" />

Here's the corresponding method:

checkIfCoconut() {
  return this.selected === 'Coconut'
}

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

communication flow in two directions between server and client

Looking for recommendations on a javascript/nodejs solution that enables bi-directional communication between server and client. The application flow involves the client sending a discovery service to the server, which responds with a challenge. The client ...

Reading data from a Node PassThrough stream after writing has finished can be achieved by piping the stream

Is it possible to write to a Node Passthrough stream and then read the data at a later time? I am encountering an issue where my code hangs when trying to do this. Below is a simplified example in Typescript: const stream = new PassThrough(); strea ...

What is a sleek method for including a key and value pair to an object using an array?

In a project using angular2/typescript, I am working with an array of objects that contain key/value pairs from a database. These values are then displayed in a table on the UI using ag-grid-ng2. The table headers are dynamic and set in the database. One ...

Finding a problem in node.js

I have encountered a problem while creating a server game using node.js. I am seeking assistance to resolve this issue. Here is the specific problem: https://i.stack.imgur.com/FMlsL.png app.js: var express = require('express'); var app = expr ...

Quickly - Secondary page with nearby resource location

My Vue project consists of multiple pages, each specified as entry points using rollupOptions.input: rollupOptions: { input: { main: resolve(__dirname, "index.html"), subpage1: resolve(__dirname, "subpage1/index.html" ...

What sets apart ajax calls from getJSON requests?

I am encountering an issue with my Web.API app being served from the URL http://server/application. When I try to pull data from the servers using a GET request on the client side, I am facing unexpected behavior. The following code snippet works as expec ...

What is the simplest method for invoking a C# function from JavaScript?

I am facing an issue with a LinkButton control in my project. I need to attach a c# method to it, but when I add the control to the page using parse control function, it changes the call to javascript and results in an undefined error. I have been trying t ...

What are the best strategies for breaking down an AngularJS application into smaller modules and managing routing effectively?

What is the most effective way to divide an AngularJS application into smaller modules? For instance, if I have a blog post with commenting functionality, I could potentially separate them into modules like "posts" and "comments", rather than having all th ...

The Material UI React radio buttons have the ability to modify the value of previous buttons

I'm facing an issue with my Material UI React Stepper Component that contains a group of radio buttons. The problem is that changing the radio button in one step affects the selected value in previous and future steps. I've experimented with diff ...

What is the best way to combine two JavaScript objects?

One object I am currently working with resembles the following structure let ob1 = { 'item_data':{ 'stack':{ 'purchase':'12345', 'order':'22222' } } } Additionally, I have anothe ...

Encountered error message: "Cannot assign argument of type '() => () => boolean' to parameter of type 'EffectCallback'"

I recently started working with TypeScript. I encountered an issue when attempting to utilize useEffect in TypeScript within a React context, Error: Argument of type '() => () => boolean' is not assignable to parameter of type 'Effec ...

`Some Items Missing from Responsive Navigation Menu`

Hey there! I'm currently diving into the world of responsive design and I'm attempting to create a navigation bar that transforms into a menu when viewed on a mobile device or phone. Everything seems to be working fine, except that not all the na ...

What is the best way to utilize multiple models under a single root in ReactJS?

Greetings! I currently have a root structure in the following code snippet: <body> <noscript>You need to enable JavaScript to run this app.</noscript> <button >Platform</button> <button >Game</button> < ...

When working with Node.js and Express, I encountered an issue where the req.session object was not defined within

It's peculiar to me that req.session.username is undefined in the tag >>>DOESNT WORK<<< while it works in the tag >>>THIS DOES WORK<<<. I passed req as an argument to my module, but it seems like there might be some ...

Handling a jQuery Ajax success callback when it fails

I am encountering an issue with my ajax post request where even though I have separate success, failure, and status code handlers, when the request fails with a 400 error, part of the success function is still being executed. Can someone provide insight in ...

Making a node.js API call using multipart/form-data

Submitting Data Using POST https://i.sstatic.net/iVTDd.png Node.js Data Capture Method without the Use of Express and Body-Parser FORM DATA: ORDERID: MID: TXNID: TXNAMOUNT: 5.00 PAYMENTMODE: NB CURRENCY: INR TXNDATE: 2021-06-14+23%3A02%3A30.0 STATUS: ...

Experiencing issues with the functionality of jQuery AJAX?

I am experiencing difficulties with a jQuery AJAX post. Here is the code: <script> var callback = function(data) { if (data['order_id']) { $.ajax({ type: 'POST', url: '<?php echo $_SERV ...

The FormData function is unable to retrieve the input fields of a form

My Unique Code <!DOCTYPE html> <html> <body> <div id="custom-form-sample"> <form enctype="multipart/form-data"> <input type="text" name="custom-text" /> <input type="radio" name="custom-radio" ...

What could be causing the delay in Express when transitioning between console logs while using AngularJS $http.get?

By utilizing Express, Node, and Angular, I incorporated an HTML button on my website that triggers a get request to Express. This request then executes a function that logs a predefined message to the console. Initially, when I click the button for the fir ...

What is the best way to populate empty dates within an array of objects using TypeScript or JavaScript?

I am trying to populate this object with dates from today until the next 7 days. Below is my initial object: let obj = { "sessions": [{ "date": "15-05-2021" }, { "date": "16-05-2021" }, { "date": "18-05-2021" }] } The desired ...