Alpine Error x-data .catch is not defined - alpine.js

H there,
im trying to get Alpine with webpack to run but allways get an error and dont know how to go about.
My Webpack config:
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const isProduction = 'production' === process.env.NODE_ENV;
const { VueLoaderPlugin } = require('vue-loader')
const webpack = require('webpack');
// Set the build prefix.
let prefix = isProduction ? '.min' : '';
const config = {
entry: {
main: './source/js/main.js',
admin: './source/js/admin.js',
admin_head: './source/js/admin_head.js',
classic: './source/js/classic.js',
},
output: {
filename: `[name]${prefix}.js`,
path: path.resolve(__dirname, 'dist')
},
mode: process.env.NODE_ENV,
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.js$/,
loader: 'babel-loader',
options: {
presets: [
[
"#babel/preset-env"
]
]
}
},
{
test: /\.s[ac]ss$/i,
use: [
'vue-style-loader',
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
require('postcss-import'),
require('tailwindcss')('tailwind.js'),
require('postcss-nested'),
require('autoprefixer'),
]
}
}
}
],
},
]
},
plugins: [
new VueLoaderPlugin(),
new MiniCssExtractPlugin(),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
],
resolve: {
alias: {
vue: process.env.NODE_ENV == 'production' ? 'vue/dist/vue.min.js' : 'vue/dist/vue.js'
}
}
}
module.exports = config
My Script File:
$(function (){
$('.color-picker').wpColorPicker();
})
})(jQuery)
import Alpine from 'alpinejs'
Alpine.start();
The script is loaded in the footer with defer:
<script defer src="https://ir.test/wp-content/plugins/real-time-comments/dist/admin_head.js?ver=1.1.1"></script>
And in a php file ive:
<div x-data="{ tab:'main' }">
<div class="bg-white grid grid-cols-6 gap-5">
<div class="pt-10 px-5">
<img src="<?php echo RTC_URL ?>dist/images/logo-full.svg" class="border-none w-full h-auto"/>
<div class="flex flex-col w-full mt-10">
<div #click="tab = 'main'"
:class="tab == 'main' ? 'border-b-2 border-plugin border-plugin font-bold' : 'border-b border-gray-500'"
class="px-5 py-3 cursor-pointer">
<?php _e( 'Main settings', 'real-time-comments' ) ?>
</div>
<div #click="tab = 'pusher'"
:class="tab == 'pusher' ? 'border-b-2 border-plugin border-plugin font-bold' : 'border-b border-gray-500'"
class="px-5 py-3 cursor-pointer">
<?php _e( 'Pusher API settings', 'real-time-comments' ) ?>
</div>
<div #click="tab = 'layout'"
:class="tab == 'layout' ? 'border-b-2 border-plugin border-plugin font-bold' : 'border-b border-gray-500'"
class="px-5 py-3 cursor-pointer">
<?php _e( 'Layout settings', 'real-time-comments' ) ?>
</div>
</div>
</div>
<div class="col-span-5">
<form method="post" action="options.php">
<div>
<div class="p-5 bg-gray-200">
<div x-show.transition="tab == 'main'">
<?php settings_fields( 'rtc_main_settings_section' ); ?>
<?php do_settings_sections( 'rtc_main_settings_page' ); ?>
</div>
<div x-show.transition="tab == 'pusher'">
<?php settings_fields( 'rtc_pusher_settings_section' ); ?>
<?php do_settings_sections( 'rtc_pusher_settings_page' ); ?>
</div>
<div x-show.transition="tab == 'layout'">
<?php settings_fields( 'rtc_layout_settings_section' ); ?>
<?php do_settings_sections( 'rtc_layout_settings_page' ); ?>
</div>
</div>
</div>
<input type="submit" name="submit" id="submit"
class="block w-full text-center bg-plugin py-3 text-center text-white my-10 shadow-lg hover:shadow cursor-pointer"
value="<?php echo __( 'save', 'real-time-comments' ) ?>">
</form>
</div>
</div>
</div>
</div>
When i load the Page i get an error;
Alpine Expression Error: func(...).catch is not a function
Expression: "{ tab:'main' }"
Has anybody an idea what im doing wrong, or anybody an sample how to include alpine.js with webpack. And where and how to include the script, i cannot load it via cdn because its not allowed by the wordpress plugin directory. I know its a bit overdoing to use alpine in the wp backend because it comes with jquery out of the box but for frontend files i get the same error.

I had a similar problem after upgrading from AlpineJS 3.2.3 (which worked) to AlpineJS 3.8 (which didn't). Eventually I found THIS. It seems at some point in time AlpineJS started using async/await which is ECMAScript 2017. My build had been targeting "es2015". Changed it to "es2017" and it worked. My build is via the esbuild embedded in Hugo so I changed js.Build's "target" option to "es2017". I don't know where to change it in webpack (that hurts my brain).

I don't see any problem with the Alpine directives in your markup.
I'm assuming admin_head.js is the bundle file in which AlpineJS is being initialized. If your script is being imported at the end of your element, there might not be a need to use the defer attribute.
Also make sure you're not loading AlpineJS twice via some CDN.

Related

Inertia mounted hook with search resets pagination on every action

My vue file:
data() {
return {
search: '',
}
},
mounted() {
this.search = this.filters.search ?? '';
},
watch: {
search(value) {
Inertia.get('/contacts', { search: value }, {
preserveState: true,
replace: true,
})
}
The Laravel Controller:
$contacts = Model::query()
->paginate(10)
->withQueryString();
return Inertia::render('Contacts/List', [
'contacts' => $contacts,
'filters' => request()->only(['search']),
'currentPage' => request()->page,
]);
It all works perfectly if the mounted block is missing.
With it, on every Inertia reload a "new search" is registered (since it's changed in the mounted hook) and it returns to page 1, so basically, every time you change the page it returns you to page 1.
It should be working perfectly with the Composition API's setup, but not sure why can't I get it to work here.
I use axios instead of Inertia.js in pagination
<template>
<div class="row mt-4">
<div class="col-md-5">
<span
>showing {{ data.from }}-{{ data.to }} of
{{ data.total }} items</span
>
</div>
<div class="col-lg-7">
<nav aria-label="...">
<ul class="pagination float-end">
<template v-for="link in data.links" :key="link.id">
<li v-if="!link.url" class="page-item">
<a #click.prevent="paginate(link.label)" class="page-link">
<span
aria-hidden="true"
v-html="link.label"
></span>
</a>
</li>
<li
v-else
:class="{ active: link.active }"
class="page-item"
>
<a
#click.prevent="paginate(link.label)"
v-html="link.label"
class="page-link"
></a>
</li>
</template>
</ul>
</nav>
</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
props: ["data", "url"],
methods: {
paginate(label) {
console.log(`${route(this.url)}?page=${label}`);
axios.get(`${route(this.url)}?page=${label}`).then((res) => {
this.$emit('paginate', res.data)
});
}
}
}
</script>
I ran into the same issue, and after some digging I found this answer. The gist is that you should set your search parameter directly in the data attribute instead of using mounted():
data() {
return {
search: this.filters.search ?? '',
}
},
watch: {
search(value) {
Inertia.get('/contacts', { search: value }, {
preserveState: true,
replace: true,
})
}

How to show/hide #foreach statement

I have a Vuejs 3 dropdown reusable component. My problem is that the #foreach statement runs before the component loads so it causes a flash of the foreach results which is very ugly upon refresh or when the page is loading.
To demonstrate please check this gif:
My component in blade:
<Dropdown title="{{ isset($currentCategory) ? ucwords($currentCategory->name) : 'Categories' }}">
<Dropdowncontent>
<Dropdownitems href="/">
All
</Dropdownitems>
<div>
#foreach ($categories as $category)
<Dropdownitems
href="/?category={{ $category->slug }}&{{ http_build_query(request()->except('category')) }}"
class="{{ isset($currentCategory) && $currentCategory->is($category) ? ' selectedCategoryItem' : '' }}">
{{ $category->name }}
</Dropdownitems>
#endforeach
</div>
</Dropdowncontent>
</Dropdown>
I added a div to contain the #foreach statement but i don't know what to do from here. I don't want to use alpineJS as it will defeat the purpose of using Vue (I guess?).
I just need a way to only display this div or the #foreach statement if the component is fully loaded or if the button is pressed or something like that. Any ideas?
-- EDIT --
I tried to hide the links in my 'dropdownitems' vue component and set the default value to false. The links are now hidden but still the blade #foreach statement echoing out the results as text before the component is loaded:
<template>
<a v-if="showLinks" href="" class="demogard categoryItems">
<slot />
</a>
</template>
<script>
export default {
name: "Dropdownitems",
setup() {
const showLinks = false;
return {
showLinks,
};
},
};
</script>
<style></style>
Here is a gif to show the result of that:
-- EDIT --
Here is my dropdown component:
<template>
<div
class="relative"
v-click-outside="onClickOutside"
#click="showCategories"
>
<slot name="toggler">
<button
class="flex max-h-52 w-full overflow-auto py-2 pl-3 pr-9 text-sm font-semibold lg:inline-flex lg:w-32"
>
{{ title }}
</button>
</slot>
<slot />
</div>
</template>
<script>
import vClickOutside from "click-outside-vue3";
import { ref, onMounted, provide } from "vue";
export default {
name: "Dropdown",
props: ["title"],
directives: {
clickOutside: vClickOutside.directive,
},
setup() {
const sharedState = ref(false);
const showCategories = () => {
sharedState.value = !sharedState.value;
};
const onClickOutside = (event) => {
sharedState.value = false;
};
provide("sharedState", sharedState);
return {
sharedState,
showCategories,
onClickOutside,
};
},
};
</script>
<style></style>
As your question, I think you have to add if condition on your dropdown component.
Your dropdown component should be like this
#dropdown.vue
<template>
<div class="dropdown">
<div #click="show = !show">{{title}}</div>
<div v-if="show">
<slot />
</div>
</div>
</template>
<script>
import { ref } from "vue";
export default {
props: ["title"],
setup(props) {
const show = ref(false);
return {
show,
};
},
};
</script>
Demo
---- EDIT ----
#dropdown.vue
<template>
<div
class="relative"
v-click-outside="sharedState = false"
>
<slot name="toggler">
<button
class="flex max-h-52 w-full overflow-auto py-2 pl-3 pr-9 text-sm font-semibold lg:inline-flex lg:w-32"
#click="sharedState = !sharedState"
>
{{ title }}
</button>
</slot>
<div v-if="sharedState">
<slot />
</div>
</div>
</template>
<script>
import vClickOutside from "click-outside-vue3";
import { ref, onMounted, provide } from "vue";
export default {
name: "Dropdown",
props: ["title"],
directives: {
clickOutside: vClickOutside.directive,
},
setup() {
const sharedState = ref(false);
// const showCategories = () => {
// sharedState.value = !sharedState.value;
// };
// const onClickOutside = (event) => {
// sharedState.value = false;
// };
provide("sharedState", sharedState);
return {
sharedState,
//showCategories,
//onClickOutside,
};
},
};
</script>
<style></style>
Try with a #if directive:
Conditional Rendering
from the documentation:
<button #click="awesome = !awesome">Toggle</button>
<h1 v-if="awesome">Vue is awesome!</h1>
<h1 v-else>Oh no 😢</h1>
As showed in the example it render the "h1" tag conditionally respect the "awesome" variable.
In this case i will set a default value of "false" and i will turn it to "true" in the mounted hook:
Lifecycle
It's impossible to load Vue before PHP because your webpage only displays when full PHP code is received from the server. Therefore, we're never able to stop PHP or HTML from flashing if we're using them inside a reusable Vue component.
The solution I made is simply passing the value of the foreach loop as a prop to the Vue component in order for it to be displayed from there, not from my blade file.
Here's my code in blade after passing the value of the category name as a prop to my Vue component.
<Dropdown title="{{ isset($currentCategory) ? ucwords($currentCategory->name) : 'Categories' }}">
<Dropdowncontent>
<Dropdownitems href="/" category="All"></Dropdownitems>
#foreach ($categories as $category)
<Dropdownitems
category="{{ $category->name }}"
href="/?category={{ $category->slug }}&{{ http_build_query(request()->except('category')) }}"
class="{{ isset($currentCategory) && $currentCategory->is($category) ? ' selectedCategoryItem' : '' }}">
</Dropdownitems>
#endforeach
</Dropdowncontent>
</Dropdown>
Here is me displaying it from there the Vue dropdown items component:
<template>
<a href="" class="demogard categoryItems">
<slot>{{ category }}</slot>
</a>
</template>
<script>
export default {
name: "Dropdownitems",
props: ["category"],
};
</script>
<style></style>

Production tailwind css not displaying correctly on production

So, I noticed some of my tailwind css is not generating properly on production but appearing well on my local any ideas ? I am using, vue + tailwind + laravel + inertia stack. Also I'm not experienced in deploying anything on production, so thus I am using digital ocean's new "Apps" thing.
Production
Ubuntu 18.04.5 LTS
Node Version: 12.22.5
Local
Ubuntu 20.04.2 LTS
Node Version: v16.7.0
What it's suppose to look like ( Local )
What it turned up like ( Production )
Here is my tailwind.config.js
module.exports = {
purge: [
'./resources/**/*.blade.php',
'./resources/**/*.js',
// './resources/**/*.vue',
],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {
colors: {
gray: {
750: '#2d3748',
850: '#1a202c'
},
},
spacing: {
112: '28rem',
120: '30rem',
128: '32rem',
136: '34rem',
},
},
},
variants: {
extend: {},
},
plugins: [],
}
Commands I run on build production
Build Command
composer install --optimize-autoloader
composer dump-autoload --optimize
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan optimize
php artisan migrate
npm install --no-optional
npm run production
php artisan ziggy:generate
Index.vue
<template>
<div class="min-h-screen bg-white pt-12">
<Navbar class="mb-2"/>
<div class="flex flex-row lg:mx-40 md:mx-10">
<div class="md:w-64 w-2/12">
<div class="bg-gray-300 h-screen flex flex-col p-4 space-y-10">
<div>
<span>
<b>Categories</b>
</span>
<div class="flex flex-col space-y-2 cursor-pointer" v-for="category in categories">
<span #click="filterCategory(category)">{{ category.name }}</span>
</div>
</div>
<div>
<span>
<b>Price Range</b>
</span>
<div class="flex flex-col font-semibold">
Minimum Price<input type="text" name="min" placeholder="Min" v-model="filters.minPrice" class="rounded-xl px-2">
Maximum Price<input type="text" name="min" placeholder="Max" v-model="filters.maxPrice" class="rounded-xl px-2">
</div>
<button class="p-0.5 px-2 mt-2 bg-gray-600 text-gray-300 rounded-xl" #click="filter">Filter</button>
<button class="p-0.5 ml-2 px-2 mt-2 text-gray-600 rounded-xl" #click="clearPrice">Clear</button>
</div>
</div>
</div>
<div class="w-10/12 flex flex-col">
<div class="flex flex-row items-center pl-2">
Search <input v-model="filters.search" #keyup="filter" type="text" name="min" placeholder="Search Products" class="border border-gray-400 rounded-xl p-1 m-2 w-full">
</div>
<FilterTags :filters="filters" #remove="removeCategory"/>
<div class="flex flex-wrap h-1/3 cursor-pointer">
<div v-for="product in products.data" class="w-1/5">
<ProductCard class="m-4" :name="product.name" :price="product.price" :currency="'RM'" #click="viewDetails(product.id)"/>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import Navbar from '#/Components/Shop/Navbar';
import ProductCard from '#/Components/Shop/ProductCard';
import FilterTags from '#/Components/Shop/FilterTags';
import { debounce } from 'lodash';
export default {
components: {
Navbar,
ProductCard,
FilterTags,
},
props: {
products: {
type: Object,
default: {}
},
categories: {
type: Object,
default: {}
},
filters: {
type: Object,
default: {}
}
},
data() {
return {
}
},
mounted () {
},
methods: {
filter: debounce( function() {
this.$inertia.get(route('playground.shop.index'), this.filters, { preserveState: true });
}, 300),
filterCategory: debounce( function(category) {
this.filters.categories.push(category.name);
this.filter();
}, 200),
removeCategory(removedCategory) {
this.filters.categories = this.filters.categories.filter(function(category) {
return category != removedCategory;
});
this.filter();
},
clearPrice() {
this.filters.minPrice = null;
this.filters.maxPrice = null;
this.filter();
},
viewDetails(product) {
this.$inertia.visit(route('playground.shop.product.show', product));
}
},
}
</script>
I'm guessing you forgot to uncomment the line that prevents PostCSS from purging the Tailwind classes you're using on your Vue files.
// Change this:
module.exports = {
purge: [
'./resources/**/*.blade.php',
'./resources/**/*.js',
// './resources/**/*.vue',
]
}
// to this:
module.exports = {
purge: [
'./resources/**/*.blade.php',
'./resources/**/*.js',
'./resources/**/*.vue'
]
}

Laravel 6 + Vue.js Failed to mount component: template or render function not defined

Perhaps someone could help me? I'm trying to create a little chat app from here and I am have trouble displaying the Vue components.
The dev tools console gives me:
Failed to mount component: template or render function not defined.
I am using Laravel 6
Appreciate any help on this one! thanks :)
I have tried deleting the node_modules folder and rebuilding, after adding .default - with no luck.
app.js
require('./bootstrap');
window.Vue = require('vue');
/**
* The following block of code may be used to automatically register your
* Vue components. It will recursively scan this directory for the Vue
* components and automatically register them with their "basename".
*
* Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
*/
const files = require.context('./', true, /\.vue$/i)
files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))
Vue.component('chat-messages', require('./components/ChatMessages.vue').default);
Vue.component('chat-form', require('./components/ChatForm.vue').default);
import Vue from 'vue'
const app = new Vue({
el: '#app',
data: {
messages: []
},
created() {
this.fetchMessages();
Echo.private('chat')
.listen('MessageSent', (e) => {
this.messages.push({
message: e.message.message,
user: e.user
});
});
},
methods: {
fetchMessages() {
axios.get('/messages').then(response => {
this.messages = response.data;
});
},
addMessage(message) {
this.messages.push(message);
axios.post('/messages', message).then(response => {
console.log(response.data);
});
}
}
});
and my two components:
ChatForm.vue
<template>
<div class="input-group">
<input id="btn-input" type="text" name="message" class="form-control input-sm" placeholder="Type your message here..." v-model="newMessage" #keyup.enter="sendMessage">
<span class="input-group-btn">
<button class="btn btn-primary btn-sm" id="btn-chat" #click="sendMessage">
Send
</button>
</span>
</div>
</template>
<script>
export default {
props: ['user'],
data() {
return {
newMessage: ''
}
},
methods: {
sendMessage() {
this.$emit('messagesent', {
user: this.user,
message: this.newMessage
});
this.newMessage = ''
}
}
}
</script>
ChatMessage.vue
<template>
<ul class="chat">
<li class="left clearfix" v-for="message in messages" v-bind:key="message">
<div class="chat-body clearfix">
<div class="header">
<strong class="primary-font">
{{ message.user.name }}
</strong>
</div>
<p>
{{ message.message }}
</p>
</div>
</li>
</ul>
</template>
<script>
export default {
props: ['messages']
};
</script>
This might help you to solve your problem, if not just tell me to find new solution. first remove this in your app.js
const files = require.context('./', true, /\.vue$/i)
files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))
Next, remove also this code, you already imported vue twice.
import Vue from 'vue'

Laravel & Vue.js. I Can't Fix My Code

I'm working on an application to manage some Cvs.
All my resource functions work perfectly (creating and editing new Cvs etc.)
But in my Vue.js part, it's about showing details for every Cv in a show view using Axios to get data from the database or post using a form.
My console shows a reactivity problem about my variables: infos and info.
Here is my show.blade.php code:
#extends('layouts.app')
#section('content')
<div class ="container" id="app">
<div class="container" id="app">
<div class ="row">
<div class="col-md-12">
<div class="panel-panel-primary">
<div class="panel-heading">
<div class="row">
<div class="col-md-18"> <h3 class="panel-title"> Experience </h3></div>
<div class="col-md-2 text-right">
<button class="btn btn-success" > Ajouter</button>
</div>
</div>
</div>
<div class="panel-body" >
<ul class="list-group">
<li class="list-group-item" v-for="info in infos">
<div class="pull-right">
<button class="btn btn-warning btn-sm">Edit</button>
</div>
<h3>#{{ info.titre }}</h3>
<p>#{{ info.body }}</p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div >
<form class="flex flex-col" #submit.prevent="addExperience">
<textarea class="border rounded p-2 mp-3" v-model="info.titre"></textarea>
<textarea class="border rounded p-2 mp-3" v-model="info.body"></textarea>
<button type="submit" class="border rounded py-2">Commenter</button>
</form>
</div>
</div>
</div>
#endsection
#section('javascripts')
<script src="{{ asset('js/vue.js')}}"> </script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
window.Laravel={!! json_encode([
'csrfToken' => csrf_token(),
'idExperience' => $id,
'url' =>url('/')]) !!} ;
</script>
<script>
var app= new Vue({
el:'#app',
data:{
message: '',
infos: [],
info:{
id:0,
cv_id:window.Laravel.idExperience,
titre:'',
body:'',
}},
methods:{
getExperiences: function() {
axios.get(window.Laravel.url+'/getexperiences/'+window.Laravel.idExperience)
.then(response=>{
console.log(reponse.data);
this.info=reponse.data;})
.catch(error =>{
console.log('errors: ', error);})},
addExperience:function(){
axios.post(window.Laravel.url+'/addexperience', this.info)
.then(repsonse => {
if(response.data.etat){
this.infos.unshift(this.info);
this.info={
id:0,
cv_id:window.Laravel.idExperience,
titre:'',
body:'',
};}})
.catch(error =>{
console.log('errors: ', error)})},
created:function(){
this.getExperiences();})};
</script>
#endsection
And my two functions get Experiences and addExperience in CvController:
public function getExperiences($id){
$cv= Cv::find($id);
return $cv->infos;
}
public function addExperience(Request $request){
$info = new Info;
$info->titre=$request->titre;
$info->body=$request->body;
$info->cv_id=$request->cv_id;
$info->save();
return response()->json(['etat'=> true, 'id'=> $info->id]);
}
And these are my routes:
Route::resource('cvs','CvController');
Route::get('/getexperiences/{id}', 'CvController#getExperiences');
Route::post('/addexperience', 'CvController#addExperience');
My console:
There are some syntax errors that need to be fixed first.
There's an extra ")" at the end of your created method
The methods property is missing a closing "}"
There's a missing ")" at the very end to match the opening new Vue( parenthesis.
It's helpful to indent your code to make these things easier to spot.
Then, you'll need to update your data to be a function that returns an object. See Vue Docs - Data Must Be a Function
You also have some typos for the "response" variable.
Lastly, in your HTML you have two divs that have id="app".
Your JS should look something like:
var app = new Vue({
el: '#app',
data: {
message: '',
infos: [],
info: {
id: 0,
cv_id: window.Laravel.idExperience,
titre: '',
body: '',
}
},
methods: {
getExperiences: function () {
axios.get(window.Laravel.url + '/getexperiences/' + window.Laravel.idExperience)
.then(response => {
console.log(reponse.data);
this.info = response.data;
})
.catch(error => {
console.log('errors: ', error);
})
},
addExperience: function () {
axios.post(window.Laravel.url + '/addexperience', this.info)
.then(response => {
if (response.data.etat) {
this.infos.unshift(this.info);
this.info = {
id: 0,
cv_id: window.Laravel.idExperience,
titre: '',
body: '',
};
}
})
.catch(error => {
console.log('errors: ', error)
})
},
created: function () {
this.getExperiences();
}
}
});
Let me know if that works for you.

Resources