Vue.js Class-based Decorator not Transmitting Event from Child to Parent Component

I'm fairly new to working with Vue.js and I've encountered an issue with the child to parent emit functionality.

Essentially, I have a Box component that contains a Search component. In the Search component, I attempted the following:

@Watch("searchValue", { immediate: true })
onSearch(value: string, oldValue: string) {
    console.log(this.searchValue); // this is logged OK
    this.$emit("search-val-change", this.searchValue);
}

In the box component template:

<box @search-val-change="doSearch">
    <search></search>
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
    </ul>
</box>

In the box.ts file:

import { Component, Prop, Vue, Watch } from "vue-property-decorator";

@Component
export default class Box extends Vue {
    doSearch() {
        console.log("TEST EMIT"); // nothing is console logged, means it doesn't come to this part at all
    }    
}

I am receiving the following error:

[Vue warn]: Property or method "doSearch" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

...even though it is defined as a method. I am using the Vue Class Decorator which allows me to define methods directly.

Is there something wrong with the syntax or flow of my code?

Answer №1

The string @search-val-change is being emitted from the container component instead of the child search component.

A proper implementation would look something like this:

container.vue

<SearchComponent @search-val-change="doSearch">
...
</SearchComponent>

Additionally, make sure to correctly import and register your search component in the container.vue file. Follow a similar structure as shown below:

@Component({
    components: {
        SearchComponent
    },
})
export default class Container extends Vue {
    doSearch() {
        console.log("TEST EMIT");
    }    
}

Answer №2

To add custom methods, make sure to place them inside the methods property, as shown below:

@Component
export default class Container extends Vue {
  methods: {
    handleClick() {
      console.log("ACTION PERFORMED"); // This message should get logged in the console
    }
  }
}

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

JavaScript file slicing leads to generating an empty blob

I am currently working on a web-based chunked file uploader. The file is opened using the <input type="file" id="fileSelector" /> element, and the following simplified code snippet is used: $('#fileSelector').on('change', functio ...

Can you provide the proper syntax for the Jquery done() function?

I'm currently dealing with the code below: However, I encountered an error on line 6: Uncaught SyntaxError: Unexpected token { Even after reviewing the documentation for the done() function, I'm unable to identify my mistake here. ...

Initializing Three.js to load the model

I have a 3D model that I initially had in the 3DS format. I then converted it to OBJ and finally to JS format. Now, my goal is to load this model into another JS file. Below you'll find the code snippet I've written for this purpose: var loader ...

Extracting specific keys from JSON data

I am working with an array named cols: var cols = ["ticker", "highPrice", "lowPrice","lastPrice"] // dynamic The JSON data is coming from the backend as: info = {ticker: "AAPL", marketCap: 2800000000, lowPrice: 42.72, highPrice: 42.84} If I want to sel ...

Is there a way to access the original query string without it being automatically altered by the browser?

I'm currently encountering an issue with query strings. When I send an activation link via email, the link contains a query string including a user activation token. Here's an example of the link: http://localhost:3000/#/activation?activation_cod ...

How to implement debouncing for an asynchronous custom validator in Vue.js using vuelidate?

I encountered an issue with my validator function that checks if a username is already registered in the database. The problem was that the request was being sent to the server after every single character input, which was far too frequent. To remedy this, ...

Showing a Remote Image on the Screen

I have a 16x16 grid of thumbnail images and I'm trying to implement a feature where clicking on an image displays the full-sized version with a dimmed background. The full-sized image should appear to float above the background. I don't want to ...

Utilizing Color-Thief in a Vue.js Environment: A Step-by-Step Guide

I'm attempting to bring in the color-thief package from NPM (https://github.com/lokesh/color-thief) using this script: import ColorThief from 'colorthief' However, when I try to instantiate new ColorThief(), it gives me an error stating th ...

Adjust Fabric js Canvas Size to Fill Entire Screen

Currently, I am working with version 4.3.1 of the Fabric js library and my goal is to adjust the canvas area to fit its parent div #contCanvasLogo. I have tried multiple approaches without success as the canvas continues to resize on its own. Below is the ...

Obtain the data from the hyperlink destination

Having some trouble extracting a value from an href link to use in my database operations. Unfortunately, I couldn't retrieve the desired value. Displayed below is the code for a button: <a class="btn btn-info" href="scheduleSetTime.php?id=&apo ...

Change the content of a selectbox dynamically with the help of jQuery and AJAX

Currently, I am exploring the best approach for a specific challenge: I have various categories, subcategories, sub-subcategories, and so on, that I need to display in separate select boxes. For instance, initially, the options may look like this: <sel ...

Exploring Angular scope variables using Jasmine while making an ajax request

Can anyone provide guidance on testing Angular scope variables in a controller that is created within an ajax request? Here's the setup: app.controller('NewQuestionCtrl', function ($scope, Question) { loadJsonAndSetScopeVariables($scope ...

Encountering an issue when transferring data between controllers utilizing a demo service

During my journey of learning AngularJS, I decided to create a small app that would allow data to be passed between two controllers using services. Below is the code snippet that showcases how I achieved this: The Controller Code <!DOCTYPE html> &l ...

Tips for resolving an Angular 504 Error Response originating from the backend layer

I am currently facing an issue with my setup where I have an Angular application running on localhost (http) and a Spring Boot application running on localhost (https). Despite configuring the proxy in Angular to access the Spring Boot APIs, I keep receivi ...

Concealing a DIV element when the value is not applicable

I'm currently working on a website for a coffee shop product. On this site, each product has a coffee strength indicator that is determined by the database. The strength can be categorized as Strong, Medium, Weak, or n/a (for non-coffee products). If ...

Angular and Three JS collaboration leads to misplacement of canvas rendering

Currently in the process of developing code for a website, I am attempting to integrate Three JS with Angular. After conducting some research, I have found that this combination is feasible and I have successfully merged these two libraries. However, I am ...

Passing an event from onSubmit in React without using lambdas

Within our current project, the tslint rule jsx-no-lambda is in place. When attempting to capture event from onSubmit, this is how I typically write my code: public handleLogin = (event: React.FormEvent<HTMLFormElement>) => { event.preventDe ...

Tips for optimizing the processing speed of large XML files using jQuery, Javascript, and PHP

Currently, I am developing a store overview page that displays about 20 products per page. The data for this page is sourced from a zipped (gzip) XML file (*.xml.gz). You can find the feed here: . Every day, I download this file to my server using PHP and ...

Sending product identification to content_ids for Facebook Pixel tracking

Looking to implement Facebook Pixel for tracking the ViewContent event on product pages. According to Facebook, it's necessary to provide content_ids or contents along with a content_type. I assume that the content_type should always be 'product ...

Is it achievable to modify the appearance of the "Saved Password" style on Firefox?

After creating a login page that initially had a certain aesthetic, I encountered an issue when saving login data in Firefox. The saved login page took on a yellow-ish tint that was not present in my original design: I am now seeking advice on how to remo ...