Advertisement
fabiobiondi

Vue CRUD Component basic example

Dec 28th, 2017
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.   <div>
  3.     <Toggable :title="`PROFILE OF ${active.name}`">
  4.       <UserForm :active="active"
  5.                 @save="save($event)" />
  6.     </Toggable>
  7.  
  8.     <hr>
  9.  
  10.     <Toggable :title="`TOTAL ${users.length}`">
  11.       <UsersList :items="users"
  12.                  :active="active"
  13.                  @select="setActive($event)"
  14.                  @delete="deleteMe($event)" />
  15.     </Toggable>
  16.  
  17.   </div>
  18. </template>
  19.  
  20. <script>
  21.   import axios from 'axios';
  22.  
  23.   import Toggable from '../../components/Toggable.vue';
  24.   import UserForm from './components/UserForm.vue';
  25.   import UsersList from './components/UsersList.vue';
  26.  
  27.   export default {
  28.     created() {
  29.       axios.get('https://jsonplaceholder.typicode.com/users')
  30.         .then((res) => {
  31.           this.users = res.data;       //  list of users
  32.           this.active = this.users[0]; // current active user
  33.         })
  34.     },
  35.     data() {
  36.       return {
  37.         users: [],
  38.         active: {},
  39.       }
  40.     },
  41.     methods: {
  42.       setActive(user) {
  43.         console.log('set', user)
  44.         this.active = user;
  45.       },
  46.       deleteMe(user) {
  47.         // ...
  48.       },
  49.       save(formData) {
  50.         this.active = {...formData};
  51.         this.update(formData);
  52.       },
  53.       update(userUpdated) {
  54.         // ...
  55.       }
  56.     },
  57.     components: {
  58.       Toggable,   // this should not be local but moved to global
  59.       UserForm, UsersList
  60.     },
  61.   }
  62. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement