简体   繁体   English

如何 map state 返回 Vue.js AWS Amplify auth 页面中的组件

[英]How to map state back to component in Vue.js AWS Amplify auth page

I am building an authentication page with Vue.js, Vuex, and AWS Amplify.我正在使用 Vue.js、Vuex 和 AWS Amplify 构建身份验证页面。

This auth page is based off Erik Hanchett's AWS Auth Example ( https://github.com/ErikCH/Aws-auth-example/blob/master/src/components/HelloWorld.vue ).此身份验证页面基于 Erik Hanchett 的 AWS 身份验证示例 ( https://github.com/ErikCH/Aws-auth-example/blob/master/src/components/HelloWorld.vue )。 Erik's original demo utilized Vuex for state management, but for the sake of simplicity only employs the state handler in the store.js file. Erik 的原始演示使用 Vuex 进行 state 管理,但为了简单起见,仅在 store.js 文件中使用state处理程序。

I am attempting to reconfigure this demo so that the various methods and hooks in HelloWorld.vue are set up to also dispatch actions and commit mutations.我正在尝试重新配置此演示,以便将HelloWorld.vue中的各种方法和挂钩设置为也调度操作和提交突变。

So far, I have been successful in setting up the findUser() method in HelloWorld.vue to dispatch actions, pass user and signedIn as payloads to their respective action handlers, and then commit mutations.到目前为止,我已经成功地在HelloWorld.vue中设置了findUser()方法来调度动作,将usersignedIn作为有效负载传递给它们各自的action处理程序,然后提交突变。

However, my issue now pertains to the computed property in the HelloWorld component.但是,我的问题现在与HelloWorld组件中的computed属性有关。

Erik's original demo returns the state directly to the component using return this.$store.state.signedIn as seen in the computed property. Erik 的原始演示使用return this.$store.state.signedIn将 state 直接返回到组件,如computed属性所示。 Based on my experience with Vuex in other projects, I would normally use a mapState helper to map directly to the state.根据我在其他项目中使用 Vuex 的经验,我通常会使用mapState助手将 map 直接用于 state。

Is it correct in this project to use this.$store.state.signedIn to return the state?在这个项目中使用this.$store.state.signedIn返回 state 是否正确? Or should I use mapState ?或者我应该使用mapState吗? If so, how can I reconfigure this computed property in order to employ mapState to map directly to signedIn ?如果是这样,我如何重新配置此computed属性以便将 mapState 直接用于mapStatesignedIn

My code is below:我的代码如下:

HelloWorld.vue你好世界.vue

<template>
  <div class="hello">
    <div v-if="!signedIn">
      <amplify-authenticator></amplify-authenticator>
    </div>
    <div v-if="signedIn">
      <Home></Home>
    </div>
  </div>
</template>

<script>
import { Auth } from 'aws-amplify'
import { AmplifyEventBus } from 'aws-amplify-vue';
import { mapState } from 'vuex'
import Home from '../components/Home.vue'
export default {
  name: 'HelloWorld',
  components: {
    Home
  },
  data() {
    return {
      login: '',
      password: ''
    }
  },
  props: {
    msg: String,
  },
  created(){
    this.findUser();
    AmplifyEventBus.$on('authState', info => {
      if(info === "signedIn") {
        this.findUser();
      } else {
        this.$store.state.signedIn = false;
        this.$store.state.user = null;
      }
    });
  },
  computed: {
    signedIn(){
      return this.$store.state.signedIn;
    }
  },
  methods: {
    async findUser() {
      try {
        const user = await Auth.currentAuthenticatedUser();
        let signedIn = true
        this.$store.dispatch('setUser', user)
        this.$store.dispatch('setSignedIn', signedIn)
      }
      catch(err) {
        let signedIn = false
        this.$store.dispatch('setSignedIn', signedIn)
      }
    }
  }
}
</script>

Store.js Store.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: null,
    signedIn: false
  },
  mutations: {
    setUser(state, user) {
      state.user = user
    },
    setSignedIn(state, signedIn) {
      state.signedIn = signedIn
    }
  },
  actions: {
    setUser: (context, user) => {
      context.commit('setUser', user)
    },
    setSignedIn: (context, signedIn) => {
      context.commit('setSignedIn', signedIn)
    }
  }
})

Home.vue主页.vue

<template>
  <div class="goodbye">
    <h1>HOME</h1><br>
      <amplify-sign-out></amplify-sign-out>
  </div>
</template>

<script>
import { Auth } from 'aws-amplify'
export default {
  name: 'Home',
  data() {
    return {
      login: '',
      password: ''
    }
  },
  props: {
    msg: String,
  },
  methods: {
    signOut() {
      Auth.signOut()
    }
  }
}
</script>

The mapState helper is just sugar syntax for not repeating multiple times the whole this.$store.state.foo piece of code. mapState助手只是糖语法,不会重复整个this.$store.state.foo一段代码。

You can certainly use mapState like this你当然可以像这样使用mapState

import { mapState } from 'vuex'

computed: mapState([
  // map this.signedIn to this.$store.state.signedIn
  'signedIn'
])

Or like this if you want to also use local properties besides the ones of mapState或者像这样,如果您还想使用mapState之外的本地属性

import { mapState } from 'vuex'

computed: 
   localComputed () { /* ... */ },
   ...mapState([
   // map this.signedIn to this.$store.state.signedIn
     'signedIn'
   ])

Here are the docs for more information on this.这是有关此内容的更多信息的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将身份验证持久性添加到 Vue.js AWS-amplify 应用程序 - How to add authentication persistence to Vue.js AWS-amplify app 在另一个组件 Vue.js 中调用 state - Call state in another component Vue.js AWS Amplify React UI 组件 - 如何调度 auth state 更改事件? - AWS Amplify React UI Component - How to dispatch auth state change event? 在使用`pushState`后浏览浏览器历史记录时,如何在我的Vue.js页面中执行状态更改? - How do perform state changes in my Vue.js page when navigating back through browser history after using `pushState`? 如何在 Vue.js 中创建/加载的组件/页面上显示模式 - How to show modal on component/page created/load in Vue.js Vue.js:如果用户单击后退按钮,如何将用户带回到组件中的位置 - Vue.js: How to bring user back to the spot in a component if they click back button 如何针对特定的单击组件并使用 Vue.js 为特定组件设置 state? - How to target specific clicked component and set the state for the specific component with Vue.js? vue.js:如何用对象更新状态? - vue.js: how to update state with object? 使用浏览器后退按钮vue.js的状态历史记录 - State history with browser back button vue.js 如何将AWS SDK导入Vue.JS - How to import the AWS SDK into Vue.JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM