vuex

How to structure api calls in Vue.js?

混江龙づ霸主 提交于 2019-12-02 15:10:55
I'm currently working on a new Vue.js application. It depends heavily on api calls to my backend database. For a lot of things I use Vuex stores because it manages shared data between my components. When looking at other Vue projects on github I see a special vuex directory with files that handles all the actions, states and so on. So when a component has to call the API, it includes the actions file from the vuex directory. But, for messages for example, I don't want to use Vuex because those data is only important for one specific view. I want to use the component specific data here. But

nuxt学习

会有一股神秘感。 提交于 2019-12-02 14:55:15
1 引言 Nuxt 是基于 Vue 的前端开发框架,这次我们通过 Introduction toNuxtJS 视频了解框架特色以及前端开发框架的基本要素。 nuxt 与 next 结构很像,可以结合在一起看 视频简介: NuxtJs 的安装、目录结构、页面路由、导航模版、asyncData、meta、vueX。 这是一个入门级视频,所以上面所列举的特征都是一个前端开发框架的最核心的基本要素。一个前端开发框架,安装、目录结构、页面路由、导航模版一定是最要下功夫认真设计的。 asyncData 和 Vuex 都在解决数据问题,meta 则是通过约定语法控制网页 meta 属性,这部分值得与 React 体系做对比,在精读部分再展开。 Nuxtjs 前端开发框架不仅提供了脚手架的基本功能,还对项目结构、代码做了约定,以减少代码量。从这点可以看出,脚手架永远围绕两个核心目标: 让每一行源码都在描述业务逻辑;让每个项目结构都相同且易读 。 Nuxtjs 等框架要做的就是定义支持现代大型项目的前端研发标准,这个规范具有网络效应,即用的人越多,价值越大。 接下来我们进入正题,看看 Nuxt 脚手架定义了怎样的开发规范。 2 概述 安装 使用 npx create-nuxt-app app-name 创建新项目。这个命令与 create-react-app 一样,区别主要是模版以及配置不同。

Vuex 2.0 Dispatch versus Commit

半世苍凉 提交于 2019-12-02 14:52:20
Can someone explain when you would use a dispatch versus a commit? I understand a commit triggers mutation, and a dispatch triggers an action. However, isn't a dispatch also a type of action? Mani As you rightly said, $dispatch triggers an action, and commit triggers a mutation. Here is how you can use these concepts: You always use $dispatch from your methods in routes / components. $dispatch sends a message to your vuex store to do some action. The action may be done anytime after the current tick , so that your frontend performance is not affected. You never commit from any of your

vuex 的使用

丶灬走出姿态 提交于 2019-12-02 13:21:21
一、state 唯一数据源 main.js中添加 const store = new Vuex.Store ({ state: { count: 0 } }) new Vue({ store, render: h => h(App) }).$mount("#app") 使用方式一:(App.vue中) <template> <h3>{{this.$store.state.count}}</h3> </template> 方式二: <template> <h3>{{count}}</h3> </template> <script> export default { name: "app", computed: { count() { return this.$store.state.count; } }} </script> 二、mutation 更改store 中的状态的唯一方法是提交 mutation, 同步函数 实现count++ main.js中 const store = new Vuex.Store ({ state: { count: 0 }, mutations: { countIncrease (state) { state.count++ } } }) App.vue中 <template> <div> <h3>{{count}}</h3> <input type

Vuex dynamic checkboxes binding

北城余情 提交于 2019-12-02 13:12:00
问题 I have a problem with binding checkboxes using Vuex. On checkbox I use v-model with variable which has getter and setter to set or get value in store, the problem is that I get wrong data in store and I don't understand what cause the problem. Checkboxes bind to store property and this property must contain array of id's from checkboxes, but when I click checkbox more than one time it rewrite or remove store values. Can anyone help me to understand why does this happens? Link to jsFiddle. The

vuex的使用

与世无争的帅哥 提交于 2019-12-02 12:51:50
vuex 使用 在vue中导入 vuex : npm install vuex --save 在src中设置 router 文件夹用来保存vuex 数据 //2.1 导入vuex import Vue from 'vue' import Vuex from 'vuex' Vue.use ( Vuex ) //2.2 ```实例化仓库对象 export default new Vuex.Store ( { state: { //保存内容区域 userInfo: { //保存内容 name: "" , intro: "" , photo: "" , email: "" , id: "" , mobile: "" , token: "" , } } , mutations: { //调取的方法区 changeUserInfo ( state,newObj ) { // state 保存的内容 //newObject 可以存一个数字 一个数组 一个对象 Object.assign ( state.userInfo,newObj ) //使用es6 新语法 userInfo ( 特点会给userInfo赋值 newObj内部没有的数据不会进行赋值 ) window,localStorage.setItem ( 'userInfo' ,JSON.stringify ( state

五分钟搞懂Vuex

青春壹個敷衍的年華 提交于 2019-12-02 11:36:26
五分钟搞懂Vuex https://www.cnblogs.com/chinabin1993/p/9848720.html 这段时间一直在用vue写项目,vuex在项目中也会依葫芦画瓢使用,但是总有一种朦朦胧胧的感觉。于是决定彻底搞懂它。 看了一下午的官方文档,以及资料,才发现vuex so easy! 作为一个圈子中的人,决定输出一下文档,如果你仔细看完这篇文章,保证你对vuex熟练掌握。 我把自己的代码上传到了github,大家有需要的可以拉下来: github 先说一下vuex到底是什么? vuex 是一个专门为vue.js应用程序开发的状态管理模式。 这个状态我们可以理解为在data中的属性,需要共享给其他组件使用的部分。 也就是说,是我们需要共享的data,使用vuex进行统一集中式的管理。 vuex中,有默认的五种基本的对象: state:存储状态(变量) getters:对数据获取之前的再次编译,可以理解为state的计算属性。我们在组件中使用 $sotre.getters.fun() mutations:修改状态,并且是同步的。在组件中使用$store.commit('',params)。这个和我们组件中的自定义事件类似。 actions:异步操作。在组件中使用是$store.dispath('') modules:store的子模块,为了开发大型项目

Vuex store with “strict: true” does not work

馋奶兔 提交于 2019-12-02 11:25:49
问题 When I set strict: true I get error: vue.js:525 [Vue warn]: Error in watcher "state" (found in root instance) warn @ vue.js:525 run @ vue.js:1752 update @ vue.js:1720 notify @ vue.js:584 reactiveSetter @ vue.js:814 Transform.resize @ transform.js:169 e.resize @ map.js:343 e._onWindowResize @ map.js:1204 vue.js:1756 Uncaught Error: [vuex] Do not mutate vuex store state outside mutation handlers. at assert (vuex.js:182) at Vue$3.store._vm.$watch.deep (vuex.js:714) at Watcher.run (vue.js:1746)

How to mock a Vuex store in VueJS test-utils parentComponent

耗尽温柔 提交于 2019-12-02 09:33:55
I'm using Jest with vue-test-utils trying to test if a child component reacts to an $emit event in the parent component. VueJS test-utils library provides a parentComponent option to be passed when mounting/shallow mounting the component. Everything is working fine except that even though I instantiate the component with a mocked Vuex store, the parent component throws a TypeError: Cannot read property 'state' of undefined on a this.$store.state.something.here piece of code in the parent component. How can I mock the Vuex store there? The component mount looks like this: const wrapper =