link2055 link2056 link2057 link2058 link2059 link2060 link2061 link2062 link2063 link2064 link2065 link2066 link2067 link2068 link2069 link2070 link2071 link2072 link2073 link2074 link2075 link2076 link2077 link2078 link2079 link2080 link2081 link2082 link2083 link2084 link2085 link2086 link2087 link2088 link2089 link2090 link2091 link2092 link2093 link2094 link2095 link2096 link2097 link2098 link2099 link2100 link2101 link2102 link2103 link2104 link2105 link2106 link2107 link2108 link2109 link2110 link2111 link2112 link2113 link2114 link2115 link2116 link2117 link2118 link2119 link2120 link2121 link2122 link2123 link2124 link2125 link2126 link2127 link2128 link2129 link2130 link2131 link2132 link2133 link2134 link2135 link2136 link2137 link2138 link2139 link2140 link2141 link2142 link2143 link2144 link2145 link2146 link2147 link2148 link2149 link2150 link2151 link2152 link2153 link2154 link2155 link2156 link2157 link2158 link2159 link2160 link2161 link2162 link2163 link2164 link2165 link2166 link2167 link2168 link2169 link2170 link2171 link2172 link2173 link2174 link2175 link2176 link2177 link2178 link2179 link2180 link2181 link2182 link2183 link2184 link2185 link2186 link2187 link2188 link2189 link2190 link2191

[Vue.js] How to get state vuex on mounted() and pass to data?

to pass the state vuex with props component input, because to make default value input on component input. i must to get data from vuex when first time the page load but state vuex is empty. i use subcribe api vuex on mounted() but can’t pass to data.

form.vue

<template>
input-modal(
:data=”namesAll”
:validations=”‘required’”
name=”child”
v-model=”form.input1”
:username=”form.input1”
class-input=”form-control-lg”
img-icon=”https://img.icons8.com/ios/20/495057/user-filled.png"
placeholder=”Child”
)

</template>

<script>
import inputModal from “~/components/form/inputModal.vue”;

data: () => ({
namesAll: []
}),

mounted() {
this.$store.dispatch(“names/getNames”);
// test load vuex
this.loadData();

// get data mutation
this.$store.subscribe((mutation, state) => {
// console.log(state.names.all);
if (mutation.type == “GET_ALL”) {
this.namesAll = state.names.all;
console.log(“na”, state.names.all); // empty data
}
});
},

methods: {
loadData() {
console.log(“data vuex”, this.$store.state.names.all); // data vuex empty
}
}

inputModal.vue

<template lang=”pug”>
.input-modal

.input-group(ref=”inputGroup”)
.input-group-prepend(v-if=”inputIcon || imgIcon”)
span.input-group-text
i(v-if=”inputIcon” :class=”inputIcon”)
img(v-if=”imgIcon” :src=”imgIcon”)

input.form-control(
type=”text” :placeholder=”placeholder”
v-model=”input” :class=”[classInput, {‘is-invalid’: errors.has(name)}]“
@focus=”focus(true)” autocomplete=”off”
v-validate=”validations” :name=”name”
)
.invalid-feedback(v-if=”errors.has(name)”) { errors.first(name) }

<!– modal –>
transition(name=”fade”)
.modal(v-show=”focused” ref=”modal”)
.modal-dialog.modal-lg(role=”document”)
.modal-content
.modal-header.bg-primary.text-white.justify-content-between
h6.modal-title Pilih Nama
button.btn.btn-link.p-0(@focus=”focus(false)”)
.h5.m-0.text-white x

.modal-body
.row
.col-md-6.col-12
ul.list
li.list-item(
v-for=”name in data”
:key=”name.username”
@click=”select(name.username)”
)
.name
span.mr-3 { name.username }
span.text-muted { name.name }
</template>

<script>
export default {
inject: [“$validator”],
props: {
data: { type: [Object, Array], required: false },
inputIcon: { type: String, required: false, default: “” },
imgIcon: { type: String, required: false, default: “” },
placeholder: { type: String, required: false, default: “” },
classInput: { type: String, required: false, default: “” },
username: { type: String, required: true },
validations: { type: [String, Object], default: “” },
name: { type: String, required: true }
},

data: () => ({
focused: false,
input: “”,
selected: “”
}),

watch: {
errors: function(error) {
this.$emit(“has-error”);
},

username: {
handler: function(val) {
if (val == “” || val == undefined) {
this.input = “”;
this.selected = “”;
return;
}
// console.log(val, this.data);
// can’t read vuex state
this.select(val);
},
immediate: true
}
},

created() {
this.input = this.username || “”;
},

mounted() {
// console.log(“im”, this.data);
if (this.username !== “”) this.select(this.username);
},

updated() {
this.$emit(“input”, this.selected);
},

methods: {
focus(bol) {
this.focused = bol;
},
select(username) {
this.input = this.nameInfo(username);
this.selected = username;
this.focus(false);
},
nameInfo(username) {
let names = this.data.filter(name => name.username == username);
console.log(“filter”, names);
if (names.length) return `${names[0].name} (${names[0].username})`;
}
}
};
</script>

i expect get data from state vuex on first time load page and can pass to props component.

Solution :

once you have loaded data from getNames, you can then easily mirror the state in the component with mapState:

computed: {
…mapState(‘namesAll’, state => state.namesAll)
},

Solution 2:

You can use mapGetters of vuex.
This is a example:

vuex

const state = {
names: {
all: []
}
}

const getters = {
nameAll: state => state.name.all
}

view.vue

<template>
<span>{ nameAll }</span>
</template>
<script>
import { mapGetters } from ‘vuex’
export default {
computed: {
…mapGetters([‘nameAll’])
}
}
</script>

[Vue.js] How to use npm package dependency

when learning to create npm packages by creating a session check function sessionFn that will popup a modal 1 minute before the session expires.

The function works as expected on the main app (a nuxtJS app) but when I make use it as an npm module there is to pass moment as an argument even though moment is listed as a dependency and is imported on the module.

when missing something, why is my module not picking up moment? to use the module like sessionFn(this, to.path); instead of sessionFn(this, to.path, moment); moment is undefined when I don’t pass it

Package files

package.json

{
“name”: “hello-stratech”,
“version”: “1.0.17”,
“description”: “Hello Stratech”,
“main”: “index.js”,
“keywords”: [
“npm”,
“hello”,
“stratech”
],
“author”: “Simo Mafuxwana”,
“license”: “ISC”,
“dependencies”: {
“moment”: “^2.22.2”
}
}

index.js (main js file)

import moment from “moment”;

module.exports = {
greeting(name) {
alert(“Hello.. “ + name);
},
department(dev) {

},
sessionFn(context) {
const exp = context.$store.state.session.exp;
let userSystemTime = new Date();
userSystemTime = moment.utc(userSystemTime)

const diff = moment(userSystemTime).diff(moment(exp), ‘minutes’);
if (diff = 1) {
// open modal
}
}
}

Usage

This is how I use the package in the main app

import moment from ‘moment’;
import { sessionFn } from “hello-stratech”;

export default {

watch: {
$route(to) {
sessionFn(this, to.path, moment);
}
}

}

Solution :

Try making moment as devDependancy through npm i moment –save-dev instead of dependency.

That way moment will only be needed when the package is development (means when you are developing the project) but not when it is used.

Hope it fixes the issue

for more depth knowledge

Solution 2:

You dont need to import and pass moment into the function since you are importing it in the function file. You are not even using the passed argument in the function. So you can just safely dont pass it. You are also not using second argument that u pass to.path, so u can omit it too.

As a suggestion you should install and use eslint, which will catch such things. You can setup a nuxt project with eslint using create nuxt app for example.

[Vue.js] Can't include vue related npm package to my Laravel + Vue project

to include beatiful flash messages to mey Laravel + vue.js app, so I found a npm package on GitHub and installed this in my app (npm install)

I’m beginner in VueJs and I’m facing a problem: I just can’t use the package in my vue.js components since it throw errors when I try to include this

What I use and what I get:

it’s my app.js code

window.vue.js = require(‘vue’);

/*including the package*/
import FlashMessage from ‘@smartweb/vue-flash-message’;
Vue.use(FlashMessage);
/*including the package*/

Vue.component(‘settings-form’,require(‘./components/SettingsFormComponent.vue’).default);
Vue.component(‘profile-nav’,require(‘./components/ProfileNavComponent.vue’).default);

const app = new Vue({
el: ‘#app’,
});

What error I get using the code:

“TypeError: Cannot read property ‘flashMessage’ of undefined at app.js:1863”

I run this method (flashMessage) when to output a message:

Also I tried to use this code in my app.js to include the packange:

Vue.component(‘FlashMessage’, require(‘@smartweb/vue-flash-message’).default);

but it doesn’t work too, it throws this error:
“Failed to mount component: template or render function not defined …”

I’m trying to use my component that way (in ProfileNavComponent.vue):

<FlashMessage></FlashMessage>

Could you please tell me what the problem can be in?
thanks guys for any help)
I really appreciate this!

Solution :

I usually use vue.js packages adding them from the name of the package, try:

import FlashMessage from ‘vue-flash-message’;

And when you want to use the component, try to use FlashMessage.show().

[Vue.js] How to use computed for object attribute?

to get an attribute from an object to be computed, but as far as I’m aware, this is not supported.

there is tried using something like objects.atributes:{ /*codes */ } but got errors.

to achieve something like this:

<template>
<div class=”form”>
<form>
<div class=”form-group”>
<label>First Name : </label>
<input type=”text” v-model=”firstName” name=”firstname” class=”form-control”>
</div>
<div class=”form-group”>
<label>Last Name : </label>
<input type=”text” v-model=”lastName” name=”lastname” class=”form-control”>
</div>
<div class=”card text-center”>
Full name is: { formdata.fullname }
</div>

</form>
</div>
</template>

<script>
export default {
data(){
return {
firstName:’’,
lastName:’’,
formdata:{
computed:{
fullname:{
get:function(){
return this.firstName + ‘ ‘ + this.lastName
}
}
}
},
}
},
/* there is tried this too, and got syntax error
computed:{
formdata.fullname{
get:function(){
return this.firstName + ‘ ‘ + this.lastName
}
}
}
*/
}
</script>

I expected the full name to show up, but it didn’t.

Solution :

Computed properties are defined outside of data object. Do it like this:

export default {
data() {
return {
firstName:’’,
lastName:’’,
}
},
computed: {
fullname: function() {
return this.firstName + ‘ ‘ + this.lastName;
}
}
}

<div class=”card text-center”>
Full name is: { fullname }
</div>

[Vue.js] Iterating through group by results

there is to iterate through groups and inside groups I need to iterate for every group in its own.

this is the response I get:

{
“message”: “”,
“status”: “success”,
“response”: {
“Te dhenat e Klientit”: [
{
“id”: 1,
“specification”: “Personal Accidents”,
“header”: “Te dhenat e Klientit”,
“item”: “Veprimtaria”,
“type”: “text”
},
{
“id”: 2,
“specification”: “Personal Accidents”,
“header”: “Te dhenat e Klientit”,
“item”: “Numri i Siguruarve”,
“type”: “text”
},
{
“id”: 3,
“specification”: “Personal Accidents”,
“header”: “Te dhenat e Klientit”,
“item”: “Mosha Mesatare”,
“type”: “text”
},
{
“id”: 4,
“specification”: “Personal Accidents”,
“header”: “Te dhenat e Klientit”,
“item”: “Tjere”,
“type”: “text”
}
],
“Shuma e Sigurimit”: [
{
“id”: 5,
“specification”: “Personal Accidents”,
“header”: “Shuma e Sigurimit”,
“item”: “Objekti i Sigurimit”,
“type”: “list”
}
],
“Mbulimet e Kerkuara”: [
{
“id”: 6,
“specification”: “Personal Accidents”,
“header”: “Mbulimet e Kerkuara”,
“item”: “Mbulimi”,
“type”: “list”
}
],
“Shenime”: [
{
“id”: 7,
“specification”: “Personal Accidents”,
“header”: “Shenime”,
“item”: “Shenime”,
“type”: “text”
}
],
“Primi total per Person”: [
{
“id”: 8,
“specification”: “Personal Accidents”,
“header”: “Primi total per Person”,
“item”: “Primi Neto”,
“type”: “text”
},
{
“id”: 9,
“specification”: “Personal Accidents”,
“header”: “Primi total per Person”,
“item”: “TVSH(18%)”,
“type”: “text”
},
{
“id”: 10,
“specification”: “Personal Accidents”,
“header”: “Primi total per Person”,
“item”: “Primi Total”,
“type”: “text”
}
]
}
}

so as you can see there are a couple of groups and each group has items inside, i need to get the group name and show it and than iterate threw items in each group to work with that data.

Solution :

data() {
return {
response: {},
}
},
methods: {
fetch() {
axios.get(‘DATA_URL’).then(({response}) => {
// here is json response of GET request. Assign it to this.response
this.response = response.data
})
}
}

<div v-for=”(items, title) in response” :key=”title”>
<b>{ title }</b>
<div v-for=”item in items” :key=”item.id”>
{ item.item }
</div>
</div>

[Vue.js] How to apply css style only for specific Vue component and its child?

my vue.js project use Vue.Router.

to apply css style for child components from parent.
but, i defined css style in parent, it is not applied in child components.
and my stylesheet use same class or id selector.

like blow:

list-one.css

.foo bar {
width: 120px;
}

not-related-for-list-one.css

.foo bar {
width: 200px;
}

two example css file use seperate different vue.js components(ListOne.vue, NotForListOne.vue)

in parent(vue.js components, ListOne.vue), i define css style several ways, like below:

<style src=”my/css/file/path/list-one.css” scope>
<script>import “my/css/file/path/list-one.css”</script>
<style scope>my css style here</style>

‘1’ is applied only parent component(ListOne.vue).

‘2’ is applied global(list-one.css and not-related-for-list-
one.css both applied ListOne.vue) because NotForListOne.vue.js has
<script>import “my/css/file/path/not-related-for-list-one.css”</script>.

‘3’ is same ‘1’.

ListOne.vue

<child-one></child-one>
<child-two></child-two>
<and-other-childs></and-other-childs>

list-one.css

/* it for ListOne.vue.js only */
.foo bar {
width: 120px;
}

/* it for chile-one only */
.foo1 bar {
display: none;
}

/* it for chile-two only */
.foo2 bar {
color: red;
}

.
.
.

I think most better ways is this:

ListOne.vue

….

<style>
.foo bar {
width: 120px;
}
</style>

ChildOne.vue

<style>
.foo1 bar {
display: none;
}
</style>

ChildTwo.vue

<style>
.foo2 bar {
color: red;
}
</style>

but css file is so big, it is so hard to split css for its vue.js components.

how to solve this?

Solution :

<style scope>my css style here</style>

try to remove the scope from style tag.

[Vue.js] Why does this function expect a semicolon only on IE11?

I’m working on some vuejs code for IE 11 compatibility. And I keep seeing an expected semicolon error with this function:

chemicalFilters: function (chemical)
{
var max = 0;
var min = 100;
for (var component of this.components)
{
if(component.component_name == chemical)
{
if (max < component.component_value)
max = component.component_value;
if(component.component_value != 0 && min > component.component_value)
min = component.component_value;
}
}

if(max == 0 && min == 100)
min = max;
else
{
min = Math.round(min*100);
max = Math.round(max*100);
}

this.component_filters.push({component_name: chemical, range:[min,max], min:min, max:max, originalRange:[min,max]});
},

Specifically this line:

if(component.component_name == chemical)

Solution :

The problem isn’t in the line you cited, but the line above it. The for…of construct was introduced in ECMAScript 2015, which is not fully supported by IE11. See MDN:

You could use a tool like Babel to transpile this into backward-compatible code, so this:

for (var component of this.components) {
//…
}

Would be transpiled to something like this (depending on the settings):

var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = this.components[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
//…

var component = _step.value;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}

[Vue.js] Getting error Cannot read property 'url' of undefined when data is present

I keep getting this error from a tag when attempting to render a component which looks like this:

<template>
<div v-if=”playlists” class=”playlists”>
<router-link
:to=”`/playlist/${playlist.id}`“
tag=”div”
v-for=”playlist in playlists”
:key=”playlist.id”
class=”playlist”
\>
<img :src=”playlist.images[0].url” alt=”bild”>
<h2>{playlist.name.toUpperCase()}</h2>
</router-link>
</div>
</template>

<script>
export default {
data() {
return {
playlists: null
};
},
beforeMount: function() {
if (this.$parent.$parent.loggedIn) {
this.spotify.getUserPlaylists().then(data => {
this.playlists = data.body.items;
});
}
}
};
</script>

All the data is present and I can render out the playlist.images[0] object but when attempting to render playlist.images[0].url it gives me the error.
The image array looks like this:

“images”: [
{
“height”: null,
“url”: “www.example.com",
“width”: null
}
]

How can I solve this?

Solution :

Render <img> when playlist is updated with API response:

change

<img :src=”playlist.images[0].url” alt=”bild”>

to

<img v-if=”playlist.images.length > 0” :src=”playlist.images[0].url” alt=”bild”>

[Vue.js] for loop in method vue js and replace caract

I must display data that i set in my user model, I get the info from the database but encode in html format.

there is already tried a lot of things like deserialize my json to put in the right format but I still have the output in pure html.

public function ReadUserData($action) {
if (strcmp($action, “read”) == 0) {
global $f3;
$user = $this->db->exec(“SELECT
users.username,
users.email,
users.mobile
FROM
users”);
return json_encode($user);
}
}

and in my vue.js js there is :

methods : {

getDataUsers: function() {
axios.get(“http://localhost/?action=read")
.then(function(response) {
if (response.data.error) {
app.errorM = response.data.message;
} else {
//app.users = response.data.users;
app.users = “{ @users }”;
console.log(app.users);
}
});
}
}

Actually my output is in html format :/

[{“username”:”toto”,”email”:”toto@gmail.com“,”mobile”:”0676565443”},{“username”:”jojo”,”email”:”jojo@gmail.com“,”mobile”:”0678654534”},{“username”:”jojo”,”email”:”jojo@gmail.com“,”mobile”:”0678654534”}]

“ where replace by &quot :/

I was thinking if it was possible to directly loop through the method and replace the “& quot” with real double quotes ?

Than’ks for the time !

Solution :

You must return json header from backend:

header(‘Content-Type: application/json’);
echo json_encode($data);

And accept json in front end:

axios.post(“/“, {
headers: {
‘Content-Type’: ‘application/json’
},
data
})

[Vue.js] Vue event handler, shared between all components

there is create a new vue.js instance and assigned it to my window, which should give me access to it throughout all components. This should give me access to events emitted anywhere within my application.

However for some reason this is not the case, can someone advise me?

app.js

window.vue.js = require(‘vue’);
window.events = new Vue();
window.app = new Vue({
el: ‘#app’,
methods: {
emit: function (name, params) {
window.events.$emit(name, params);
}
}
});

there is created a emit method within my app for my components to use, as simply calling v-on:change=”window.events.$emit(‘makeChanged’, $event)” was not working, I was getting the error that window is not defined.

I’m listening for any emitted events from the emit within my component as follows:

window.events.$on(‘makeChanged’, function(evt) {});

Edit:

Still can’t get this working…

app.js

Vue.component(‘models-select’, require(‘./components/results-text/ModelsSelect.vue’));

window.app = new Vue({
el: ‘#app’,
methods: {
emit(name, …params) {
this.$root.$emit(name, …params);
},

ModelsSelect.vue

export default {
props: [ ‘endpoint’, ‘makeId’, ‘modelId’, ‘typeId’],
mounted() {

this.$root.$on(‘make-changed’, function(evt) {
console.log(evt);
});

Within one of my views (add.blade.php)

<select name=”make” id=”makes” v-on:change=”emit(‘make-changed’, $event)” tabindex=”2” class=”{ old(‘make’) ? ‘ is-changed’ : ‘’ }”>

Solution :

You can’t use window in v-on::

API docs:

The expression can be a method name, an inline statement, or omitted if there are modifiers present.

To do what you want to do you should move access to window.events to the method:

<some-element v-on:change=”globalEmit(‘makeChanged’, $event)” />

{
methods: {
globalEmit(name, …params) {
window.events.$emit(name, …params);
}
}
}

Then, you can move this globalEmit method to the global mixin, rename it to $gemit (for example) and use it everywhere like this:

<some-element v-on:change=”$gemit(‘makeChanged’, $event)” />

Solution 2:

You can emit and listen events with $root element.

methods: {
emit(name, …args) {
this.$root.$emit(name, …args)
}
}

In components:

mounted() {
this.$root.$on(‘event-name’, this.handleEvent)
},
beforeDestroy() {
this.$root.$off(‘event-name’, this.handleEvent)
},
methods: {
handleEvent() {
// handle event (with optional args)
}
}