Vuejs

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

React, Vue, Angular, etc., how to handle errors application wide?

Wed, 2017-09-06 16:43

Let's say I have application (no matter, what is on frontend).

Backend is just API.

There are many pages/forms. Every page/form requires some interaction with API.

Many errors can arise:

1. User is disconnected from network 2. User is not authenticated 3. API for some reason returns unexpected 500 code

It is bad to code handling of these errors in every form and every page (because we want to notify user that something went wrong) so it must be done in one place.

In Vue, for example, I can setup http interceptor, which swallows disconnected network error and 500 errors and shows info to user.

But some things can be bad, like web app checks some API every N seconds, so every N seconds it will show error (if network is disconnected).

Where can I read about web app architecture to remove code duplication like this?

Another thing, showing user that app is processing it's request

In React as I know I can make Redux store with something like

{ "loading": false }

Then every page/form dispatches action, which makes loading = true, upon finishing request sets loading=false so in some top level component I can show loading spinner.

Where to read about such cases?

Categories: Software

How to bind class to a div based on some conditions using Vuejs?

Wed, 2017-09-06 16:14

Actually, I need to activate the background color of a div based on the specific value given by the user. So I have done something like this. And don't know how to go further.

<div v-bind:class="[{active1: r_id === 1},{active2: r_id === 2},{active3: r_id === 3},{active4: r_id === 4},{active5: r_id === 5}]" > Mycard </div>

and my css part is:

active1{background:red;} active2{background:black;} active3{background:green;} active4{background:yellow;} active5{background:white;}

So how should I go on using Vuejs?

Categories: Software

Change laravel variable on every page using vue.js

Wed, 2017-09-06 16:00

I have navbar like this:

<nav class="navbar"> <p style="text-align: center; color: white;">{{ @$test}}</p> </nav>

And my page looks like:

<body class=""> <div id="app"> <div class=""> @include('navbar') @yield('content') <- here another components </div> </div> </body>

How to change that @$test laravel variable using vue.js?

Categories: Software

Axios not passing headers on requests

Wed, 2017-09-06 15:58

I'm building a VueJS application and I'm using JSON web tokens as my auth system. When I log the user, I store the token with localStorage and works fine. I check the headers and it's in the 'Authorization' param.

I pass with axios.defaults.headers.common['Authorization'] = localStorage.getItem('token')

I see the headers and it's okay. But when I execute a get request to an protected route in my API, return 'unauthorized'. But when I pass the header with token manually in the request, works fine.

Somebody know how to pass the header automatically when executing some request?

Categories: Software

Vue instance inside another Vue instance

Wed, 2017-09-06 15:20

I’m integrating Vue with a CMS called AEM thats works basically as component base system like Vue works too. But instead of having a webpack and imports of .vue files, every component on this CMS is a new Vue instance (new Vue({…})). So on my page I have a lot of Veu instances that communicate with each other using the same store (vuex).

This is actually working fine, but I have a scenario when I need a CMS component inside another. Since both this components are a unique vue instance and the “el” property from the parent includes the “el” from the child, the child component doesn’t work.

I know that this is not the expected use of this lib, but is there any way that I can tell or share the same “context” on both vue instances or even another approach for this scenario.

Thx, Alexandre.

Categories: Software

Many Vue projects, one node_modules

Wed, 2017-09-06 15:10

I'm making a new project. I have one server application (Node, Express, Mongo) and I'm having two client application (both are VueJS). I want this structure (I want only one node_modules directory):

- client --- admin (vue project) --- page (vue project) - server --- server.js - node_modules - package.json

I server app works fine, but I have problems with VueJS. I created new project with vue-cli, remove node_modules and package.json. All dependencies I have in main node_modules. I had error:

These relative modules were not found: * ./build/dev-client in multi ./build/dev-client ./src/main.js * ./src/main.js in multi ./build/dev-client ./src/main.js

But I resolved this by changing paths in webpack.base.conf from this:

entry: { app: './src/main.js' },

to this:

entry: { app: './client/admin/src/main.js' },

and in webpack.dev.conf from this:

Object.keys(baseWebpackConfig.entry).forEach(function (name) { baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name]) })

to this:

Object.keys(baseWebpackConfig.entry).forEach(function (name) { baseWebpackConfig.entry[name] = ['./client/admin/build/dev-client'].concat(baseWebpackConfig.entry[name]) })

Project is now running but now I have this error:

ERROR Failed to compile with 1 errors error

and I don't know how to fix it. Please help

Categories: Software

Fuzzy search required for searching nested objects using feathers-vuex?

Wed, 2017-09-06 14:57

I am using feathers-vuex in a project and am not very familiar with the rest of the feathers package. I am using this is because with the scaffolding cli, it was very easy to get started and it just works. Has been a really good experience so far. However, this also means that I do not entirely get what's going on under the hood. I am trying to use the find function to retrieve all records where a nested array contains a certain string from a mongodb. The questions are as follows:

  • So far, the only option that I can think of is a fuzzy search. Is that the way to do it? Or are there other possibilities?
  • Is my assumption that fuzzy search won't work because of the absence of hooks correct? Or have I misread the docs?
  • Any other general way of accomplishing this?
  • Does this mean that fuzzy search will not work using feathers-vuex or are there ways to accomplish this?
Categories: Software

Vuex, what is the best practice for asynchronous calls

Wed, 2017-09-06 14:37

I'm discovering Vuex at this time and I have came across the concepts of Actions which should be used instead of Mutations if it is about asynchronous code.

But, I have this question, would it better to make all the asynchronous logic in the action itself or do the asynchronous login in the component and just commit Mutations in the then call?

In other words, is this code considered best practice in the Vuex realm or I just need to move the API call to a state Action?

enter image description here

Thanks.

Categories: Software

Communicating variable in vue.js between components

Wed, 2017-09-06 14:34

I am building app using laravel and vue. I have navbar, currently it looks like:

<template> <nav class="navbar"> <p>{{msg}}</p> </nav> </template>

And I use it like here:

<body class=""> <div id="app"> <div class=""> <navbar></navbar> @yield('content') </div> </div> </body>

In yield I am loading another components, so I have navbar and another component together. Now I want to override that {{msg}} variable from navbar in another components. In every component that variable will be diferent.

I do not know how to override it in components and from {{msg}} do some text. Can you help me? (That code above is all what I have)

Categories: Software

Vue table in one loop html

Wed, 2017-09-06 14:17

I have an array, with sizes of shoes and quantity each of them in shop, structure like this:

array = { 36=>1, 37=>0, 38=>5, 39=>2 }

In my table key in this table ( here 36, 37 ... ) are TH, and value is TD. I can't do this in one loop. I tried like this:

<table class="table"> <tr> <th v-for="(quantity, key) in productSizes" :key='key'>{{key}}</th> </tr> <tr> <td>Here should be quantity for each size<td> </tr> </table>

Is there a possibility to do this at once?

Here is structure how it should look like ( there is an input, because someone can change quantity ).

enter image description here

Categories: Software

evnetbus listen to $on can't be executed vue js

Wed, 2017-09-06 13:16

i try to use eventbus to send and get data from different components .icreate a global variable eventbus and in each component when i ant to send data to a specific component i write this

eventBus.$emit('dis',this.displaytype);

and in this specific component i write:

eventBus.$on('dis', function (snippet) { this.displaydis = snippet console.log('display: ', this.displaydis) }.bind(this));

i write it in created hook but the problem this one is not executed can any one help me ?

Categories: Software

How to get the user after login in VueJS

Wed, 2017-09-06 12:58

I use both axios and vue-resource in a project. Because I started with vue-resource and on post didn't work, I used axios for post in the login.

I'm new to vuejs and I don't know how to get the user after login. Loggin is a component and in the header, that is another component I want to put the name of that user that is logged in.

How to get that user after login in the Header component?

Login method in Login component

login() { axios.post('http://192.168.100.93/login', this.user) .then ( (response) => { AuthPlugin.getToken(response.data); alertify.success("Success! You are now logged in."); this.$http.get("/user", this.user) .then((response) => { this.$router.push('/home'); }) }) .catch(function (error) { alertify.error('Invalid User'); }); }

The Header component where I want to put the user logged in

<div class="dropdown dropdown-navbar-btn-wrapper"> <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="ti-user"></i> The Login User <i class="ti-angle-down"></i> </button> <div class="dropdown-menu" aria-labelledby="dropdownMenuButton"> <a class="dropdown-item" href="#">My account</a> <a class="dropdown-item logout-btn" href="#" @click.prevent="logout"><i class="ti-power-off"></i> Log out</a> </div> </div>
Categories: Software

Vue with TypeScript without components

Wed, 2017-09-06 12:55

I encountered with problem when develop application. I use Vue + Vuetify with typescript, but I don't want to create SPA application or use webpack for work with .vue components, I need to create several page, where I create each times new Vue instance. But when I create for example

import * as Vue from 'Vue'; import axios from 'axios'; <any>window.vue = new Vue({ el: "#app", data: { drawer: true, mini: false, totalItems: 0, items: [], headers: [, { text: 'Dessert (100g serving)', align: 'left', sortable: false, value: 'name' }, { text: 'Calories', value: 'calories' }, { text: 'Fat (g)', value: 'fat' }, ], }, methods: { getData() { axios.get("http://exmaple1234.com/api/list") .then((response) => { this.$data["totalItems"] = 1; this.$data["items"] = [ { value: false, name: 'Frozen Yogurt', calories: 159, fat: 6.0, } ]; }) } }, mounted() { this.$options.methods["getData"].call("getData"); }, });

My tsconfig.json

{ "compilerOptions": { "alwaysStrict": true, "noImplicitAny": false, "noEmitOnError": true, "removeComments": true, "sourceMap": false, "target": "es5", "moduleResolution": "node", "allowSyntheticDefaultImports": true, "lib": [ "es2017", "dom", "dom.iterable" ] }, "exclude": [ "node_modules" ], "compileOnSave": true }

With typescript I can't use this.totalItems, this.items and I can't call this.getData() in mounted(), but when I debug in browser my code, I see that object "this" has all these properties and methods.

I use property $data["property"] name and $options.methods["methodName"] in order to work with it, but I understand that isn't correct approach. I read in Vue documentation about ComponentOptions which help to create interface or vue-class-component, but all these tools use components, which I want to avoid.

Can I use vue + typescript in this case? I'd appreciate tips to my question

Categories: Software

Uncaught Error: [vuex] actions should be function but "actions.__esModule" is true

Wed, 2017-09-06 12:32

i am new in vue jsand i want to use vue boilerplate from https://github.com/marcosmoura/vue-boilerplate.

When i try to run this using command npm run dev it will generate one link ex:http://localhost:8080

When i run this link into browser, this error occurs and i am not able to do any thing

error image in chrome browser

Please help me with this if any one know about it.

Categories: Software

Vue.js unit tests error Module not found

Wed, 2017-09-06 12:17

I don't get it .. why my import is wrong ?

here is my project structure

project src components ... ... vuex actions.js getters.js mutation_types.js mutations.js store.js App.vue main.js test unit specs vuex mutations.spec.js

Here is my test spec file w the imports

// mutations.spec.js import mutations from 'src/vuex/mutations' import { ADD_SHOPPING_LIST } from 'src/vuex/mutation_types' ...

here is my package.json script

"scripts": { ... "unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run", ... },

when I execute: npm run unit

I get MOdule not found

terminal cd project npm run unit ERROR in ./test/unit/specs/vuex/mutations.spec.js Module not found: Error: Can't resolve 'src/vuex/mutations' in '/Users/myself/Developments/project/test/unit/specs/vuex'

what's wrong ? isn't relative to the project directory ?

thanks for feedback

Categories: Software

How to return some message to another component in vue.js?

Wed, 2017-09-06 12:15

I have navbar like this:

<template> <nav class="navbar"> <p style="color:white; text-align:center;">{{msg}}</p> </nav> </template>

And I import it in all my pages (components). How to return {{msg}} on all pages? I mean something like:

msg: 'index'
Categories: Software

Year - Month Calendar plugin

Wed, 2017-09-06 12:02

Looking for recommendations on a calendar plugin for a Laravel project.

I would like the plugin to display months then when a user clicks the year it takes them back a step to a year selection, then clicking on a year takes you into the months for that year. Something like below, appearance is not important as I will customise this to match the application. Clicking on a month will display information relating to that month but again that will be done by myself, just need a good solution for the year/month selection.

Month selection

Year selection

Categories: Software

how do i add 'enter-active-class' to 'transition' in custom directives

Wed, 2017-09-06 11:50

How do I add enter-active-class to transition in custom directives? now enter image description here

But this cannot satisfied with my request,I need define enter-active-class with custom directives,so I need your help,thanks!

Categories: Software

Back - jQuery mobile and Vue

Wed, 2017-09-06 11:47

I have embedded a Vue component for a Activity Feed on a page inside a jQuery Mobile website.

The script loads 10 activities per request and adds them to the end of the list - triggered by scroll

If i click on a activity (for example "user added photo") it takes me to that user's profile. If I swipe back (on my phone) or hit back in browser it takes me back to the page and the feed reinitialises.

Any method for going back to the same state and to not being forced to load all the activities that were previously loaded and scroll to the specific one ?

Categories: Software

Setting v-bind:class with method not working

Wed, 2017-09-06 11:45

I am trying to set the style of a div with

<div v-for="q in questions" v-bind:class="{seen:isseen(q),unseen:isunseen(q)}">

The problem is that these functions get computed for each q, but I also need them to get recomputed when a different variable updates.

methods:{ isseen: function(id_1){ if(ans[id_1]==2) return true; else return false }, isunseen:function(id_1){ if(ans[id_1]!=2) return true; else return false; } }

Here, I need the

v-bind:class="{seen:isseen(q),unseen:isunseen(q)}"

computed even when ans[id_1] changes.

I have looked at the computed and watch approach, but cannot figure out what will work here.

Categories: Software

Pages