質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Vuex

Vuexは、Vue.js アプリケーションのための状態管理ライブラリです。アプリケーション内で使用するコンポーネントのための集中データストアを提供。コンポーネント同士でデータをやり取りし、処理のフローを一貫させたり、データの見通しを良くすることができます。

Q&A

0回答

464閲覧

Vuexのactionsで値をmutationsに渡す方法が分からない

Tikka123456

総合スコア34

Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Vuex

Vuexは、Vue.js アプリケーションのための状態管理ライブラリです。アプリケーション内で使用するコンポーネントのための集中データストアを提供。コンポーネント同士でデータをやり取りし、処理のフローを一貫させたり、データの見通しを良くすることができます。

0グッド

0クリップ

投稿2021/05/31 12:30

Vuexでactions内で非同期処理をする際、responceからきたデータを加工してmutationsに渡して表示する処理を書いています。
その際、actions内の処理が実行されません。
よろしくお願いします

Vuexの処理
index.js

import Vuex from 'vuex' import axios from 'axios' const createStore = () => { return new Vuex.Store({ state: function (){ return { tests: [], test: '' } }, getters: { tests: state => state.tests, test: state => state.test }, mutations: { settests(state, { tests }){ state.tests = tests }, settest(state, { test }){ state.test = test }, }, actions: { getTest({commit}, tests) { axios.get('http://localhost:8000/api/test') .then(res => { // 返ってきた値を配列に変換している const tests = []; const values = Object.values(res.data); values.forEach((value) => { const test = { content: value.content, created_at: value.created_at, updated_at: value.updated_at, id: value.id }; keywords.push(test); });      // ここの値をcommitしたい state.tests = tests; commit('settests', {tests: state.tests}); }) .catch(error => { console.log(error.response); }); } } }) } export default createStore

Home.vue

<template> <div> <el-main> <el-table :data="$store.state.tests" style="width: 100%"> <el-table-column label="キーワード" prop="content" width="180"> <template slot-scope="scope"> {{ scope.row.content }} </template> </el-table-column> <el-table-column prop="created_at" label="投稿日時" width="180"> <template slot-scope="scope"> {{ scope.row.created_at }} </template> </el-table-column> <el-table-column prop="updated_at" label="更新日時" width="180"> <template slot-scope="scope"> {{ scope.row.updated_at }} </template> </el-table-column> </el-table> </el-main> </div> </template> <script> import axios from 'axios'; import { mapGetters } from "vuex"; import { mapActions } from "vuex"; export default { async asyncData({ store }) { await store.dispatch("getTest"); }, computed: { ...mapGetters(["tests"]), }, methods: { ...mapActions(["getTest"]), } } </script>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問