Last active
May 23, 2019 16:46
-
-
Save storuky/78d5edbcd75b17b23330c309621ae75f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<form @submit.prevent="addUser"> | |
<input type="text" v-model="form.name" /> | |
<button type="submit">Add user</button> | |
</form> | |
<ul> | |
<li :key="user.id" v-for="user in users">{{user.name}}</li> | |
</ul> | |
</div> | |
</template> | |
<script> | |
import {mapState} from "vuex"; | |
export default { | |
data () { | |
return { | |
form: {} | |
} | |
}, | |
created () { | |
this.setUsers(); | |
}, | |
methods: { | |
setUsers () { | |
// Запрашиваем юзеров, устанавлием стейт entities.users = [<User>, ...] | |
this.$store.dispatch("entities/query", { | |
name: "users", | |
resource: this.$api.User.query | |
}); | |
}, | |
async addUser () { | |
await this.$store.dispatch("entities/query", { | |
name: "users", | |
resource: this.$api.User.create, | |
params: this.form | |
}); | |
this.form = {}; | |
alert("User was successfully added"); | |
} | |
}, | |
computed: { | |
...mapState({ | |
users: (state) => state.entities.users | |
}) | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment