Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第70题:请描述一下什么是Vuex? #70

Open
noxussj opened this issue Jun 18, 2020 · 0 comments
Open

第70题:请描述一下什么是Vuex? #70

noxussj opened this issue Jun 18, 2020 · 0 comments
Labels

Comments

@noxussj
Copy link
Owner

noxussj commented Jun 18, 2020

什么是Vuex?

Vuex 是一个专为 Vue.js 应用程序开发的状态管理器,采用集中式存储管理应用的所有组件的状态。也可以理解为vuex主要存储一些全局的变量

优势和劣势有哪些?

  • 优势:主要就是可以全局共享数据,方法。方便统一管理

  • 劣势:页面刷新后state的变量都会还原清空,不会像cookies一样持久性存储

页面刷新后vuex的state数据丢失怎么解决?

先说一下为什么会丢失呢?
因为store里的数据是保存在运行内存中的,当页面刷新时页面会重新加载vue实例,store里面的数据就会被重新赋值

如何避免?
其实主要还是看使用的场景是怎样的,如果想某些数据持久性保留也可以搭配使用cookies或者localStorage。比如一些登录的信息等。

比如请求拿到了登录信息后可以先存在cookies,路由加载的时候可以把cookies的登录信息赋值到vuex中。最后页面直接使用vuex中的变量

Vuex有哪些属性?

  • state:可以理解为和data属性一样,存储一些全局公用的变量

  • getters:类似computed计算属性,也可以在state变量的基础上进行处理并且返回新的值

  • mutations:主要是定义同步方法,并且推荐state中的变量由该方法进行更改,方便数据跟踪,调用方法:this.$store.commit('xxx', params)

  • actions:主要是定义异步方法,调用方法:this.$store.dispatch('xxx', params)

  • modules:定义子模块,每个子模块也会有state、getters、mutations、actions、modules

总结:不建议通过直接赋值的操作改变state变量,推荐使用mutations来改变

@noxussj noxussj added the Vue label Jun 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant