Vue.js and Laravel (Uncaught TypeError: vue__WEBPACK_IMPORTED_MODULE_0__ is not a constructor) - laravel

I've been following a tutorial on youtube on how to make a todolist app (https://www.youtube.com/watch?v=UHSipe7pSac) so far I have experienced a few errors and resolved them searching here, but I couldn't find an answer to why I can't see anything on when running the app. I tried to inspect the element, and specifically, I get this message on the console:
Uncaught TypeError: vue__WEBPACK_IMPORTED_MODULE_0__ is not a constructor
app.js code:
require('./bootstrap');
import *as Vue from "vue"
import App from "./vue/app.vue"
const app = new Vue(
{
el: '#app',
components: { App }
}
);
welcome.blade.php code:
<body>
<div id="app">
<app></app>
</div>
</body>
<script src="{{ mix('js/app.js') }}"></script>
Any help/insights will be very appreciated.

Related

My Vue root file/component is not showing up in Laravel

I'm following this simple Crud tutorial for Vue + Laravel and everything in Laravel part seems fine (from creating controllers & models) seems fine but I can't make vue show up in Laravel welcome blade. Here's my code:
app.js file
require('./bootstrap');
window.Vue = require('vue');
import App from './App.vue';
import VueAxios from 'vue-axios';
import VueRouter from 'vue-router';
import axios from 'axios';
import { routes } from './routes';
Vue.use(VueRouter);
Vue.use(VueAxios, axios);
const router = new VueRouter({
mode: 'history',
routes: routes
});
const app = new Vue({
el: '#app',
router: router,
render: h => h(App),
});
App.vue file:
<template>
<div class="container">
<h2>Vue app</h2>
<router-view> </router-view>
</div>
</template>
<script>
export default {}
</script>
and this is the welcome.blade.php
<!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>
<style>
body {
font-family: 'Nunito', sans-serif;
}
</style>
</head>
<body class="antialiased">
<div id="app"> </div>
<script src="{{ mix('js/app.js') }}" type="text/javascript"></script>
<h2>The welcome blade</h2>
</body>
</html>
edit: answer added below, basically followed another tutorial
Looking at the code after composer require laravel/ui and php artisan ui vue I see small change that may affect the execution of code.
Originally the Laravel setup creates window.Vue = require('vue').default;, but your code has window.Vue = require('vue')
Firstly, try this:
<script src="{{ asset('js/app.js') }}" defer></script>
Secondly, you may need a router view element inside the div with id app. I can't say for sure since I don't know your routes. If the change above doesn't solve the problem, add router view like
<div id="app">
<router-view />
</div>
UPDATE
Another thing to try. Remove these lines
import VueAxios from 'vue-axios';
import axios from 'axios';
Vue.use(VueAxios, axios);
because you have axios on bootstrap.js file.
And make sure there is no error message on the console.
So it must have been something wrong with my installation process because I followed another tutorial and now it's working video

Laravel 5.7 cant use assets with Vue in blade template

Hello I have Laravel version 5.7.24. I have problem with import app.js to blade template.
I have app.js in resources/js/app.js, this same file is other location: public/js/app.js
In welcome.blade.php I add:
<body>
<div id="app">
Hello
<example-component></example-component>
<articles></articles>
</div>
<script src="{{ asset('js/app.js') }}"></script>
</body>
I created articles component in resources/js/components/articles.vue:
<template>
<div>
Hello
</div>
</template>
<script>
export default {
name: "Articles"
}
</script>
Now Laravel return me error:
Unknown custom element: - did you register the component
correctly? For recursive components, make sure to provide the "name"
option.
Because asset refers to the public/js/app.js
I read in this article, taht Laravel removed assets foler. So I added assets folder and my file structure looks like this:
but still Laravel references the file public/js/app.js.
How I can import srcipt (resources/js/app.js) to my welcome.blade.php file ?
Edit:
my resources/js/app.js file:
require('./bootstrap');
window.Vue = require('vue');
Vue.component('articles', require('./components/Articles.vue').default);
const app = new Vue({
el: '#app'
});
When I change script from (in welcome.blade.php):
<script src="{{ asset('js/app.js') }}"></script>
to
<script src="{{ asset('assets/js/app.js') }}"></script>
I have error: GET http://127.0.0.1:8000/assets/js/app.js
net::ERR_ABORTED 404 (Not Found)
It looks like you have a mistake in this line of your app.js:
Vue.component('articles', require('./components/Articles.vue').default);
Try removing .default from here, and see if the component is registered correctly when you build again (npm run dev).
Side note: <articles> should contain a hyphen like my-articles, v-articles, or something else.
When using a component directly in the DOM (as opposed to in a string
template or single-file component), we strongly recommend following
the W3C rules for custom tag names (all-lowercase, must contain a
hyphen). This helps you avoid conflicts with current and future HTML
elements.
https://v2.vuejs.org/v2/guide/components-registration.html#Component-Names

laravel [Vue warn]: Error compiling template:

I'm not using any vue component in my app i just use app.js file in order to compile my npm packages to my assets js/app.js or css/app.css but i'm getting [Vue warn]: Error compiling template: error in console.
I'm not sure why is that!
Code
app.js file:
require('./bootstrap');
window.Vue = require('vue');
import clipboard from 'clipboard';
Vue.component('example-component', require('./components/ExampleComponent.vue'));
const app = new Vue({
el: '#app'
});
Layout
<body>
<div id="app">
<div class="container-scroller">
#include('admin.partials.navbar')
#yield('content')
</div>
</div>
</body>
blades
#extends('layouts.admin')
#section('content')
data
#endsection
Any idea?
SOLVED
Yet i'm not sure why but i added Vue.use(clipboard); and it has stopped to show error.

Laravel - Import new Vue Component

I started working with Vue JS and I want to use it in my laravel project. The Laravel version I'm working with is 5.5
I have a vueTest.blade.php. Looks like this:
<html>
<head>
<title>
Vue Test
</title>
<style href="css/app.css" rel="stylesheet"></style>
</head>
<body>
<div id="app">
<example-component></example-component>
<test></test> <<<----- This component is the problem!
</div>
<script src="js/app.js"></script>
</body>
</html>
This comes with laravel and works fine. the test component however is written by me and doesn't work at all. No errors in the console.
At the bottom of the html file, I'm including the app.js ( Thats where vue.js starts in laravel )
The app.js looks like this:
require('./bootstrap');
window.Vue = require('vue');
Vue.component('example-component', require('./components/ExampleComponent.vue'));
Vue.component('test', require('./components/Test.vue')); <<--- Thats the only line I addet
const app = new Vue({
el: '#app'
});
And the Test.vue is this:
<template>
<h1>hello</h1>
<p>Bla bla bla</p>
</template>
<script>
export default {
}
</script>
<style scoped>
</style>
I tried many things out but nothing worked for me at all. Do I have to register my new components anywhere else? There are no errors in the console and I can't see where the problem is.
Thanks for any help!
Your template needs to have a root element:
<template>
<div>
<h1>hello</h1>
<p>Bla bla bla</p>
</div>
</template>
And better run npm run watch because it needs to compile on every update.

Laravel vuejs keep saying data is undefined

I am new to laravel/vue js. I was following some tutorial and I could not render data in the html. It throws error message is undefined for some reason. And also I did not change much from initial laravel project setup.
one more question. I would like to see console.log result but it does not show in chrome dev-tool. Is this normal for laravel app ?
Error Use of undefined constant message - assumed 'message' (View:
/home/vagrant/flowers/resources/views/app.blade.php)
app.js
Vue.component('example', require('./components/Example.vue'));
const app = new Vue({
el: '#app',
data: {
message: 'test',
todos: [
{ text: 'Learn JavaScript' },
{ text: 'Learn Vue' },
{ text: 'Build something awesome' }
]
}
});
app.blade.php
<html>
<head>
<link re="stylesheet" href="{{ asset('css/app.css')}}"
</head>
<body>
<div id="app">
{{message}}
<example/>
</div>
<script src="{{ asset('js/app.js')}}"/></script>
</body>
</html>

Resources