Atsuhiko / Web-App

Webアプリ研究会
2 stars 2 forks source link

Vuex #25

Open yuyuyuriko78 opened 4 years ago

yuyuyuriko78 commented 4 years ago

Vuex

yuyuyuriko78 commented 4 years ago

環境構築

yuyuyuriko78 commented 4 years ago

準備

src > store > index.js または src > store.js

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

Vue.use(Vuex);

main.js

import Vue from 'vue'
import App from './App.vue'
import store from './srore'

new Vue({
  store,
  render: h => h(App),
}).mount('#app')
yuyuyuriko78 commented 4 years ago

index.js(store.js)の基本構造

import Vue from 'vue';
import Vuex from 'vuex';
import データ from 'フォルダ';

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {},
  getters: {},
  mutations: {},
  actions: {},
});

state

名前 役割 補足
state データそのもの aa
getters stateのデータをVueコンポーネントに渡す aaa
mutations stateのデータを変更することができる唯一の存在 コミットにより呼び出す
actions APIと非同期通信を行い取得したデータをコミットする mutationsを呼び出す

まとめ

image image

yuyuyuriko78 commented 4 years ago

例 本棚アプリ

Vue.use(Vuex);

const store = new Vuex.Store({ state: { books, }, getters: { getNewBookId(state) { return state.books.length + 1; }, findBookById: (state) => (id) => { return state.books.find((book) => book.id === id); }, }, mutations: { saveNewBook: function(state, book){ state.books.push(book); }, updateBook: function(state, { oldBook, newBook }){ Object.assign(oldBook, newBook); }, }, actions: { addBook: function({ commit, getters}, book){ commit('saveNewBook', { id: getters.getNewBookId, ...book, }); }, updateBook: function({ commit, getters }, {id, book} ){ const oldBook = getters.findBookById(id); if(oldBook) { commit('updateBook', {oldBook, newBook: book}); } } }, });

export default store;


book.js
```js
const books = [
  {
    id: 1,
    title: 'JS Book',
    author: 'Suzuki',
    image: require('@/assets/web.png'),
    isFavorite: true,
  },
  {
    id: 2,
    title: 'PHP Book',
    author: 'Tanaka',
    image: require('@/assets/programming.png'),
    isFavorite: false,
  },
];

export default books;
yuyuyuriko78 commented 4 years ago

①dispatch → actions

this.$store.dispatch('アクション名', 渡すデータ)

NewBook.vue(コンポーネント)

<script>
import BookForm from '@/components/BookForm';
export default {
  name: 'NewBook',
  components: { BookForm },
  methods: {
    onSubmit(book) {
      this.$store.dispatch('addBook', book);
      this.$router.push('/');
    },
  },
};
</script>

store.js

actions: {
    addBook: function({ commit, getters }, book){
      commit('saveNewBook', {
        id: getters.getNewBookId,
        ...book,
      });
    },
  },
yuyuyuriko78 commented 4 years ago

②actions → mutations

store.js

mutations: {
    saveNewBook: function(state, book){
      state.books.push(book);
    },
  },
yuyuyuriko78 commented 4 years ago

storeの利用 (getters)

getterを利用しない

Top.vue(コンポーネント)

computed: {
    books() {
      return this.$store.state.books;
    }
}

getterを利用する

store.js

getters: {
  todos(state) {
    return state.books.author
  }
}

Sample.vue(コンポーネント)

computed: {
  todos() {
    this.$store.getters.todos
  }
}

store.js

getters: {
   squared: function(state) {
      return state.count * state.count
    },
}

Sample2.js

computed: {
  square() {
      this.$store.getters.squared
  }
}