[英]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()
方法来调度动作,将user
和signedIn
作为有效负载传递给它们各自的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 直接用于mapState
到signedIn
?
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.