Issues with Laravel and Vue.js Event Handlers not Triggering

Is it possible to exclusively use Single File Components with Laravel? Despite successfully mounting the components, the events are not triggering. Specifically, within ExampleComponent, a button with @click="test" is failing to fire.

Previously, everything was working fine when directly using the ExampleComponent in the blade template. However, since introducing an App.vue component, the events have stopped functioning.

Blade Template

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Laravel</title>

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css">

    <!-- Styles -->
    <style>

    </style>
</head>
<body>
    <div id="app">
    </div>

    <script src="{{ mix('/js/app.js') }}""></script>
    <script src="/js/app.js"></script>
</body>

app.js

require('./bootstrap');
window.Vue = require('vue');
import Vue from 'vue'
import Buefy from 'buefy'
import 'buefy/dist/buefy.css'
import App from './components/App.vue'

Vue.use(Buefy)
Vue.component('navbar', require('./components/Navbar.vue'));


new Vue({
    el: '#app',
    template: '<App/>',
    components: { App }
});

App.vue

<template>
  <div id="app">
    <ExampleComponent/>
  </div>
</template>

<script>
import ExampleComponent from './ExampleComponent.vue'

export default {
  components: {
    ExampleComponent
  }
}
</script>

ExampleComponent.vue

<template>
  <div style="background-color: #f7faff">
    <navbar></navbar>
    <button @click="test">Submit</button>
  </div>
</template>

<script>
export default {
  data() {
    return {

    }
  },
  methods: {
    test() {
      console.log("TEST")
    }
  },
  mounted() {
    console.log("Component mounted.");
  }
};
</script>

Answer №1

It appears that there may be conflicting JavaScript files being loaded in your Blade Template.

<script src="{{ mix('/js/app.js') }}""></script>

Make sure this is the only script being loaded. By doing so, Laravel's Mix can consistently compile the most recent changes to your app.js file. Verify if this resolves the issue, as your .vue and .js files appear to be wired up correctly.

Answer №2

Encountering a comparable issue, I realized that an extraneous import was included in my app.js file.

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

I'm puzzled as to why my Vuex modules are not functioning properly. I keep receiving the error message: "[vuex]

I've been searching for hours and can't figure out why I keep getting this error message: [vuex] unknown mutation type: groceryStore/getStoreApple on all of the commits to the mutations in the groceryStore-module. I believe I'm following the ...

Authentication error: The API data retrieval was disrupted due to an unexpected termination of input

I encountered an error while trying to fetch data from an API using JavaScript. <button onclick="event.preventDefault(); getOdds()">Print odds</button> JS function getOdds() { const sportsKey = prompt("Enter the sports key:&q ...

How can I execute a basic query in jQuery or JavaScript based on a selected value that changes

After successfully retrieving the dropdown selection value with alert(selectedString) in this scenario, I am now looking to utilize that value for querying a table using mysqli and PHP. What is the best approach for querying a database table based on the ...

What is the benefit of using an IIFE in this particular way when constructing a JavaScript library?

Many libraries I've seen use a similar style to define their structure. Upon further inspection, I've noticed that the first self-invoking function is often related to Require.js or AMD systems, with 'factory' as an argument. My curiosi ...

Designing versatile buttons with HTML

When accessing the website on a phone, I have trouble seeing and tapping on these two buttons because they are too far apart. I would like to change it so that once a file is selected, the 'choose file' button will be replaced by the upload butto ...

Symfony: The Database Query Button with a Pop-up Feature

I am looking to create a button that will automatically search my database for all users with a "secouriste" attribute set and display their first name, last name, and phone number in a popup. After conducting research, here is what I have gathered: In m ...

When a jQuery click event is triggered, the event.target will return the child element that was clicked within the

When I have a jQuery click event assigned to a hyperlink that contains an image, each with separate ids, I expect clicking the hyperlink to trigger the code event.target.id, returning the hyperlink's id. However, it actually returns the image's i ...

Error code 1 encountered an internal error while trying to execute a task

Currently, I have this job set up to clear out unnecessary records. The code provided has been simplified for debugging purposes. However, almost 80% of the time when running it, it fails to find anything due to Error code 1 "internal error": Parse.Cloud. ...

What is the best way to refresh jCarousel following the loading of AJAX content?

After purchasing a custom jCarousel JavaScript (available at http://pastebin.com/BwsVpNjr), I encountered an issue when trying to load content with AJAX. The carousel breaks because it is unable to detect the width or height. Is there a way to have jCarous ...

Tips on adding several elements to an array depending on the selected value from various checkboxes?

I am working on a project where I need to populate an array based on selected checkboxes. Let's assume we have 3 arrays containing strings: const fruits = ["Apple", "Banana", "Orange", "Grapes"]; const vegetable ...

Tips on automatically adjusting the width of a div to fit the content, maintaining center alignment, and ensuring the contents float to the left

I am facing an issue with a div element that has multiple children. My goal is to have the children neatly fit to the bottom left of the grid when the window expands, utilizing the next available space efficiently. However, as the div expands with the wind ...

Troubleshooting Safari compatibility issues with Twitter Direct Messages in Angular

I am attempting to create a Twitter direct message with predetermined text already filled in. My current method involves using window.open() to prepare the message. window.open(https://twitter.com/messages/compose?text=${this.helloWorld}); helloWorld = ...

Is there a way to assign a null value to an empty material UI text field when submitting the form, rather than an empty string?

One issue I am facing is that the default value of the text field is zero, but when I submit the form, the value of the text field becomes an empty string instead. This is not the intended behavior as I want the value to remain zero in the end. How can I r ...

Confirmed the validation of an input that falls outside the parameters of the ReactiveForms model

Check out this StackBlitz example In my Angular application, I am utilizing a Reactive form with 3 inputs, each having its own validation. Additionally, there is an input that exists outside of the form within its own component, also with its own reactive ...

The map function is malfunctioning when trying to query data from MongoDB

I have multiple mongodb documents structured like this {'_id':'1', 'year':2020, 'season':'Summer', 'status':'Over'}, {'_id':'2', 'year':2020, 'se ...

The blank body is being displayed when sending a POST request with vanilla JavaScript using Ajax

I've been struggling to display the data received on the server side, but all I get is a blank body with req={} printed. I've spent days trying to fix this issue and would really appreciate any help! Here's the server-side code: const expr ...

Implementing Blob image rendering in Vue.js from a database

In my front-end development, I am utilizing Vue.js. On the backend, I have set up Node.js, Express, and PostgreSQL with Sequelize. One of the challenges I encountered involved storing an item in the database that includes a thumbnail image. Database Model ...

It looks like everything is running smoothly, but it seems like the ReactDOM.render() method is missing in action

I'm currently diving into the world of React.js and eager to build my knowledge from the basics upwards. While delving into the documentation, I stumbled upon the utilization of ReactDOM.render(element, Document.getElementById("root")), whi ...

Separating views, collections, and models in Backbone.js into different JS files can lead to issues where they may not be able to communicate with each other effectively

I have successfully created a web app using Backbone.js where all the views, collections, and models are written into one js file. Now, I want to separate them into different js files like this: <script type="text/javascript" src="js/layermanagemodel. ...

Application with Single Page design has a Large DOM, leading to sluggish performance

I've been working on a single page application that heavily relies on widgets like grids and tabs from the jqWidgets library. These widgets are all loaded when the page is first opened. However, I've noticed that after interacting with the site f ...