1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

NuxtでVuex を使いかた覚えるために作ったもの

Posted at

Vueで書かれている本をNuxtに書き換えたときのメモ
NuxtやVue関してあまり詳しくないのでもっといい方法あれば教えて欲しい

リアルタイムで入力した値がVuexを通して変換するものを作りました。

まずファイル構成はこんな感じ 
スクリーンショット 2020-01-13 13.54.52.png
スクリーンショット 2020-01-13 13.55.12.png

###pages/index.vue

.<template>
  <div>
    {{message}}
    <EditForm />
  </div>
</template>

<script>
import EditForm from "./component/EditForm";

export default {
  computed: {
    message() {
      return this.$store.state.store.message;
    }
  },
  components: {
    EditForm
  }
};
</script>

###pages/component/index.vue

<template>
  <div>
    <input :value="message" @input="doUpdate" />
  </div>
</template>

<script>
export default {
  computed: {
    message() {
      return this.$store.state.message;
    }
  },
  methods: {
    doUpdate(event) {
      this.$store.dispatch("store/doUpdate", event.target.value);
    }
  }
};
</script>

###store/store.js


export default {
    name: 'store',
    state: {
        message: 'Hello Vue.js'
    },

    // 
    mutations: {
        setMessage(state, payload) {
            state.message = payload.message
        }
    },

    actions: {
        doUpdate({ commit }, message) {
            commit('setMessage', { message })
        }
    }
}

##メモ(つまずいたところ)
this.$store.dispatch("store/doUpdate", event.target.value);
の部分本ではdoUpdateのみになっているがこの書き方にせねばならん

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?