1

Trying to use only Single File Components with Laravel. The components are mounted but events do not fire. Inside ExampleComponent I have a button @click="test" that does not fire.

It worked previously when I directly used the ExampleComponent within the blade template but after incorporating an App.vue component the events stopped.

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>
3
  • why are using id="app" also in app.vue <div id="app"> <ExampleComponent/>? Commented Nov 28, 2018 at 17:04
  • That's how Vue.js does it in their demo app. codesandbox.io/s/o29j95wx9 @BoussadjraBrahim Commented Nov 28, 2018 at 17:06
  • it's not need try to remove and it could generate some issues because in this case you have two elements with the same id Commented Nov 28, 2018 at 17:08

2 Answers 2

4

Your Blade Template seems to have conflicting Javascript files that are loaded.

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

Should be the only script you are loading. That way, Laravel Mix can always compile the latest changes to your app.js file. See if that helps, because your .vue and .js files look wired up correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thats it! Thank you so much!
0

I experienced a similar problem, only to find that I had made an unnecessary import in the app.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.