Vuejs

Subscribe to Vuejs feed
most recent 30 from stackoverflow.com 2017-09-14T21:10:11Z
Updated: 7 years 1 week ago

VueJs draggable sortable cancel adding element without cancel the drag preview

Fri, 2017-08-18 13:49

I am working with draggable and sortable with vueJS, and i am trying to accomplish a thing, basicly i have 2 lists, the first one is a list with some sections like (table, paragraph ...) the second one are building blocks with this sections, but basicly the flow i need is, drag a element from list1 to list2, but don't add the element this is important because i need to open a modal first to set the properties of the element that will be added, and then add it to the futureIndex.

At the moment i have a solution where i can do almost what i need, i just need to understand how to cancel the addition of the element without struggling with the preview of the dragging, with preview i mean this:

enter image description here

i wanna see the adition of the elment at the middle.

So my first list i have done this:

<draggable v-model="sectionList" class="dragArea" @end="changeView()" :move="moveItem" :options="{group:{ name:'sections',pull:'clone',put:false }}"> <div v-for="(section, index) in sectionList" class="card card-color list-complete-item"> <div class="card-block"> <h4 class="card-title text-center">{{section.text}}</h4> </div> </div> </draggable> methods: { addParagraph() { this.$set(this.paragraph, 'key', this.key); this.$set(this.paragraph, 'text', this.text); this.$set(this.paragraph, 'fontSize', this.fontSize); this.$store.commit("appendToDocument", this.paragraph) }, changeView: function () { this.$store.commit("changeCurrentView", this.sectionList[this.dragedIndex].component); this.show(); }, show() { this.$modal.show('modalSection'); }, hide() { this.$modal.hide('modalSection'); }, currentIndex(evt) { console.log(evt.draggedContext.index); }, moveItem(evt) { // future index of the modal that will be draged console.log(evt.draggedContext.futureIndex); this.dragedIndex = evt.draggedContext.index; } },

the second list is not that important, as long as i can understand with this post, how to move the item without adding it and see the preview. i had added the return false at the end but with that i can't see the preview and i can't drag elements between the same list.

Any help with this?

Categories: Software

Saving state on back button press in vue-electron

Fri, 2017-08-18 13:30

I want to make a desktop app in vue-electron. I am new to both vue.js and electron. I am having some problems while managing state.

When I click on login button https://cloudup.com/cFl9MTY6cnn I send data i.e sessionId and username to next screen https://cloudup.com/c76fmL8OGbF and on this screen I display these props https://cloudup.com/csahnc6Z04J using this code https://cloudup.com/cdR0F6Qyt-3 but as I go to the third screen https://cloudup.com/c0F1ztX8qu3 and then come back then this data disappears https://cloudup.com/cTFW32DxeRa

I am going back to second screen using router.go(-1) https://cloudup.com/cvpxk4GsIRx

The vue-router documentation https://router.vuejs.org/en/essentials/navigation.html says that “router.push method pushes a new entry into the history stack, so when the user clicks the browser back button they will be taken to the previous URL.” and router.go(n) "This method takes a single integer as parameter that indicates by how many steps to go forwards or go backwards in the history stack"

However in my case, lifecycle hooks are called again when I go back to history stack. So does that mean when we come to previous page that component is created again and not popped from stack. I actually don’t want to refresh / reload the page on back button.

Categories: Software

Toggle in loop?

Fri, 2017-08-18 13:10

I wish to toggle (show/hide) a list when clicking on a title, but cant get the following to work

I have this:

<!-- Title --> <div v-for="(subitem, index) in item" v-if="index === 0" @click="toggle(subitem)"> {{subitem.system_name}} - ({{item.length}}) </div> <!-- All title items that should expand on click "Title" --> <div v-if="subitem.clicked"> <p>{{subitem.system_name}}</p> </div>

When clicking on the im triggering a toggle function called toggle, that sets a property on the item "clicked" to true or false (I should mention that this property does not already exist on the object, and I haven't got the possiblity add it, as we get the JSON from an API)

The toggle function is this:

toggle: function (data) { data.clicked = !data.clicked; },

Now, when I do this above, I get an error saying: "Property or method "subitem" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option"

Im guessing I get this because the "clicked" property doesnt exist in the object... So how do I work around this? Can't see any real solution ?

Categories: Software

Date Timestamp formatting in vuejs

Fri, 2017-08-18 12:45

I am building a SPA in vue js and I am getting date in a timestamp format i want it to be more human readable hence I tried using momentjs which didn't work for me. What is the best way to go in vue js

Categories: Software

How to get data from a nested component

Fri, 2017-08-18 12:18

I have this 3 components. One is for the Question component that consists of questions. The second is for QuestionOption for the list of choices. And the third one is my main component. How do I get the data from the QuestionOption component to the main component. Since the main component is not the parent of the QuestionOption I cannot use the $emit. Also I make use of bus but it's not working on my case. Can anyone help me on this.

Also how will I get all the chosen answers after the submission of the button? Here is my code.

Main.vue

<div class="quiz-background"> <question v-for="(question, $indexParent) in randomQuestions" :question="question['doc'].question" :choices="question['doc']['choices']" :indexParent="$indexParent" :correctAnswer="question['doc'].correctAnswer" > </question> <section style="margin:16px"> <v-ons-button modifier="cta" @click="submit" v-show="!isDone" >Submit </v-ons-button> </section> </div> <script> submit() { bus.$on('choosed', function(answer) { console.log(answer); }); }, </script>

Question.vue

<template> <v-ons-list> <v-ons-list-header> {{indexParent + 1}}.{{ question }} </v-ons-list-header> <question-option v-for="(choice, key, $index) in choices" :choice="choice" :letter="key" :indexParent="indexParent" :index="$index" :correctAnswer="correctAnswer" > </question-option> </v-ons-list> </template> <script> import QuestionOption from './QuestionOption.vue'; export default { props: ['question', 'indexParent', 'choices', 'correctAnswer'], components: { QuestionOption } } </script>

QuestionOption.vue

<template> <v-ons-list modifier="inset"> <v-ons-list-item :name="'question-' + indexParent" :modifier="longdivider" :key="letter" tappable > <label class="left"> <v-ons-radio :name="'question-' + indexParent" :input-id="indexParent + '-' + index" :value="letter" :key="letter" v-model="chosenAnswer" @change="appendAnswer($event)" > </v-ons-radio> </label> <label class="center" :for="indexParent + '-' + index" :class="{'success' : isSuccess, 'danger' : isDanger}" > {{ letter }} . {{ choice }} </label> </v-ons-list-item> </v-ons-list> </template> <script> import Vue from 'vue'; var bus = new Vue(); export default { data() { return { chosenAnswer: '' } }, props: ['letter', 'choice','correctAnswer', 'indexParent', 'index'], computed: { isSuccess () { return this.chosenAnswer === this.correctAnswer; }, isDanger () { return this.chosenAnswer !== this.correctAnswer; } }, methods: { appendAnswer (event) { var answer = event.target.value; bus.$emit('choosed', answer); } } } </script>
Categories: Software

Mounting VueJS to top level div & user submitted content

Fri, 2017-08-18 12:03

I'm building an small guide website which allows users to submit articles about a game. There are a number of components on the website written in Javascript, using VueJS. The majority of the site is powered by Laravel and renders in plain HTML except these components. At the moment, I have VueJS mounted to #app, or my top level div element, as is default with the Laravel configuration.

I then drop components into the page as usual () and VueJS will pick these up and render them appropriately. Awesome.

However, there are a few situations where user submitted content will actually break the page. For example, if double brackets are used {{ example }}, this is VueJS syntax, and it will pick up the tags and refuse to render the page. Another example is if I need to use tags in the body which aren't VueJS, for example embedding a Twitch TV stream.

It seems like because Vue is mounted to #app, absolutely everything inside that shares VueJS syntax or is a script tag will break the page.

Is mounting to #app the wrong idea? Should I be splitting all of my components and somehow launching them with more granularity? I really like the approach of just dropping in Vue components into code with .

Thanks!

Categories: Software

Ask for front end book recommendation [on hold]

Fri, 2017-08-18 11:29

I'm asking this question on behalf of my friend.

I want to get a front-end job next year. There's about half a year for me to practice. I have the basic knowledge of html/css/js. Now I want to learn some architecture to do some projects.

I'd like to follow some books' guide and develop a website. Any recommendation?

Here's the requests:

  1. The book will teach you step by step to develop a website. The ideal situation is that the book develops a website, and each chapter adds some components and functions to this website.
  2. The book uses and teaches you to use one architecture like vue.js, angular.
  3. The book includes back-end knowledge, if the back-end is node.js, it would be perfect.

Can anyone give me some recommendations? Web tutorial and book are both ok!

Thanks in advance.

Categories: Software

How to get random element in Vue.js

Fri, 2017-08-18 11:16

I have 3 hero images and their content and I want to display them randomly when user refreshes the page!

Basically I am trying to display random div using Jquery on loading page, but issue is that the size of hero image is large and by using Jquery, all these 3 images start loading in DOM which affects the speed of page.

Is there any solution for this in Vue.js for loading that one specific div at a time, not all 3 divs when user refreshes the page!?

jQuery Code:

mounted() { var random = Math.floor(Math.random() * $('.slider-item').length); $('.slider-item').eq(random).show(); },
Categories: Software

How do you import components having same name on Vue Router

Fri, 2017-08-18 11:09

I'm working on a VueJs project and now structuring my router records. I've realized I could be having different components having same name. How do I import them and use them in the route records? How do I make their names unique when configuring the router?

import Vue from 'vue' import Router from 'vue-router' import AllUsers from '../components/Sales/AllUsers' import AllUsers from '../components/Finance/AllUsers' ... export default new Router({ routes: [ { path: '/', name: 'home', component: Home }, { path: '/sales/users', name: 'sales-users', component: AllUsers }, { path: '/finance/users', name: 'finance-users', component: AllUsers }

I have used a hypothetical example here (since I could as well call the components SalesUsers and FinanceUsers) but you will agree there are times when components will really have same name. How do I handle that, given that I need to specify the component for each route record?

PS: Slightly new to VueJS, so advice on improvements and best practices is welcome.

Categories: Software

Vuejs single file component (.vue) model update inside the <script> tag

Fri, 2017-08-18 10:38

I'm new to vuejs and I'm trying to build a simple single file component for testing purpose.

This component simply displays a bool and a button that change the bool value. It also listen for a "customEvent" that also changes the bool value

<template> {{ mybool }} <button v-on:click="test">test</button> </template> <script> ipcRenderer.on('customEvent', () => { console.log('event received'); this.mybool = !this.mybool; }); export default { data() { return { mybool: true, }; }, methods: { test: () => { console.log(mybool); mybool = !mybool; }, }, }; </script>

The button works fine. when I click on it the value changes. but when I receive my event, the 'event received' is displayed in the console but my bool doesn't change.

Is there a way to access the components data from my code?

Thanks and regards, Eric

Categories: Software

Symfony 3 routes + VueJS's index.html with F5 falls to 404

Fri, 2017-08-18 10:22

I try connect Vue.js with Symfony 3, but have no idea how to create proper route configuration for it. Project tree looks like that:

Project root - app/ - Resources/ - views/ - default/ - web/ - assets/ - static/ index.html // here Vue is included

From Symfony I provide some REST API, the routes to it are prefixed by /api/. And I have some routes in Vue SPA. The problem is if I press F5 in browser on any Vue route, which of course is not included in my routing.yml, browser returns 404 error from Symfony. One half-working decision is when I put content of index.html to Resourses/views/default/twig with such a route:

fallback_for_vue: path: /{req} defaults: { _controller: 'AppBundle:Default:index' } requirements: req: ".+"

,taken from another question, but this is not actually what I need, because I don't want my frontend programmer to go somewhere except web/ folder. So which configuration of Symfony routes I should use?

Categories: Software

Vue - named exports in *.vue files are ignored

Fri, 2017-08-18 10:13

I am getting warnings in the console that the:

named exports in *.vue files are ignored.

On looking around about the problem I came across this where it is suggested that if I upgrade the vue loader to the version 8.3.1 that all the warnings are gone. On checking the package.json file in the node_modules/vue-loader folder I saw that I already have that version:

{ "_args": [ [ { "raw": "vue-loader@^8.3.1", "scope": null, "escapedName": "vue-loader", "name": "vue-loader", "rawSpec": "^8.3.1", "spec": ">=8.3.1 <9.0.0", "type": "range" },

How can I fix this and get rid of this warnings?

Categories: Software

pass dragable v-for option to function when drag is over

Fri, 2017-08-18 10:02

I want to drag a element from list 1 to list 2 and call a method when i leave the drag, i almost accomplish everything with it, my only problem here is that i only can put the @end(responsable for detecting the end of dragging) in the draggable tag, and the div that has the v-for is a child from it, so i can't get the element from the list to pas to a function in my draggable.

This is my code:

<draggable v-model="sectionList" class="dragArea" @end="changeView(section.component)" :options="{group:{ name:'sections', pull:'clone', put:false }}"> <transition-group name="list-complete"> <div v-for="(section, index) in sectionList" @click="changeView(section.component)" :key="section.text" class="card card-color list-complete-item"> <div class="card-block"> <h4 class="card-title text-center">{{section.text}}</h4> </div> </div> </transition-group> </draggable>

the @end=changeView receives a parameter, that parameter should be the section.component that comes from the list, any help to get the element?

Thank you

Categories: Software

Filtering in Vue, nested array

Fri, 2017-08-18 10:00

Im trying to create a computed function that can filter out my Object by "alarms",

So I've created a computed function, called filteredAlarms, and in my loop im doing a v-for loop:

<li class="event-log__item timeline__item" v-for="(item, key) in filteredAlarms" :key="key">

And in my filter im trying to do the following:

let filteredAlarms = Object.keys(this.eventLog).forEach(key => { this.eventLog[key].filter(item => { return item.type.indexOf("alarm") > -1 }); }); return filteredAlarms

Unfortunately, this doesn't work - Im getting a an error: TypeError: this.eventLog.filter is not a function

What am I doing wrong? :)

The object im trying to filter is something similar to the one below:

"system_events": { "1013": [{ "id": 25899, "timestamp": "2017-08-15T21:26:42Z", "type": "alarm", "code": 190, "title": "", "description": "", "appeared": "2017-08-15T21:26:40Z", "disappeared": null, "acknowlegded": null, "solved": null, "system_name": "Randers pr 44b sidste station" }, { "id": 26157, "timestamp": "2017-08-15T21:32:17Z", "type": "warning", "code": 190, "title": "", "description": "", "appeared": "2017-08-15T21:32:06Z", "disappeared": null, "acknowlegded": null, "solved": null, "system_name": "Randers pr 44b sidste station" }, { "id": 26387, "timestamp": "2017-08-15T21:37:38Z", "type": "info", "code": 190, "title": "", "description": "", "appeared": "2017-08-15T21:37:33Z", "disappeared": null, "acknowlegded": null, "solved": null, "system_name": "Randers pr 44b sidste station" } ], "1015": [{ "id": 23777, "timestamp": "2017-08-15T20:38:08Z", "type": "alarm", "code": 191, "title": "", "description": "", "appeared": "2017-08-15T20:38:00Z", "disappeared": null, "acknowlegded": null, "solved": null, "system_name": "Favrskov Svenstrup gyvelvej" }, { "id": 23779, "timestamp": "2017-08-15T20:38:08Z", "type": "alarm", "code": 190, "title": "", "description": "", "appeared": "2017-08-15T20:37:58Z", "disappeared": null, "acknowlegded": null, "solved": null, "system_name": "Favrskov Svenstrup gyvelvej" }, { "id": 23841, "timestamp": "2017-08-15T20:39:41Z", "type": "alarm", "code": 192, "title": "", "description": "", "appeared": "2017-08-15T20:39:31Z", "disappeared": null, "acknowlegded": null, "solved": null, "system_name": "Favrskov Svenstrup gyvelvej" }, { "id": 25243, "timestamp": "2017-08-15T21:12:03Z", "type": "alarm", "code": 191, "title": "", "description": "", "appeared": "2017-08-15T21:11:55Z", "disappeared": null, "acknowlegded": null, "solved": null, "system_name": "Favrskov Svenstrup gyvelvej" }, { "id": 25529, "timestamp": "2017-08-15T21:18:11Z", "type": "alarm", "code": 190, "title": "", "description": "", "appeared": "2017-08-15T21:18:00Z", "disappeared": null, "acknowlegded": null, "solved": null, "system_name": "Favrskov Svenstrup gyvelvej" } ],

} ]

}

Categories: Software

Redraw highchart on vue model

Fri, 2017-08-18 09:57

I am trying to use vuejs and highchart on displaying the data. When I change the model, I want to redraw the chart data. Any idea ? This is what I tried to do?

var options = { title: { text: 'Temperature ', x: -20 //center }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] }, yAxis: { title: { text: 'Temp' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { valueSuffix: '' }, legend: { layout: 'vertical', align: 'right', verticalAlign: 'middle', borderWidth: 0 }, series: [{ name: 'Japan', data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6] }, { name: 'Australia', data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8] }] };

For the Vue

return new Vue({ el: '#app', data: { modelValue = '', options:options }, methods: { changetemp = function(){ //The option value should be change here } },

When the model value is changed, I want the options to be redrawn

Categories: Software

How to show list dom revered in vue js?

Fri, 2017-08-18 08:52

I want to show my vue js list in v-for reversed on dom. Object should not be change. Just created reversed list by v-for. HTML default list is top to bottom, I want to append items from bottom to top in dom. You can see telegram web messages list that do this with ng-repeat.

Is there any way to do this by v-for? or another option?

Thanks.

Categories: Software

How can I get data inside of Vue from database like config or smth>

Fri, 2017-08-18 08:50
var app = new Vue({ el: '#app', data: { positions: [] }, created() { axios.get('/config').then(function(response) { this.$set(this.positions, "positions", response.data[0].dragedPositions); }); console.log(this.positions.positions); } });

I have configs for my app in database and I want to use them inside of Vue instance (here are positions of my elements). But when im getting "this.positions.positions" Im getting empty string.. How can I do that?

Categories: Software

how to mock module when using jest

Fri, 2017-08-18 08:48

I'm using Jest for unit test in a vuejs2 project but got stuck in mocking howler.js, a library imported in my component.

Suppose I have a component named Player.vue

<template> <div class="player"> <button class="player-button" @click="play">Player</button> </div> </template> <script> import { Howl } from 'howler'; export default { name: 'audioplayer', methods: { play() { console.log('player button clicked'); new Howl({ src: [ 'whatever.wav' ], }).play(); } } } </script>

then I have its test file named Player.spec.js. Test code was written based on the answer here, but the test failed since called wasn't set as true. It seems that mocked constructor won't be called when running this test.

import Player from './Player'; import Vue from 'vue'; describe('Player', () => { let called = false; jest.mock('howler', () => ({ Howl({ src }) { this.play = () => { called = true; console.log(`playing ${src[0]} now`); }; }, })); test('should work', () => { const Constructor = Vue.extend(Player); const vm = new Constructor().$mount(); vm.$el.querySelector('.player-button').click(); expect(called).toBeTruthy(); // => will fail }) })

Though I'm using Vuejs here, I considered it as a more general question related to the usage of Jest's mock API, but I'm not able to get further.

Categories: Software

Where do I store shareable data in vuejs?

Fri, 2017-08-18 08:33

I am building an app with various pages, and when users goes to /orgs I have a template that I require

// routes.js ... import Orgs from './components/Orgs.vue'; ... { path: '/orgs', component: Orgs, meta: { requiresAuth: true } },

from here I have a simple template in Orgs.vue that looks like:

<template lang="html"> <div> {{orgs}} </div> </template> <script> export default { data(){ return { orgs: []; } }, created() { //use axios to fetch orgs this.orgs = response.data.orgs; } } </script>

The problem is that if I want to show list of organizations in other pages, I am bound to duplicate the same code for other pages as well, but I am trying to find a solution that would call return organizations so I can use that in multiple page?

What is the solution for this?

Categories: Software

Vue js computed result not accurate

Fri, 2017-08-18 04:33

I am trying to implement a vote button with vue js, when user click "Vote" will send axios request to server and store the data, then return json back. Same with unvote. I also check if the user is voted, the button should change to Unvote like facebook. So far the vote and unvote button is work correctly. But i found a problems which is the voted features is not working. If user voted, after refresh page it will change back to "Vote", but it should be Unvote. But if the button was clicked, in database will showing the vote was deleted. Mean it should be problems of computed. But i am struggle on it since i not really know vue js.

This is my vue components.

<template> <a href="#" v-if="isLiked" @click.prevent="unlike(comment)"> <span>UnVote</span> </a> <a href="#" v-else @click.prevent="like(comment)"> <span>Vote</span> </a>

<script> export default{ props: ['comment','liked'], data: function() { return { isLiked: '', } }, mounted() { axios.get('/comment/'+ this.comment.id +'/check', {}) .then((response) => { this.liked = response.data;//here will get json "true" or "false" }); this.isLiked = this.liked ? true : false; }, computed: { isLike() { return this.liked; }, }, methods: { like(comment) { axios.post('/comment/'+ comment.id +'/like') .then(response => this.isLiked = true) .catch(response => console.log(response.data)); }, unlike(comment) { axios.post('/comment/'+ comment.id +'/unlike') .then(response => this.isLiked = false) .catch(response => console.log(response.data)); }, } }

Categories: Software

Pages