账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    VUE-CLI做项目的时候,请求回来的数据怎么保存为全局?
    52
    0

    比如,用户登录,登录成功后拿到用户数据。如果有多个JS文件要用到用户数据,怎么办?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 队友最帅 普通会员 1楼

      在Vue CLI中,可以使用Vuex来管理应用的状态和数据。Vuex是一个状态管理库,它可以让你将数据存储在一个地方,而不需要每次都手动更新它。

      以下是如何在Vue CLI中保存请求返回的数据为全局:

      1. 首先,你需要安装Vuex库。在你的项目根目录下运行以下命令:

      npm install vuex

      1. 然后,你需要在你的项目中创建一个store文件,它将被Vuex用来管理应用的状态。在store文件中,你可以定义你的状态变量,这些变量可以被Vue组件引用。

      javascript export default { state: { // state variables }, mutations: { // mutations to modify the state }, actions: { // actions to perform actions on the state }, getters: { // getters to access the state } }

      1. 然后,你需要在你的Vue组件中使用store来管理状态。你可以使用Vue的状态API来访问和修改store的状态。

      javascript export default { data() { return { // data to be passed to the component }; }, methods: { // methods to be called on the component }, computed: { // computed to access the state }, store: { // the instance of the Vuex store } }

      1. 最后,你需要在你的应用程序的主入口文件(如index.html或main.js)中配置Vuex。

      ```html

      ```

      ```javascript import Vue from 'vue' import Vuex from 'vuex'

      Vue.use(Vuex)

      new Vuex.Store({ state: { // state variables }, mutations: { // mutations to modify the state }, actions: { // actions to perform actions on the state }, getters: { // getters to access the state } }) ```

      这样,你就可以在你的应用程序中保存请求返回的数据为全局了。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部