Software
Webpack not creating css and js files
I am using laravel and vue.js. When I do npm run dev my sass files are not compiling and they are not exist in /public. What can be the reason? I can not show code because of nda..
How to filter two data set from one selection in VueJs
I'm building a small application in VueJs 2.0 where I'm having two select options with Select2 plugin as such:
<div class="form-group"><label class="col-sm-2 control-label">Categories:</label> <div class="col-sm-4"> <v-select multiple :options="selectTable" placeholder="Categories" v-model="categories"></v-select> </div> </div> <div class="hr-line-dashed"></div> <div class="form-group"><label class="col-sm-2 control-label">Sub-Categories:</label> <div class="col-sm-4"> <v-select multiple :options="selectSubtractedTable" placeholder="Subtracted Categories" v-model="subtractedCategories"></v-select> </div> </div>Now I've a computed property to list these elements inside, like:
computed: { selectTable() { if(this.model) { return this.model.map(a => ({ value: a.id, label: a.name })); } }, selectSubtractedTable() { if(this.subModel) { return this.subModel.map(a => ({ value: a.id, label: a.name })); } } },Now I'm having a watch function which checks the first list and remove the elements from the second list as:
watch: { categories(newValue) { this.subModel = this.model for(var i = 0; i<newValue.length; i++ ) { const element = this.subModel.find(a => a.id == newValue[i].value) console.log(element) if(element>0) this.subModel.splice(index, 1) } } }I've data set in following format:
this.model = this.subModel = [ { "id":1,"name":"Jeans","created_at":null,"updated_at":null }, { "id":2,"name":"Shirts","created_at":null,"updated_at":null }, { "id":3,"name":"Top","created_at":null,"updated_at":null }, { "id":4,"name":"Women","created_at":null,"updated_at":null }, { "id":5,"name":"Men","created_at":null,"updated_at":null }, { "id":6,"name":"Jwellary","created_at":null,"updated_at":null } ]and const element = this.subModel.find(a => a.id == newValue[i].value) this is always showing value 0 in the watch property
Help me out.
Why can't this component access its props data?
When the following component was inline, it was able to access rowData but as a referenced template it can't:
HTML: <div id="app"> <div> <row-component v-for="(row, index) in rows" :row-data="row" v-on:delete-row="deleteThisRow(index)"></row-component> </div> </div> <script id="theRow" type="x-template"> row component: {{rowData}} <button @click="$emit('delete-row')">Delete3</button> </script> JavaScript: Vue.component('row-component', { props: ["rowData"], template: '#theRow' }) new Vue({ el: '#app', data: { rows: ["line1", "line2", "line3", "line4", "line5"] }, methods: { deleteThisRow: function(index) { this.rows.splice(index, 1); } } })How can I get the component to recognize rowData?
How to use calendar in javascript
I'm using javascript (vue js) and I'm trying to add a calendar that I can choose from it date and time, I'm using this one:
<span class="glyphicon glyphicon-calendar"></span>but I can only choose date from the calendar,
I want something like the Enabled/Disabled Dates Bootstrap 3 Datepicker v4 Docs, which is this one:
<div class="container"> <div class="row"> <div class='col-sm-6'> <div class="form-group"> <div class='input-group date' id='datetimepicker5'> <input type='text' class="form-control" /> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div> </div> </div> <script type="text/javascript"> $(function () { $('#datetimepicker5').datetimepicker({ defaultDate: "11/1/2013", disabledDates: [ moment("12/25/2013"), new Date(2013, 11 - 1, 21), "11/22/2013 00:53" ] }); }); </script> </div>But I can't use it since it uses jQuery and I don't want something with jQuery. Is it possible?
Thank you.
How to set a component non-reactive data in Vue 2?
I have a categories array, which is loaded once (in created hook) and then it is static all the time. I render this array values in a component template.
<template> <ul> <li v-for="item in myArray">{{ item }}</li> </ul> </template>My data property looks (it does not include myArray - I dont want reactive binding):
data() { return { someReactiveData: [1, 2, 3] }; }My create hook:
created() { // ... this.myArray = ["value 1", "value 2"]; // ... }Problem is, that Vue throw error - I cant use myArray in a template, because this variable is not created when the DOM is created - mounted.
So how to do this? Or where can be stored component constants?
Difference between the created and mounted events
Vue.js documentation describes the created and mounted events as follows:
createdCalled synchronously after the instance is created. At this stage, the instance has finished processing the options which means the following have been set up: data observation, computed properties, methods, watch/event callbacks. However, the mounting phase has not been started, and the $el property will not be available yet.
mountedCalled after the instance has just been mounted where el is replaced by the newly created vm.$el. If the root instance is mounted to an in-document element, vm.$el will also be in-document when mounted is called.
This hook is not called during server-side rendering.
I understand the theory, but I have 2 questions regarding practice:
- Is there any case where created would be used over mounted?
- What can I use the created event for, in real-life (real-code) situation?
IE11 + Vue-Cli + Webpack + Babel - SCRIPT1003 Error
I have the SCRIPT1003: Expected ‘:’ error in IE11. I have installed vuejs over vue-cli tool. It worked until I have added vuex library to the package.json. My package.json dependencies are following:
"dependencies": { "axios": "^0.16.2", "babel-polyfill": "^6.26.0", "lodash": "^4.17.4", "rxjs": "^5.4.2", "vue": "^2.3.3", "vue-i18n": "^7.1.0", "vue-router": "^2.6.0", "vue-rx": "^3.3.0", "vuetify": "^0.14.11", "vuex": "^2.3.1", "vuex-persistedstate": "^2.0.0" }As suggested on many other help forums I have tried using babel polyfill. My .babelrc file is like so:
{ "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"] } } }I have included the polyfill in the entry point in build/webpack.base.conf.js:
entry: { app: ['babel-polyfill', './src/main.js'] },Unfortunately I am still getting the same error after all of this. Could you please help me how to make it work in IE11?
Thank you in advance.
Best regards,
Goran
how to use vue to complete this function? [on hold]
I want use Vue to complete this function, when I click the buttons, input will show the result,and can mutiple,clicked button will highlighted
JSON.parse() can not turn string to object, and print out SyntaxError: Unexpected token o in JSON
In my vue.js project, i use axios library to get a JSON data, and successful(1.ok could print json data: {...}). but when i use JSON.parse() method to turn data to a object, console print out a error(2.error), could you tell why?
axios.get('/data/cart.json') .then(response=> { // 1.ok console.log(response.data); // 2.error let data = JSON.parse(response.data); console.log(data); }) .catch(function(error){ console.log(error); });This is console print out:
SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) at axios.get.then.response (cart.js:19) at <anonymous>laravel vue.js - using items from node modules folder
I just installed easy-autocomplete library using npm install easy-autocomplete. Now I would use this in my .vue components. Since I am green in laravel and vue.js I don't know how to import it to use. Could I ask for your help?
File to import not found
So I'm trying to import my SCSS file with all vars and mixins definitions to all components I have. File name for defs is _defs.scss:
@import "styles/defs";I also used alias in webpack to define where to look for styles folder:
resolve: { alias: { 'vue$': 'vue/dist/vue.esm.js', assets: path.resolve(__dirname, "src/assets/"), styles: path.resolve(__dirname, "src/styles/") } }But for some reason webpack still throws error telling he can't find the import file.
Where am I wrong?
Computed properties on function instances
In knockout.js I was able to do the following:
function Person() { this.firstName = ko.observable('Foo') this.lastName = ko.observable('Bar') this.fullName = ko.pureComputed(() => { return this.firstName() + this.lastName() }) } var p = new Person() console.log(p.fullName()) // FooBarIs there any way to add reactive computed properties on objects which are not a component data in Vue?
Checkbox selection through number of counts in array in VueJs/Javascript
I'm building a small application in Vuejs where I'm having a list of year presented in a table something like this:
<tr v-for="model in yearsTable"> <td class="text-left"> <div class="i-checks"> <label> <input type="checkbox" :value="model.id" :checked="model.active" @click="yearCheck(model)"> <i></i>{{ model.name }}</label> </div> </td> </tr>and a computed property to have the data something like this:
computed: { yearsTable: function () { if(this.yearModel) { return this.yearModel.map(d=> ({name: d.name, id: d.id, active: d.is_active ? true : false})) } } }I've a function in which I want to check the is_active count and I want to have only 3 to be checked at a time. I mean if more than 3 is checked then it should alert so I have a method function something like this:
yearCheck(model) { var count = 0; var ele = this.yearModel.findIndex(t => t === model) if(ele.is_active = 0) { ele.is_active = 1 } else { ele.is_active = 0 } for(var j=0; j<this.yearModel.length; j++) { if(this.yearModel[j].is_active === 1) { count++; } } if(count>3) { ele.is_active = 0 alert('You already have 3 selected years') } else { //Do other function } }I'm getting an error of:
Uncaught TypeError: Cannot create property 'is_active' on number '-1'
Cannot clear radio button Vue js
I have this three components - main, question, and questionOption. What I want is to clear the checked radio button on the questionOption from the main component startNewGame method. When I'am only using Main component resetting the radio button is just simple. I just assign the model to an empty array. Can someone help me on this. By the way, Im using onsen ui for the customize tags ans removed some unnecessary methods and data. 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" @collectedAnswer="pushToAnswers" > </question> <section style="margin:16px"> <v-ons-button modifier="cta" @click="submit" v-show="!isDone" >Submit </v-ons-button> </section> </div> <script> export default { data() { return { dialogVisible: false, resultDialogVisible: false, questionnaires: [], chosenAnswers: [], correct: [], total: 0, isDone: false } }, methods: { pushToAnswers(answer) { this.chosenAnswers.push(answer); }, submit() { //will compare chosenAnswers to correct answer } }, startNewGame() { //will reset answered questions this.getQuestionaire(); this.chosenAnswers = []; this.resultDialogVisible = false; this.total = 0; this.isDone = false; }, components: { Question } } </script>Question.vue
<question-option v-for="(choice, key, $index) in choices" :choice="choice" :indexParent="indexParent" :index="$index" :letter="key" :name="'question-' + indexParent" v-model="selectedValue" @change="changeValue" > </question-option> <script> import QuestionOption from './QuestionOption.vue'; export default { data() { return { selectedValue: '' } }, props: ['question', 'indexParent', 'choices', 'chosenAnswer'], methods: { changeValue(newValue) { this.selectedValue = newValue; this.$emit('collectedAnswer', this.selectedValue); } }, components: { QuestionOption } }Question.vue
<v-ons-list-item :modifier="longdivider" :key="letter" tappable > <label class="left"> <v-ons-radio :name="name" :input-id="indexParent + '-' + index" v-model="chosenAnswer" > </v-ons-radio> </label> <label class="center" :for="indexParent + '-' + index" > {{ letter }} . {{ choice }} </label> </v-ons-list-item> <script> export default { props: ['choice', 'indexParent', 'index', 'name', 'letter'], computed: { chosenAnswer: { get() { return this.letter; }, set() { this.$emit('change', this.letter); } } } } </script>How to use v-on:change in VueJS and send the parameter to get update JSON data with axios
Please see my code that I'am not sure what I'm doing wrong?. I want to get new JSON data when the id="select1" has changed and then send the parameter to id="select2" and then id="select2" will be show data in option.
var appZone = new Vue({ el: '#el', data() { return { options: [], list:[], selected:'' } }, mounted() { var self = this axios.get('https://www.iam-tour.com/wp-json/tour-api/v1/search/0') .then(function (response) { console.log(response); self.options = response.data; }) .catch(function (error) { console.log(error); }); }, methods: { onChange: function (){ axios.get('https://www.iam-tour.com/wp-json/tour-api/v1/search/'+this.selected) .then(function (response) { //console.log(response); self.list = response.data; console.log(list) }) .catch(function (error) { console.log(error); }); } } }) <script src="https://unpkg.com/vue"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <div id="el"> <select id="select1" v-model="selected" class="custom-select" v-on:change="onChange" > <option v-for="item in options" v-bind:value="item.id" > {{ item.title }} </option> </select> <span>Selected: {{ selected }}</span> <select id="select2" class="custom-select" > <option v-for="data in list" v-bind:value="data.country_id">{{ data.title }}</option> </select> </div>
Automatically log out user when token is invalidated
I have a SPA that is built on vuejs. When a user is logged in via API, the token is stored in local storage.
I need a global solution which will logout and prompt the user when the token is no longer valid. At the moment, I get "invalid token" error when accessing private API endpoints.
How do I rig axios so that ALL response of invalid tokens will trigger the logout/prompt code?
How to run Vue.js dev serve with https?
I'm using Vue-cli to create vue project with webpack template. how to run it with https in development using: npm run dev?
Vue attach PDF file in form
I have a form right now I built in vue. The issue I am having is that when I attach the file I can console out the atachement and see its an object but using vue tools its just listed as file.
Template
<input class="file-input" type="file" @change="onFileChange">Method
onFileChange(e) { var files = e.target.files || e.dataTransfer.files; if (!files.length) return; this.form.resume = files[0]; console.log(files[0]); },Vue.js not updating automatically
I have got this in my html code
<div class="pl_wrapper"> <div class="options_pl"> <input type="button" @click="optionButtonClicked" class="option_button_pl" value="List"> <input type="button" @click="optionButtonClicked" class="option_button_pl" value="Add a language"> </div> {{show2}} </div>And this in my vue.js
const ProgrammingLanguages = new Vue({ el:".pl_wrapper", data:{ name: "aa", show1: false, show2: false }, methods: { optionButtonClicked(){ var indexOfClicked = index(event.target,event.target.className); var equalIndexOfDiv = getOnIndex(indexOfClicked,"pl_divs") $(".options_pl").hide(); show1 = (indexOfClicked==0) ? true : false show2 = (indexOfClicked==1) ? true : false } } });Now when i click option_button_pl i expect {{show2}}'s to also change from false to true and vice versa.But alas that doesn't Jsfiddle: happen.https://jsfiddle.net/3akfzcyf/
Vue resource - dynamically determine http method
I would like to dynamically determine the appropriate http method and make a single api call. However an exception is thrown when I call the method.
I expect that I am doing something wrong rather than this being a vue-resource bug. Would anyone have any advice? Thanks
For example:
let method = this.$http.post if (this.model.id) { method = this.$http.put } method( this.url, this.model, options ).then(response => { this.$router.push(this.redirect_to) }).catch(response => { console.log(`Error: ${response.statusText}`) })A javascript TypeError is thrown with message "this is not a function"
The code below works, but a bit long winded.
if (this.model.id) { this.$http.put( this.url, this.model, options ).then(response => { this.$router.push(this.redirect_to) }).catch(response => { console.log(`Error: ${response.statusText}`) }) } else { this.$http.post( this.url, this.model, options ).then(response => { this.$router.push(this.redirect_to) }).catch(response => { console.log(`Error: ${response.statusText}`) }) }