how to call vuex from a laravel blade file - laravel

Hi I am quite new to laravel and vue. I am trying to use vuex for the first time but could not figure out why.
I have installed vuex and have imported it in app.js like this
import Vuex from 'vuex';
Vue.use(Vuex);
and I am declaring the vue individually in the blade files so i am trying to do the same with vuex in the blade file
<script>
const store = new Vuex.Store({
state: {
village: ''
},
mutations: {
changeVillage (state,value) {
state.village = value;
}
}
});
var app = new Vue({
el: '#app',
store: store,
data: {
},
});
</script>
now the problem is vuex is not recognized and says its undefined.
i tried to declare the constant store in app.js but then this time it says store is undefined
any help will be appreciated

var app = new Vue({
el: '#app',
store: store,
data: {
},
methods: {
changeVillageMethod() {
var cval = 'somevalue';
this.$store.commit('changeVillage', cval)
},
},
computed: {
village() {
return this.$store.state.village;
},
},
});
Now you have access to village value and changeVillageMethod method in the template part.
I recommend reading Veux docs. https://vuex.vuejs.org/

try again according to the instructions:
import Vuex in the file bootstrap.js:
import Vuex from 'vuex';
...
window.Vuex = Vuex;
Vue.use(Vuex);
then make the following changes to the file app.js:
import store from './YOUR_FILE_WITH_DEFINITION_STORAGE_VARIABLE.js';
new Vue({
el: '#app',
store: new Vuex.Store(store)
});
and your storage JS file:
let store = {
...
};
export default store;

Related

vue v-select array of object not showing in select option

laravel project with vue.i install all thing in app js to work v-select, i don't know anyhting is missing in installation or not.if something is missing tell me that or somthing is wrong in v-select tag please help if anyone is experienced such a thing
on app.js-----------------------------------------------------------------
require('./bootstrap');
window.Vue = require('vue');
import VueRouter from 'vue-router'
import Cat from './components/dashboard/category/Category.vue';
import Sub from './components/dashboard/sub-category/sub-category.vue';
import Dash from './components/dashboard/Dashboard.vue';
import VueToast from 'vue-toast-notification';
import Vuetify from 'vuetify';
import 'vuetify/dist/vuetify.min.css';
import VModal from 'vue-js-modal';
// import Vue from 'vue';
import vSelect from 'vue-select';
import 'vue-select/dist/vue-select.css';
// Import one of available themes
import 'vue-toast-notification/dist/theme-default.css';
import { Form, HasError, AlertError } from 'vform';
window.Form = Form;
// import {routers} from './routers';
Vue.use(Vuetify)
Vue.use(VueToast);
Vue.use(VueRouter);
Vue.use(VModal);
// Vue.use(vSelect)
Vue.component('v-select', vSelect)
const routes = [
{ path: '', component: Dash },
{ path: '/category', component: Cat },
{ path: '/sub-category', component: Sub },
]
const router = new VueRouter({
routes
})
const app = new Vue({
el: '#app',
router,
vuetify: new Vuetify()
});
on component--------------------------------------------------------------------------------
<v-select v-model="select" :items="categories" label="Select" item-text="name" item-value="id" return-object> </v-select>
export default {
data(){
return {
form: new Form({
name:'',
imgUrl:'',
remember: false
}),
categories:[],
select: 'Select',
}
},
mounted(){
this.getCategories()
},
methods:{
getCategories(){
axios.get('sub-category')
.then(res => (
res.data.forEach((value, index) => {
this.categories.push(value);
})
));
}
}
}
Should the items property in the v-select tag not be called options? Source: https://vue-select.org/guide/options.html#options-prop

Vuetify not translating properly data-table

This is the problem:
main.js
import Vue from 'vue'
import Vuetify from 'vuetify/lib'
import { pt } from 'vuetify/es5/locale/pt'
import { Ripple } from 'vuetify/lib/directives'
Vue.use(Vuetify, {
directives: {
Ripple,
},
lang: {
locales: {
pt,
},
current: 'pt',
},
})
const opts = {}
const vuetify = new Vuetify(opts)
new Vue({
router,
store,
vuetify,
mounted() {
this.$vuetify.lang.defaultLocale = 'pt'
this.$vuetify.lang.current = 'pt'
},
}).$mount('#app')
So I´m trying to initialize Vuetify using Portuguese.
The data table that´s being used is printing these error messages in the console:
[Vuetify] Translation key "dataTable.ariaLabel.sortNone" not found, falling back to default
[Vuetify] Translation key "dataTable.ariaLabel.sortNone" not found in fallback
and so on...
Codepen reproducible example: https://codepen.io/adrielwerlich/pen/MWKJaRV
GitHub bug report link: https://github.com/vuetifyjs/vuetify/issues/11676
How can I solve this?

How to extend app.js methods/data/watch from laravel blade view file without creating components

I'm new in vue.js.
My app.js is:
import { store } from './store';
const app = new Vue({
el: '#app',
store,
mounted(){
...
},
methods:{
...
}
});
I'm using laravel and I don't want every time to create components for very small reason.
Without making components app.js will be full of methods whose all are not useful in every pages. That's why I want a way to extend app.js from my home.blade.php file's
#section(scripts)
<script>
...
</script>
#endsection
(without creating any component).
Or, Updating/add in data:{ ... } & methods:{ ... } using that <script> .. </script> in *.blade.php
I've found a solution!
Use Mixin
Create mixin.js file besides app.js
For example
export var custom = {
data: {
show: true
},
methods: {
hide: function(){
this.show = false;
}
},
watch: {
...
},
...
};
and, in app.js file:
import { store } from './store';
import { custom } from './mixin'; //Import mixin
window.app = new Vue({
el: '#app',
mixins: [custom], // Add mixin var here
store,
});
/*
[note: for multiple: import {custom1, custom2, ...} and define [custom1, custom2, ...] ]
*/
More details: Official Documentation

finally() not working in Vuejs another app

Recently I did this tutorial.
In this app I can successfully run a vuejs app and axios.then.catch.finally or this.form.then.catch.finally.
But I added npm to my old project that was recently upgraded to laravel 5.7.
In my app I can't add finally function. If i do, it says:
Uncaught TypeError: axios.post(...).then(...).catch(...).finally is not a function
My app.js:
/**
* First we will load all of this project's JavaScript dependencies which
* includes Vue and other libraries. It is a great starting point when
* building robust, powerful web applications using Vue and Laravel.
*/
// require('./bootstrap');
import Vue from 'vue'
window.Vue = Vue
window.axios = require('axios');
import { Form, HasError, AlertError } from 'vform'
window.Form = Form;
Vue.component(HasError.name, HasError)
Vue.component(AlertError.name, AlertError)
Vue.component('pagination', require('laravel-vue-pagination'));
//select 2
import vSelect from 'vue-select'
Vue.component('v-select', vSelect)
//sweet alert 1
import swal1 from 'sweetalert';
window.swal1 = swal1;
import swal from 'sweetalert2'
window.swal = swal;
const toast = swal.mixin({
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 3000
});
window.toast = toast;
/**
* Next, we will create a fresh Vue ap
*
*
* plication instance and attach it to
* the page. Then, you may begin adding components to this application
* or customize the JavaScript scaffolding to fit your unique needs.
*/
Vue.component('example-component', require('./components/ExampleComponent.vue'));
Vue.component('timetable-component', require('./components/TimetableComponent.vue'))
/* filter active and inactive */
Vue.filter('activeInactive', function (value) {
if(value==1){
return 'Active'
}else{
return 'Inactive'
}
})
const app = new Vue({
el: '#app',
data:{
},
methods:{
},
mounted(){
}
});
I think my app.js is exactly the same as the tutorial app.js for Form request.
Another thing is that in the tutorial he did not use axios import anywhere but he smoothly used it. But without import, I can't use axios.then.
How did he use it without importing axios?
How can I use finally with then.catch?
a componenet:
loadSite() {
axios
.get("/api/site/list")
.then(({ data }) => {
console.log(data);
this.siteList = data;
})
.catch(function(error) {
console.log(error);
}).finally(()=>{
});
},
As document, to make finally works, you need to add promise.prototype.finally
npm install axios promise.prototype.finally --save
and then
const axios = require('axios');
require('promise.prototype.finally').shim();
axios
.get('http://www.example.com/user')
.then((response) => {
console.log(response.data);
return response;
})
.finally(() => {
console.log('this will always be called');
});
I have the same issue, just solved it by includeing this file
<script src="https://polyfill.io/v3/polyfill.min.js?features=Promise.prototype.finally" defer></script>

Vuejs Single File organization with Gulp

I've followed the laracast tutorial about Vue Workflow with Laravel.
So I have a main.js:
var Vue =require('vue');
import Tournament from '../components/Tournament.vue';
new Vue({
el: 'body',
components: {Tournament},
ready(){
alert('Ready To Go');
}
});
And then I have my Tournament.vue:
<template>
I can't see this text on screen
</template>
<script>
export default {
props: ['list'],
}
</script>
And then, in my HTML, I have that:
<tournament></tournament>
<script src="/js/tournaments.js"></script>
/js/tournament.js is generated by gulp.
I can see the alert, so Vue works fine.
What I cant see is my template.
I never see the sentence:
I can't see this text on screen
I also tried to display dynamic data with data(), but with no success.
Any idea what am I forgetting?
var Vue = require('vue');
import Tournament from '../components/Tournament.vue';
new Vue({
el: 'body',
components: {template: Tournament},
ready() {
alert('Ready To Go');
}
});
Does that work??

Resources