Feed aggregator

Why isn't router.currentRoute.path reactive?

Vuejs - Wed, 2017-07-26 15:26

I have an app which is contained in

<div id="app" v-bind:style='{backgroundColor: backgroundColor}'> ... the app ... </div>

The routing is done following the example in the documentation (this is a webpack project):

import Vue from 'vue/dist/vue.js' import VueRouter from 'vue-router' import ComponentOne from './component1.vue' import ComponentTwo from './component2.vue' Vue.use(VueRouter) const routes = [{ path: '/foo', component: ComponentOne }, { path: '/bar', component: ComponentTwo } ] const router = new VueRouter({ routes // short for `routes: routes` }) const app = new Vue({ router, data: { day: "Monday" }, computed: { backgroundColor: function () { console.log(JSON.stringify(router.currentRoute)) if (router.currentRoute.path == "/foo") { return "green" } else { return "blue" } } } }).$mount('#app')

I wanted to background to be dependent on the current route (router.currentRoute.path). The solution above does not work, however, as router.currentRoute.path is not detected by the Vue instance as having changed (is not reactive).

What is the correct way to access these dynamic router data from within the Vue instance?

Categories: Software

Vue.js sync input field

Vuejs - Wed, 2017-07-26 14:38

In my vue application I've got lots and lots of input fields (example):

<div class="field"> <label for="name" class="label">Naam</label> <div class="control"> <input id="name" name="name" type="text" v-model="relation.name" class="input" :class="{ 'is-danger': errorsHas('name') }" autofocus> <p class="help is-danger" v-if="errorsHas('name')">{{ error('name') }}</p> </div> </div>

So I would like to wrap this in a input component. But since vue 1 the .sync method is gone so how would I do this? Firing events is not realy a solution I guess. Just wondering how to solve this?

I would like to have something like this:

<custom-input v-model=relation.name></custom-input>

And everything else (class name, autofocus etc...) must be handled in that component.

Is this possible?

Categories: Software

Filtering array based on multiple key where key are dynamic in nature

Vuejs - Wed, 2017-07-26 13:12

I am trying to filter array on key where keys are dynamic in nature. The checkbox will add another key to array. There are two type of ride [A,B], when user select A, only ride of type A get displayed and when user select B, only ride of type B get rendered, if both are selected, all rides are get rendered.

<!DOCTYPE html> <html> <head> <title></title> </head> <body> <div id="app"> <div id="filters"> <input type="checkbox" name="filter" value="A" v-model="rideFilter"> Ride Type A <input type="checkbox" name="filter" value="B" v-model="fileFilter"> Ride Type B </div> <div id="rides"> <ul> <li v-for="ride in rideAfterFilter"> {{ride.src}} - {{ride.dest}} </li> </ul> </div> </div> <script> new Vue({ el:'#app', data:{ rides: [ {src:'A',dest:'G',type:'A'}, {src:'B',dest:'H',type:'B'}, {src:'C',dest:'A',type:'A'}, {src:'D',dest:'B',type:'B'}, {src:'E',dest:'E',type:'C'}, {src:'F',dest:'D',type:'D'}, ], rideFilter:[] }, computed:{ rideAfterFilter:function (){ this.rides.filter(function(ride){ //here }); } } }); </script> </body> </html>
Categories: Software

Vue.js: insert value into textfield but don't bind it

Vuejs - Wed, 2017-07-26 12:47

I am facing an interesting problem: I have a form where I want to insert old values (if the form was submitted before) like this:

<input type="text" :value="oldName" id="name" />

Now the problem is that I can't overwrite the oldName variable like this, so yes, I have the old value in there, but I can't change it anymore. Can you think of a solution? I basically want the value to be in the textfield, but I want the user to be able to change it. Thank you!

Categories: Software

VueJS and FineUploader component structure

Vuejs - Wed, 2017-07-26 12:45

Hi everyone i'm having a bit of a problem organizing my components in a way that makes sense, i'm using VueJS 2 and Fine Uploader 5.14.5 and these components i'm creating are essentially wrappers for FineUploader.

I have 3 custom components, Uploader, Dropzone and Preview.

Uploader contains the FineUploader instance and it's constructor, an implementation of FineUploaderBasic from the following import

import FineUploader from 'fine-uploader/lib/all'

It sets up FineUploader in the mounted() hook using the options available on FineUploader's documentation

Then Dropzone is an implementation of FineUploader's DragAndDrop functionality and finally Preview is just a div where i show img previews of each file through Canvas...

So the problem is that my implementation is supposed to work through steps, so first you get the Dropzone and the upload button (from the Dropzone and Uploader components) and then after that you would get the Preview component, supposedly by switching the components using v-if and v-else the problem with this is that since my implementation of FineUploader is defined in the Uploader component i cannot get rid of that component, if the Component is destroyed then FineUploader's instance stops working and i need access to it's methods in order to be able to cancel queued files, pause and resume uploads... etc.

I can't really keep it as the parent component either as that would mean that FineUploader's hidden input remains on the DOM and will be placed right in front of my Preview component's action buttons...

https://codeshare.io/2p6mE4 <- this is my Uploader.vue component.

Let me know if you guys need anything else, all i want to know if someone has ever succeeded in using this library with Vue and if so what approach did they take as i'm completely confused as to how to structure something like this. Could i use an external class for FineUploader and import it into my Vue template? But then how could i bind anything to it's callbacks (as i'm doing in Uploader.vue)?

Again the idea is, Uploader appears with the Dropzone, user selected X files, Uploader (clickable interface) disappears along with Dropzone, and leaves a totally different component visible instead, but this component still needs access to FineUploader's methods which are left in the Uploader.vue component...

Categories: Software

Can I use <style> tag inside of inline <svg> tag on my html5 page while using Vue?

Vuejs - Wed, 2017-07-26 12:22

I'm rendering svgs inline on my html5 page. I'd like to have a nested style block inside of each of the svgs for convenience, rather than using presentation attributes or moving everything to the main stylesheet.

But I am also using vue at the current time and there seems to be a conflict.

I get an error in the browser console when trying this (and the svg initially appears, then goes black), which seems to be from the Vue loader.

In all honesty I'm not all that clear on exactly what is happening. If anyone can explain that would be greatly appreciated! Thanks.

Categories: Software

VueJS not picking up data passed

Vuejs - Wed, 2017-07-26 12:15

I'm very new to VueJS so expect this is very much a me problem but hoping for some guidance.

I have a Vue instance the person object in data contains some predefined data but I'm adding to the object when it's mounted. For whatever reason I cant understand it's not updating the DOM with the data inserted when mounted. I've checked the vue app and can confirm the person being added on the mounted method is being added correctly.

new Vue({ el: '#app', data: { people: { Dave: {'Age': 30, 'Plays': 5} } }, mounted: function() { this.people['Rob'] = {'Age': 22, 'Plays': 24}; } }); <script src="https://unpkg.com/vue"></script> <div id="app"> <ul> <li v-for="(person, key) in people"> {{ key }}: {{ person.Age }} </li> </ul> </div>

I have a feeling this is either a Vue gotcha or I'm doing something really stupid.

Categories: Software

Response text contain "Error occured while trying to proxy"

Vuejs - Wed, 2017-07-26 11:46

I'm developing with webpack-dev-server, and in order to proxy backend APIs, I use http-proxy-middleware. But there is a strange thing that ALL the response contains a string "Error occured while trying to proxy" in the end as shown in the picture. However, the http status code is 200. Btw, I'm using OS X and there isn't any error when the code running on Windows.

Response text contain errors

proxyTable: { '/rest/': { target: 'http://127.0.0.1:8080', changeOrigin: true }, '/upload/': { target: 'http://127.0.0.1:8080/upload/', changeOrigin: true, } },
Categories: Software

force update computed properties in vuejs

Vuejs - Wed, 2017-07-26 11:42

I've a Vuejs component that takes a "datasetId" as prop. I want a 2 way binding in my case, so I use the .sync modifier

<mycomponent :datasetid.sync=1 />

In this component I've a select component and I want it to show the good dataset, so I bind it to my props.

<select-component v-model="datasetid" />

Since we cannot mutate a prop (datasetid is a prop and bind it to v-model will potentially mutate datasetid). I decided to wrap this value on a computed property.

<select-component v-model="computedDatasetid"> computed: { computedDatasetid: { get() { return this.datasetid; // return the prop }, set(v) { // don't mutate the prop directly but update the parent component. this.$emit('update:datasetid', v); } } }

With this code, the "getter" is only called 1 time (when the component is create the first time). It's logical because the documentation says that the computed property is updated when an inner data is updated. And I don't update anything in the "setter".

How to force model to change when I call the setter ? (ex: this.computedDadasetid = 5)

Computed properties are the good way to do what I want ? Would be better to use raw 'data' or watcher or anything else ?

Thanks

Categories: Software

How to show specific field on click button of a loop vue js

Vuejs - Wed, 2017-07-26 11:28

I have a posts loop where each post has a comment box.Initially all comment box are hidden and when someone clicks on 'comments' button it should show the comment field for this user. I am having trouble to show specific comment box associated with the specific post. my code is as follows -

<div class="post-section" v-for="(post,index) in posts"> <div class="media-content">{{post.body}}</div> <button @click="getComments(post, index)" class="btn btn-link"><i class="fa fa-comments-o"></i>{{ post.total_comments }} Comments</button> <comment-input :postId="post.id"></comment-input> </div> <script> export default { data() { return { posts: [], } }, created() { Post.all(posts => this.posts = posts); }, methods: { getComments(post, index){ axios.post('getcomments', {id: post.id}) .then(response => { this.$set(this.posts, index, Object.assign({}, post, { comments: response.data })); }); }, } } </script>

When getComments(post, index) method executes i want to make the next comment-input only. Any help??

thanks in advance.

Categories: Software

Failed to download repo vuejs-templates/webpack: EACCES: permission denied, mkdir '/.vue-templates'

Vuejs - Wed, 2017-07-26 11:23

I'm trying to learn Docker. I installed Docker ToolBox on Windows 10 (I have Home Edition, so I can't use regular Docker installation because that Windows version doesn't have Hyper-v).

Next, I tried this container from Docker Hub:

docker pull ebiven/vue-cli

I added new alias as was written on container page (I changed name to vuejs because I have vue installed locally):

alias vuejs='docker run -it --rm -v "$PWD":"$PWD" -w "$PWD" -u "$(id -u)" ebiven/vue-cli vue'

And then in the console I wrote:

vuejs init webpack .

I got an error message:

vue-cli · Failed to download repo vuejs-templates/webpack: EACCES: permission denied, mkdir '/.vue-templates'

How to fix this?

Categories: Software

vueify+node: compile separate .vue files

Vuejs - Wed, 2017-07-26 11:10

It is possible to setup vueify this way, so it compiles single .vue files to corresponding .js files?

For example.

Input: my-component.vue

<template> <div>{{ message }}</div> </template> <script> module.exports = { data() { return { message: 'Hello World', }; }, }; </script>

Output: my-component.js

Vue.component('my-component', { template: '<div>{{ message }}</div>', data() { return { message: 'Hello World', }; }, });
Categories: Software

Unable to get datepicker to work with VueJS2

Vuejs - Wed, 2017-07-26 10:43

I am trying to use jquery-ui with VueJS 2 so i can use functions such as datepicker, sortable and slider. However, i am faced with an error:

TypeError: $(...).datepicker is not a function

Here's what i've done to try get this working, in my app.js file i have loaded JQuery and JQuery UI before I require Vue:

window.$ = window.jQuery = require('jquery'); window.$ = $.extend(require('jquery-ui'));

I have created my component slider and here is the content:

Vue.component('datepicker', require('./components/Datepicker.vue'));

Here is the component itself

<template> <div class="datepicker"></div> </div> </template> <script> export default { data() { return { }; }, mounted() { $(this.el).datepicker(); } } </script>

How can I get this to work?

Categories: Software

DragEnd event not fired for files

Vuejs - Wed, 2017-07-26 10:40

My template

<div class="upload-component" @dragend="log('end')" @dragenter="dragEntered" @drop.prevent @dragover.prevent> <div class="zone" @drop="dropped"> </div> <p draggable="true">drag me</p> </div>

My js

export default { name: 'UploadComponent', methods: { log(str){ console.log(str) }, dragEntered(e){ // change some styles }, dropped(e){ console.log(e.dataTransfer.files) } } }

The problem: If I drag the P element and drop it or hit escape it will log "end". If I come with a file from my desktop and put it back on desktop or hit escape or drop it, the dragend event wont fire and it wont log anything

Categories: Software

Webpack UglifyJS running into unexpected token

Vuejs - Wed, 2017-07-26 10:31

I am trying to set up a development boilerplate which can also easily push a Vue.js project to NPM.

I am running into a problem with my webpack.prod.js file, the error is:

ERROR in build.js from UglifyJs Unexpected token: operator (>)

The code to uglify is:

// minify with dead-code elimination new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } })

This is my project and the exact file where it seems to go wrong:

https://github.com/stephan-v/vue-inline-svg/blob/master/webpack/webpack.prod.js

The project uses Babel to transpile to ES6 and Webpack to compile to UMD format when I run npm run production. This command uses the webpack.prod.js configuration.

I am probably not seeing something that could be fixed easily but I have no clue what is going wrong here.

Categories: Software

conditional binding in vue js

Vuejs - Wed, 2017-07-26 09:30

stores data using ajax

data: datas: new dataBinder() methods: dataBind: (event)-> parent = this current = event.currentTarget $.ajax url: $(current).attr("data-action") type: $(current).attr("data-method") dataType: "json" contentType: "application/json" headers: 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') success: (msg)-> parent.datas.store(msg) error: (msg)-> console.log(msg.responseJSON)

class of databinder

class dataBinder constructor: ()-> @datas = {} get: (field)-> if (@datas[field]) @datas[field] else @datas = null store: (datas)-> @datas = datas clear: ()-> @datas = {}

hello im having a trouble in vue js i want to bind my object in my input if the object is not null using

v-bind:value="datas.get('code')"

this code is working however...everytime there's an event happening, my inputs value sets to null (or clearing my inputs)

is there a way to make vue bind properties only if my object is not null?

Categories: Software

Vue i18n regex missing transaltions

Vuejs - Wed, 2017-07-26 09:17

have a small question, I have used i18n and it is great but is there a way to see missing translations before starting the app and walking trough components

Examples here https://github.com/kazupon/vue-i18n/pull/177 Here it is ok , but can I see all missing translation without having to traverse the app manually. I mean when I just start app I want to see all missing translation not only when I run specific component. I have script to search trough translations but I cannot find regex for {{ $i18n.t("save me") }} to search it.

Thanx in advance

Categories: Software

Single page mobile website using VUE JS

Vuejs - Wed, 2017-07-26 07:31

I’m planning to develop the mobile website using VUE JS. The site is like a Social web site. Is Developing as Single Page Application is a good idea?

Categories: Software

Introducing js framework into legacy asp.net mvc

Vuejs - Wed, 2017-07-26 05:09

I recently stepped into a large project written on ASP.NET MVC4, generally server generated pages fit well, but some places require rich client side logic. Currently it is done through direct DOM (jQuery) manipulation, that looks VERY messy and results lots of bugs due to lack testing capabilities. As far as I can see, the only things that require are: validation, hide/show controls based on user input and results of ajax calls. Also, I see quite a resistance from a team, people just never have seen well working client side apps and don't understand benefits.

I was working with Angular, but not sure we need a whole framework.

Could community recommend technique/framework that solve my problem in fast and elegant way?

Thanks

Categories: Software

Laravel Setting form value="old('input')" with Vue JS 2

Vuejs - Wed, 2017-07-26 04:09

I have a simple form with a calculator for the amount being charger including shipping and tax. If the form is submitted and comes back with errors, how do I display the old input into the value field of the form with Vue JS? The instance values are coming back correct however they are not displayed in the form.

<div class="form-group"> <label for="totalAmount">Total Amount</label> <div class="input-group"> <div class="input-group-addon">$</div> <input type="totalAmount" class="form-control" id="totalAmount" name="totalAmount" value="{{old('totalAmount')}}" v-model="totalAmount" v-on:change="getTotal" placeholder="0.00" required> </div> </div> <div class="form-group"> <label for="shipAmount">Shipping Amount</label> <div class="input-group"> <div class="input-group-addon">$</div> <input type="shipAmount" class="form-control" id="shipAmount" name="shipAmount" value="{{ old('shipAmount') }}" v-model="shipAmount" v-on:change="getTotal" placeholder="0.00"> </div> </div> <div class="form-group"> <label for="taxRate">Tax Rate</label> <div class="input-group"> <div class="input-group-addon">%</div> <input type="taxRate" class="form-control" id="taxRate" name="taxRate" value="{{ old('taxRate') }}" v-model="taxRate" v-on:change="getTotal" placeholder="0.00"> </div> </div> <div class="widget-body text-center"> <h4 class="m-b-md">Total Amount $ @{{ combinedAmount }}</h4> </div>

JS File:

new Vue({ el: '#app-content', data: { billSameAsShip: '', isHidden: 'true', totalAmount: '', shipAmount: '', taxRate: '', combinedAmount: '' }, computed: { totalAmount2: function () { return this.totalAmount = {{ old('totalAmount') ?? 0 }}; }, shipAmount2: function () { return this.shipAmount = {{ ( old('shipAmount') ?? 0 ) }}; }, taxRate2: function () { return this.taxRate = {{ ( old('taxRate') ?? 0 ) }}; } }, beforeUpdate() { this.getTotal() this.totalAmount = this.totalAmount2; }, methods: { onChange: function() { if(this.billSameAsShip == 'false'){ this.isHidden = !this.isHidden; } else { this.isHidden = Boolean('true'); } }, checkNaN: function() { if(isNaN(parseFloat(this.totalAmount))){ this.totalAmount = 0 } if(isNaN(parseFloat(this.shipAmount))){ this.shipAmount = 0 } if(isNaN(parseFloat(this.taxRate))){ this.taxRate = 0 } }, getTotal: function() { this.checkNaN(); this.combinedAmount = (parseFloat(this.totalAmount) + parseFloat(this.shipAmount)) * (1 + (parseFloat(this.taxRate /100))); } } }) </script>
Categories: Software

Pages