Software

Get element where Event is binded

Vuejs - Tue, 2017-08-29 12:15

I am binding an event to an element either using @click property in Vue (it behaves the same as javascript .onclick property).

The problem I have is when the event is called by propagating from the child DOM nodes click events I get the child DOM element as target property and I cannot find a way a clean way (without searching for the parents, as the element might me nested deep inside) to access the DOM element that the event was registered onto inside the event callback.

<td @click="onCellClick($event)"> <div class="text"> <span>default</span> </div> </td>

Categories: Software

How to render div's dynamically in Vue?

Vuejs - Tue, 2017-08-29 11:45

I want to add button on my page. When we click that button, above generates div where we can add photo. When we click twice, we have two divs etc. The problem is that my code is not generating... How I can achieve this?

And another question. How I can add +1 to identifier while adding another div?

<div v-for="row in rows" class="row"> <div v-model="test" class="col-3"> <photo :upload_url="" :parent="this" identifier="image01"></photo> </div> </div> <button type="button" class="button btn-primary" @click="addRow()">Add row</button> addRow() { this.rows.push({ test: "" <-- I DON'T KNOW WHAT TO DO HERE }); }
Categories: Software

Webpack+Babeljs cannot transpile ES6

Vuejs - Tue, 2017-08-29 11:41

I am building a javascript web application using vue-cli.

I noticed there is some const keywords in the compiled javascript code. And the browser said SyntaxError: Unexpected keyword 'const'. Const declarations are not supported in strict mode

The compiled code looks like:

"use strict"; eval("/*blabla*/ const xxx ...");

My babelrc:

{ "presets": [ ["env", { "modules": false, "targets": { "browsers": ["> 1%", "last 2 versions", "not ie <= 8"] } }], "stage-2" ], "plugins": ["transform-runtime"], "env": { "test": { "presets": ["env", "stage-2"], "plugins": ["istanbul"] } } }

The babel-loader configuration file is like:

{ test: /\.js$/, loader: 'babel-loader', include: [resolve('src'), resolve('test')] },
Categories: Software

Mapping JSON responses to model classes in Vue.js

Vuejs - Tue, 2017-08-29 11:15

Is there a best practice or de-facto library for Vue.js when it comes to mapping JSON API responses to "real" javascript Model objects? Coming from iOS Development, there are excellent libraries like JSONModel and MagicalRecord dealing with exactly this matter.

Most Vue.js tutorials/examples I could find just seem to work with the plain JSON responses. IMHO, this gets cumbersome real quick. For instance, when dealing with raw data like date strings which would be better of as real Date objects. So it involves some sort of (de)serialization functionality. Also, it'd be nice to have stuff like Foo.findAll(), Foo.findById(1) or foo.save() to create appropriate calls to the backend service.

I found vue-model which looks promising, but it doesn't seem to have a lot of traction. This makes me wonder, if mapping JSON to objects is just not something a lot of people do in their SPAs? What are alternative approaches?

Categories: Software

vue-i18n : how to use inside vue instance filter

Vuejs - Tue, 2017-08-29 11:14

I want to use a filter to perform translations.
Problem is that 'this' doesn't point to my vue instance inside my filter function.

This is what I currently have.

inside my template I have this:

<p>{{ parking.status | translate }} </p>

inside my component I have this:

new Vue({ ... filters: { translate: function(value, vue) { return this.$i18n.t('MLAA-47'); }

The error I get is that this == undefined.
How do i point it to my vue instance inside my filter function ?

Categories: Software

What is the difference between watch and computed methods in vuejs

Vuejs - Tue, 2017-08-29 11:01

They both seem to do the same thing, and I can't tell when to use which

Categories: Software

v-if inside template props

Vuejs - Tue, 2017-08-29 10:32

so i have this setup, i have my component called dataviewer.vue that use to show table

<template> <table class="table"> <thead class="bg-primary"> <tr> <th v-for="item in thead"> <span>{{item.title}}</span> </th> </tr> </thead> <tbody> <slot v-for="item in model.data" :item="item"></slot> </tbody> </table> </template> <script> import Vue from 'vue' import axios from 'axios' export default { props: ['source', 'thead'], data() { return { model: { data: [] }, } }, beforeMount() { this.fetchData() }, methods: { fetchData() { var vm = this axios.get(this.source) .then(function(response) { Vue.set(vm.$data, 'model', response.data.model) }) .catch(function(error) { console.log(error) }) } } } </script>

and then i just call this component in my article index.vue file

<div class="page-container"> <div class="page-content"> <div class="content-wrapper"> <data-viewer :source="source" :thead="thead"> <template scope="props"> <tr> <td>{{props.item.name}}</td> <td>{{props.item.creator}}</td> <td> <i class="icon-checkmark5" v-if="props.item.publish === '0'"></i> <i class="icon-cancel-circle2" v-else></i> {{props.item.publish}} //for testing purpose to see returned value </td> <td>{{props.item.created_at}}</td> </tr> </template> </data-viewer> </div> </div> </div> <script type="text/javascript"> import DataViewer from '../../components/dataviewer.vue' export default{ components:{ DataViewer }, data(){ return{ source: '/api/article', thead: [ {title: 'Name', key: 'name', sort: true}, {title: 'Creator', key: 'creator_id', sort: true}, {title: 'Publish', key: 'publish', sort: true}, {title: 'Created', key: 'created_at', sort: true} ], } } } </script>

as you can see in my article index.vue file inside <template scope="props"> i then write my html for showing table row. And there is one column (props.item.publish) where i don't want to just show raw data from database but do some condition where v-if="props.item.publish === '0'" then i will render some checklist icon and otherwise if it is not then it will render cancel icon

<td> <i class="icon-checkmark5" v-if="props.item.publish === '0'"></i> <i class="icon-cancel-circle2" v-else></i> {{props.item.publish}} //for testing purpose to see returned value </td>

but when i run it, it just render cancel icon for all rows... so my question is how to do v-if inside it? i also thinking doing computed property but i can't get access props.item.publish from script

Categories: Software

getting undefined when getting each component in browser console using vuejs

Vuejs - Tue, 2017-08-29 10:28

when i type this($vm0.$children.forEach(tab => console.log(tab.name)); in console getting undefined iam just learning vuejs and creating components. my html file is here

<!DOCTYPE html> <html> <head> <title></title> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.0/vue.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.5.1/css/bulma.css"> <style type="text/css">body {padding-top:40px; }</style> </head> <body> <div id="root" class="container"> <tabs> <tab name="About Us" :selected="true"> <h1>Here is the content for About us tab.</h1> </tab> <tab name="About Our Culture"> <h1>Here is the content for cukture tab.</h1> </tab> <tab name="Contact tab"> <h1>Here is the content for contact us tab</h1> </tab> </tabs> </div> <script src="js/11.js"></script> </body> </html>

my js code is here

Vue.component('tabs', { template: ` <div> <div class="tabs"> <ul> <li class="is-active"><a>Pictures</a></li> <li><a>Music</a></li> <li><a>Videos</a></li> <li><a>Documents</a></li> </ul> </div> <div class="tabs-details"> <slot></slot> </div> </div> `, mounted() { console.log(this.$children); }

}); Vue.component('tab',{ template: <div><slot></slot></div> , props: { name: { required : true } } }) new Vue({

el: "#root"

})

Categories: Software

laravel vue.js select list from databse

Vuejs - Tue, 2017-08-29 10:12

I just begun Vue.js and I have some issues with it :

I’m currently working with Laravel, a PHP framework.

I would like to do a select list for a research, i have different applications but there are all related to different countries. And when I select a country I would like that only apps with this country appear.

I used to use php but we have to reload the page each time and it’s not very quick and easy to use.

My main page is like that :

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>VueJS</title> <link rel="stylesheet" href=""> <script src="/js/vue.js"></script> </head> <body> <div id="app"> <div id="country"> <select v-model="selectedcountry"> <option v-for="option in options" v-bind:value="option.value">@{{ option.text }} </option> </select> <span>Sélectionné : @{{ selectedcountry }}</span> </div> <script src="/js/app.js"></script> </body> </html>'

And my app.js :

new Vue({ el: '#country', data: { selectedcountry: 'All Countries', options: [ { text: 'All Countries', value: 'world' }, { text: 'Denmark', value: 'denmark' }, { text: 'France', value: 'france' }, ] } })'

And in my database my table is : "cards" with an attribute call "country".

How can i do that ?

Thank for you any ideas or solutions :slight_smile:

Categories: Software

vuex unknown mutation type in registered module

Vuejs - Tue, 2017-08-29 10:10

i just wanna create dynamic registered module in vuex, but it seems doesnt work. this is my store file

import Vuex from 'vuex' import descriptionModule from './module/descriptionModule'; const {state: stateModule, getters, mutations} = descriptionModule; const createStore = () => { return new Vuex.Store({ state: { descriptions: [], }, mutations: { addDescriptions(state, payload){ state.descriptions.push(state.descriptions.length + 1); createStore().registerModule(`descriptionModule${payload}`, { state: stateModule, getters, mutations, namespaced: true // making our module reusable }); } } }) }; export default createStore

and this is my custom module that i will registered

const state = () => { return {description: ''} }; const getters = { description: (state) => state.description }; const mutations = { updateDescription(state, payloads){ state.description = payloads; } }; export default { state,getters,mutations }

and then this is my custom methods that will call addDescriptions mutation and commit updateDescription from registeredModule

beforeMount(){ console.log("hahahaha"); this.$store.commit('addDescriptions', this.id); }, ... more code .... methods: { onType(editor, content){ console.log(this.$store.state.a); console.log(this.$store.state); console.log(this.$store); this.$store.commit(`descriptionModule${this.id}/updateDescription`, content, {root: true}) } }

every onType called, i get an error unknown mutation type: descriptionModuleeditor1/updateDescription in browser.

currently iam following this solution link , but it doesnt work for me :(

can anyone solve this,,, sorry for bad english

Categories: Software

Vue scroll event inside jquery mobile

Vuejs - Tue, 2017-08-29 10:06

I'm integrating a small vue app inside a jquery mobile site. What's happening: i'm going for the first time inside the site on the URL of the page that cointains the vue app. Then i scroll and I get the console.log saying "scroll".

If i go to another page and come back to the page containing the vue app OR if i enter the site with a URL to another page and I go to the page with the vue app, I get the console.log saying "applyEvents" but when I scroll I get nothing

// app.js methods: { scroll(){ console.log("scroll") }, applyEvents(){ console.log("applyEvents") jQuery(window).on('scroll', this.scroll); } }, mounted(){ this.applyEvents() }, beforeDestroy(){ console.log("beforeDestroy") jQuery(window).off('scroll', this.scroll); } // main.js jQuery(document).ready(() => { var vueFeed = null; jQuery(window).on("pagebeforehide", (event) => { if(vueFeed){ console.log("destroying") vueFeed.$destroy() } }) jQuery(window).on("pageshow", (event) => { if(jQuery.mobile.activePage.attr('id') == "page-dashboard"){ vueFeed = new Vue({ el: '#feed', template: '<App/>', components: { App } }) } }) })

// edit If i go from the console and do $vm.applyEvents() i get the console.log saying "applyEvents" and the scroll works

Categories: Software

How to make this progress bar into a circle javascript vuejs css

Vuejs - Tue, 2017-08-29 07:15

I have been having the most stressful time making this progress bar into a circle progress bar. I've tried using progressbar.js but that isn't helping me.

html

<div id="myProgress"> <div id="myBar" v-bind:style="{width: activewidth}">{{activewidth}} done</div> </div> <br>

js

moveBar: function () { let numLessons = 1 for (let i = 0; i < this.opts.lessonsArr.length; i++) { numLessons++ } // this.numComp is the percentage of the bar this.numComp = (numLessons % 5).toString() console.log('numLeft', this.numComp) // this.activewidth determines the % completed, so the green bar this.activewidth = (this.numComp * 20 + '%').toString() console.log('thiswidth', this.activewidth) }

css

#myProgress { width: 95%; background-color: grey; margin-left: auto; margin-right: auto; } #myBar { width: 30%; height: 20px; background-color: #2ecc71; text-align: center; /* To center it horizontally (if you want) */ line-height: 20px; /* To center it vertically */ color: white; }

what i have now: image of progress bar

this is in vuejs, and in a big project :( please help me do this in pure css (if possible)

Categories: Software

[Vue warn]: Unknown custom element

Vuejs - Tue, 2017-08-29 04:17

I am new to vuejs, While writing one js code inside laravel framework, I am getting error :

[Vue warn]: Unknown custom element: <app> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

App.js

import Vue from 'vue' import Vcinfo from './Vcinfo.vue' import router from './router' const app = new Vue({ el: '#vcinfo', components: { Vcinfo }, template: '<app></app>', router })

please suggest what to do?

Categories: Software

Uncaught Error: [vee-validate] No such validator '1234567' exists

Vuejs - Tue, 2017-08-29 03:56

I am using vee-validate to validate the register form and i have made the code as follows,

<form @submit.prevent="signUp()"> <div class="form-group" :class="{'has-error': errors.has('register.mobile_number') }" > <input v-model="register.mobile_number" v-validate="register.mobile_number" data-vv-rules="required" class="form-control" type="number" placeholder="Mobile Number"> </div> <div class="form-group" :class="{'has-error': errors.has('register.email') }" > <input v-model="register.email" v-validate="register.email" class="form-control" type="email" data-vv-rules="required|email" placeholder="Email"> </div> <div class="form-group" :class="{'has-error': errors.has('register.password') }" > <input v-model="register.password" v-validate="register.password" name="password" data-vv-rules="required" class="form-control" type="password" placeholder="Password"> </div> <div class="form-group" :class="{'has-error': errors.has('register.confirm_password') }" > <input v-model="register.confirm_password" v-validate="register.confirm_password" name="confirm_password" data-vv-as="password" data-vv-rules="required|confirmed:password" class="form-control" type="password" placeholder="Confirm Password"> </div> <div class="modal-footer btn-center"> <button type="submit" class="btn btn-default">Sign Up</button> </div> </form>

And the script was:

export default { data() { return { register: { mobile_number: '', email: '', password: '', confirm_password: '', }, } }, methods: { signUp() { this.$validator.validateAll().then((result) => { }); axios.post(config.apiDomain+'/Home',this.register).then(response=>{ }); } }, }

And also imported vee-validate in main.js as,

import VeeValidate from 'vee-validate'; Vue.use(VeeValidate);

But if we enter anything inside the input box, it is throwing error as

Uncaught Error: [vee-validate] No such validator '12312321' exists.

Whatever thing i enter inside any of the input box, it is showing the same error. Kindly help me to resolve this issue.

Categories: Software

vue-i18n use inside of functional component

Vuejs - Tue, 2017-08-29 03:33

I'm trying to use vue-i18n inside of functional component. How would that be included? I'm using most recent Vue 2.x and vue-i18n 7.x Thanks

Categories: Software

Importing lokijs library on Vue Component

Vuejs - Tue, 2017-08-29 00:19

I am pretty new to vue-cli, I am developing an app with electron and vue. I am using vue-cli and everything is working fine. But one of my requirements is too use lokijs to persist my data.

I have managed to import lokijs to my component but i would like it to be globally, also i want to save de lokijs db to a local file.

Do you have any hints for this requirements?

Here is my partial code:

<template> <div class="home-page"> <h1 class="title">{{ title }}</h1> <h2>{{ counter }}</h2> <icon class="beer-icon" name="beer"></icon> <button v-on:click="createDB()">Add 1</button> </div> </template> <script> import Loki from 'lokijs' export default { name: 'homePage', data () { return { title: 'Home Page', counter: 1 } }, created () { // alert('created') }, methods: { createDB () { let db = new Loki('example.db') var children = db.addCollection('children') // Insert a document: children.insert({ name: 'Sleipnir', legs: 8 }) let a = children.get(1) alert(a.name) } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped lang="scss"> </style>

Hope my question is clear.

Categories: Software

Vue Draggable add event picking up incorrect item

Vuejs - Mon, 2017-08-28 22:42

I'm using Vue.Draggable: https://github.com/SortableJS/Vue.Draggable

Fiddle: https://jsfiddle.net/o5pcLhhj/

I have two lists setup and the ability to drag between them. I've added an onAdd event to the second list so I can detect when something gets added to that list:

onAdd: function(evt) { console.log(evt.item); }

Now if I drag "John" or "Joao" to the 2nd list, the event is not correctly picking up the item that was dragged in. It seems to be off by one index. If I drag "Jean" in, it correctly picks up this item.

What am I doing wrong here?!

Fiddle: https://jsfiddle.net/o5pcLhhj/

Thank you in advance!!

Categories: Software

Multiple <td> elements per v-for loop

Vuejs - Mon, 2017-08-28 22:35

In my Vue app, I am looping through an array of schools. Each school has a name, an array of teacher counts (one per grade), and an array of student counts (one per grade).

The following code works, but only because I am coding the <td>s manually.

new Vue({ el: '#app', data: { schools: [ { name: 'Lincoln', teachers: [3, 4, 1], students: [55, 42, 39] }, { name: 'Oak Grove', teachers: [1, 2, 1], students: [31, 36, 23] }, { name: 'Fairview', teachers: [1, 3, 2], students: [30, 26, 39] }, ], }, }); thead th, tbody td { text-align: center; } <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script> <table id="app" class="table"> <thead> <tr> <th rowspan="2"></th> <th colspan="2">Grade K</th> <th colspan="2">Grade 1</th> <th colspan="2">Grade 2</th> </tr> <tr> <th>Teachers</th> <th>Students</th> <th>Teachers</th> <th>Students</th> <th>Teachers</th> <th>Students</th> </tr> </thead> <tbody> <tr v-for="school in schools"> <th>{{ school.name }}</th> <td>{{ school.teachers[0] }}</td> <td>{{ school.students[0] }}</td> <td>{{ school.teachers[1] }}</td> <td>{{ school.students[1] }}</td> <td>{{ school.teachers[2] }}</td> <td>{{ school.students[2] }}</td> </tr> </tbody> </table>

Notice repeated lines:

<td>{{ school.teachers[x] }}</td> <td>{{ school.students[x] }}</td>

It is not too much of a problem on this simplified example. But in my real project, there are many more columns and sub-columns. Is there a way to do a repeating loop to display the <td>s?

I've tried another v-for loop, but since it's inside the <tr>, only <td> and <th> are allowed.

Categories: Software

vuejs dynamic directive bound to data/computed value

Vuejs - Mon, 2017-08-28 21:33

I use vuetifyjs for my html & css. One of it's components changes it's styling/behavior based on a custom directive (error || success || warning || info)

e.g.

<v-snackbar error></v-snackbar> <v-snackbar success></v-snackbar>

Is there a way to bind the directive to a data or computed value?

something like this:

<v-snackbar {{ type }}></v-snackbar>
Categories: Software

passing user input to another component in vue.js

Vuejs - Mon, 2017-08-28 20:40

I'm fairly new to vue.js but I couldn't seem to find a concrete answer for what I'm looking for but I also am new enough that I might have been looking right at the answer and not known it. So.

What I'm trying to do is create a 1 page application that tracks turns basically. It will:

  1. takes user input on how many users are participating
  2. ask for the names of all the users
  3. use the names which I currently have saved in an array, to build user cards, just showing the name of each user.
  4. You will click your name when your turn is over and the next persons card/button will 'raise' and then they click it, etc.

I'm currently having a hard time with that 3rd part. I need to figure out how I can pass this array of users to my other component's data object so I can use vue.js to loop and just spit the cards out.

initial-start.vue - template

<template lang="html"> <div> <div> <h1>How many users?</h1> <input type="number" id="myNumber" value="0"> <button v-on:click="getUsers">Submit</button> <app-area v-if="users > 0 && users < 5"></app-area> </div> </div> </template>

initial-start.vue - script

<script> export default { methods:{ getUsers(){ var counter = 0; var users = []; var x = document.getElementById("myNumber").value; if (x <= 0){ alert('Please choose a number greater than 0.'); }else if(x > 4){ alert('Maximum 4 users!'); }else{ alert('thank you for that.') } while(counter < x){ name = prompt('Please enter your names.'); users.push(name); counter++; } return users; } } } </script>

app-area.vue - template

<template lang="html"> <div> <h1>Turn: {{ turn }}</h1> <userCards></userCards> </div> </template>

app-area.vue - script

<script> export default { data() { return{ turn: 0, users: [] } }, props: ['users'] } </script>

The question is, "How do I get the array from the getUsers() function, into the app area where I would be able to loop like

<userCards v-for="user in users"></userCards>

and have a card/button for each person entered ?"

Categories: Software

Pages