Vue js is throwing an error because it is unable to find the "buscador" property or method that is being referenced in the render function

I am currently diving into the world of laravel combined with Vue js. I am working on integrating a search engine using vue js components, and I would greatly appreciate any help you can provide. Thank you in advance.

Below is the Vue js component where the search input is located:

<template>
    <div class="container">
        <form>
            <input 
                class="form-control" 
                placeholder="Search"
                type="search"
                v-model="searchTerm"
                @keyup="searchProducts" 
            >
        </form>
    </div>
</template>

This snippet shows my app.js file configuration:

const { default: Axios } = require('axios');

require('./bootstrap');

window.Vue = require('vue');

Vue.component('example-component', require('./components/ExampleComponent.vue').default);

const app = new Vue({
    el: '#app',
    
    created(){
        this.fetchProducts();
    },

    data: {
        
        products:[],
        searchTerm: '',
        searchTimeout:''
    },
    methods: {
        fetchProducts(){
            
            Axios.get('./products', {
                params:{
                    filter: this.searchTerm
                }
            })
            .then(res => {
                this.products = res.data.data ;
            })
            .catch( error => {
                console.log( error.response )
            });
        },

        searchProducts(){
            clearTimeout( this.searchTimeout = setTimeout(this.fetchProducts, 360) )
        }

    }
 
});

Finally, I embed my Vue js component within a blade view:

<div id="app">
    <example-component></example-component>      
</div>

Answer №1

To implement the functionality in your ExampleComponent.vue file, make sure it follows this structure:

<template>
    <div class="container">
        <form>
            <input 
                class="form-control" 
                placeholder="search"
                type="text"
                v-model="searchTerm"
                @keyup="searchProducts" 
            >
        </form>
    </div>
</template>
<script>
import Axios from 'axios';

export default {
    
    created(){
        this.fetchProducts();
    },

    data(){
        return {
            searchTerm: ''
        }
    },
    
    methods: {
    
        fetchProducts(){
            
            Axios.get('./products', {
                params:{
                    filter: this.searchTerm
                }
            })
            .then(res => {
                this.products = res.data ;
            })
            .catch( error => {
                console.log( error.response )
            });
        },

        searchProducts(){
            clearTimeout( this.setTimeoutSearch = setTimeout(this.fetchProducts, 360) )
        }
    }
}

Next, update your app.js as shown below:

require('./bootstrap');

import Vue from 'vue';
import ExampleComponent from './ExampleComponent'; //assuming this is your component

Vue.component('example-component', ExampleComponent);

const app = new Vue({
    el: '#app'
});

You can then use the ExampleComponent within your HTML like this:

<div id="app">
    <example-component></example-component>      
</div>

Note that data properties of the root component cannot be accessed locally in a vue component. You can access them using $root.searchTerm for example.

It's best practice to keep Vue components decoupled and pass required properties as props from parent components when needed.

For more examples and detailed information, refer to the Vue documentation.

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

Tips for properly modifying an attribute within an array of objects in JavaScript using ReactJS

My array of objects looks like this: this.state = { itemSquare: [{ item: "bomb", status: false }, { item: "bomb", status: false }, { item: "bomb", status: false }, { item: "bomb", status: ...

converting an angular object into a string representation

I stumbled upon this guide: . and it includes the following piece of code: import { Component } from '@angular/core'; import { FormGroup, FormControl } from '@angular/forms'; @Component({ selector: 'app-root', templateUrl ...

Show a row of pictures with overflow capabilities

I am looking to develop my own image viewer that surpasses the capabilities of Windows. My goal is to design an HTML page featuring all my images laid out horizontally to maximize screen space. I also want the images to adjust in size and alignment as I z ...

How to access selection range styles using JavaScript

It is common knowledge that we can retrieve the selection of text in JavaScript using the following method: var range = window.getSelection (); However, how can we obtain the style of this selection? For example, when I select bolded text or italicized ...

In React, is it typical to maintain identical values in both state and ref?

When working with my React app, I encountered a situation where I needed to access state values inside setTimeout() and setInterval(). However, due to closures being bound to their context once created, using state values in these functions would not refle ...

Node.js and MongoDB Login Form Integration with Mongoose

I am relatively new to web development and currently working on a simple web page for user login authentication. My goal is to verify user credentials (username & password) on the LoginPage from a mongoose database, and if they are correct, redirect them t ...

Typescript does not produce unused members

Having an issue with the JS code that TypeScript compiler is generating. Here's an example class: // Class export class UserDTO { Id: number; FirstName: string; LastName: string; DateOfBirth: Date; getFullName(): string { ...

Tips for concurrently and asynchronously executing multiple AJAX requests

I am working with a javascript object named Agendamento which includes the following key parts: const Agendamento = { // ... storeResultados: async function (consulta) { //... $.ajax({ type: 'POST', ...

Having trouble with the dropdown feature on AngularJs?

Encountering an issue while trying to display the dropdown list. Upon inspecting in Chrome, it seems like the data is loading correctly but when clicked, the dropdown menu does not show up. The data is fetched from the BooksController and all options are ...

I am encountering an issue while trying to update SQL data from within a Node.js

Using a for-loop to update SQL command is common practice. Here's an example: for(var i=count1; i < count2;i++){ Book.TimeStart = Times[I] console.log(Book.TimeStart) sql = sql + `UPDATE projectroom.Details SET BookingId = `+Book.Bo ...

Default close x button not functioning to close modal dialog

When I click the [X] button in my modal dialog box, it doesn't close. Here is an example of my code: $('#apply_Compensation_Leave').show(); This is the modal code: <div class="modal" id="apply_Compensation_Leave" tabindex="-1" role="di ...

Is the return value a result of destructuring?

function display(): (number, string) { return {1,'my'} } The code above is displaying an error. I was hoping to use const {num, my} = print(). How can I correctly specify the return type? ...

The React component is failing to display updated data from the Redux store

I've been working with React and Redux, trying to update my counter value in the React view. I can successfully log the latest state of my Redux store, but the changes are not reflecting in my React view. const counter = (state = 0, action) => { ...

JavaScript onclick event on an image element

I have a JavaScript function that shows images using CSS styles. <script type="text/javascript"> $(function () { $("div[style]").click(function() { $("#full-wrap-new").css("background-image", $(this).css("background-image")); }); }); ...

What is the best way to imitate a DOM in order to effectively test a Vue application with Jest that incorporates Xterm.js?

I've created a Vue component that displays an Xterm.js terminal. Terminal.vue <template> <div id="terminal"></div> </template> <script> import Vue from 'vue'; import { Terminal } from 'xterm/lib/public ...

Customizing the label styles within MUI's Chip component

Incorporating React with MUI's library has been a seamless experience for me. One of the key MUI components I have integrated is the Chip Within this particular Chip, there lies the label attribute, offering the option to showcase a text f ...

Changing the location of an ArcGIS map with a click event in a Vue application

I am attempting to dynamically update a map to display my current location using Vue. I have created an onClick event that updates the props and sends them to my map component. To trigger a re-render of the map component when the data changes, I am utilizi ...

Secure your data with public key encryption and decryption using the Crypto Module in NodeJS

I have a challenge with encrypting/decrypting data using a public key that is stored in a file. The code snippet below illustrates my approach: encryptWithKey (toEncrypt, publicKeyPath) { var publicKey = fs.readFileSync(publicKeyPath, "utf8"); ...

What could be causing Express to display a different page than the one specified in res.render?

Upon trying to view the compare.ejs page, I encountered an unexpected issue where a different page was being rendered instead. What could be causing this discrepancy? Here is my app.js code: var compare = require('./routes/compare')(nav); app.u ...

Automatically assigning a default dynamic value to a dropdown option using Vue.js

I need to set a default selected value for the select option element, but I am facing difficulty in achieving the desired result. <template> <select v-model="tutor_work.start_year"> <option>{{tutor_work.start_year}}< ...