vue-resource

How to make an async call in a beforeCreate hook of a Vue instance?

99封情书 提交于 2019-12-19 15:37:45
问题 I am building a Vuejs app with authentication. When the page is loaded and I initialise the app Vuejs instance, I am using beforeCreate hook to set up the user object. I load a JWT from localStorage and send it to the backend for verification. The issue is that this is an async call, and the components of this app object (the navbar, the views etc.) are being initialised with the empty user data before the call returns the result of the verification. What is the best practice to delay the

How to make an async call in a beforeCreate hook of a Vue instance?

你说的曾经没有我的故事 提交于 2019-12-19 15:36:12
问题 I am building a Vuejs app with authentication. When the page is loaded and I initialise the app Vuejs instance, I am using beforeCreate hook to set up the user object. I load a JWT from localStorage and send it to the backend for verification. The issue is that this is an async call, and the components of this app object (the navbar, the views etc.) are being initialised with the empty user data before the call returns the result of the verification. What is the best practice to delay the

Which VueJS lifecycle hook must Asynchronous HTTP requests be called in?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 10:22:23
问题 I'd like to know how before I render a page, I want to send an async GET request to my server to retrieve data and populate the properties in data. I heard the best way to do this is to call the function that sends this request in one of the three lifecycle hooks Vue js offers that operate before the DOM is rendered. The three are beforeCreate() , created() , beforeMount() . Which one must this request be called in ideally? And why? 回答1: Vue's initialization code is executed synchronously.

Vue.js - How to properly watch for nested data

℡╲_俬逩灬. 提交于 2019-12-17 06:57:30
问题 I'm trying to understand how to properly watch for some prop variation. I have a parent component (.vue files) that receive data from an ajax call, put the data inside an object and use it to render some child component through a v-for directive, below a simplification of my implementation: <template> <div> <player v-for="(item, key, index) in players" :item="item" :index="index" :key="key""> </player> </div> </template> ... then inside <script> tag: data(){ return { players: {} }, created(){

vue-其他

怎甘沉沦 提交于 2019-12-15 10:48:10
vue-resource // 2.1 导入 vue-resource import VueResource from 'vue-resource' // 2.2 安装 vue-resource Vue.use(VueResource) // 设置请求的根路径 Vue.http.options.root = 'http://vue.studyit.io'; // 全局设置 post 时候表单数据格式组织形式 application/x-www-form-urlencoded Vue.http.options.emulateJSON = true; moment // 导入格式化时间的插件 import moment from 'moment' // 定义全局的过滤器 Vue.filter('dateFormat', function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss") { return moment(dataStr).format(pattern) }) preview // 安装 图片预览插件 import VuePreview from 'vue-preview' Vue.use(VuePreview) 缩略图使用实例 <template> <div class="photoinfo-container"> <h3>{{

Not able to use $http or Vue.http

落花浮王杯 提交于 2019-12-12 18:12:16
问题 FOLLOW UP A combination of comments answered this question. A followup is why the import/require statements matter. I thought the effectively did the same thing. ORIGINAL QUESTION I'm trying to use vue-resource to make REST calls to an API. I have up to now a working Vue app that also uses vue-material and it works nicely. I'm just having trouble getting a reference to the http "object" through which I can make get/post/put calls. The stack is basically vue-cli to create a webpack/npm project

Why am I getting a 422 error code?

不羁岁月 提交于 2019-12-12 10:37:17
问题 I am making a POST request, but unable to get anything besides a 422 response. Vue.js client code: new Vue({ el: '#app', data: { form: { companyName: '', street: '', city: '', state: '', zip: '', contactName: '', phone: '', email: '', numberOfOffices: 0, numberOfEmployees: 0, } }, methods: { register: function() { this.$http.post('/office-depot-register', this.form).then(function (response) { // success callback console.log(response); }, function (response) { // error callback console.log

How to add preloader and success message on form submit using vue-resource

旧街凉风 提交于 2019-12-11 08:45:56
问题 How to accomplish below task using vue-resource: Include preloader text Loading... or gif image when fetching the data from the server. Show success message on form submit. 回答1: One way of doing this is : <template> <div> <div class="loader" v-if="loader"></div> <div> //display fetchedData using logic you wish like v-for..... </div> <form> //your form inputs <button @click.prevent="submit">Submit</button> </form> </div> </template> <script> export default{ data(){ return{ loader: false,

The content not shown properly in function callback in Vue.js

最后都变了- 提交于 2019-12-11 07:57:31
问题 I've got two problems here. The first is that I can't get the star rendered properly. I can do it if I change the value in the data() function but if I want to do it in a function callback way, it doesn't work (see comments below). What's going wrong here? Does it have something to do with Vue's lifecycle? The second one is that I want to submit the star-rate and the content of the textarea and when I refresh the page, the content should be rendered on the page and replace the <textarea><

How to know when all child components are loaded in vue.js

那年仲夏 提交于 2019-12-11 07:33:10
问题 I have a vue.js app where there are several child components like this: <main-app> <child-component></child-component> <child-component></child-component> <child-component></child-component> ... </main> When the page is loaded, I can use ready hook to know that the components are done loading. Now, these components are filled using ajax data and they can be manually updated by clicking a button. I can get data from each child-component by using broadcast and dispatch methods. But I want to