質問するログイン新規登録

回答編集履歴

1

長文追記した。

2020/08/17 06:23

投稿

退会済みユーザー
answer CHANGED
@@ -78,4 +78,141 @@
78
78
  </script>
79
79
 
80
80
  </html>
81
- ```
81
+ ```
82
+ ## 追記
83
+ 長々と書くのもアレだったので短く書きすぎました。解決後ですが追記しておきます。
84
+
85
+ まず、nuxt.jsを使ったもので説明します。
86
+ ``npx create-nuxt-app sample-nuxt``を↓な感じで作ります。
87
+ ```text
88
+ create-nuxt-app v3.2.0
89
+ ✨ Generating Nuxt.js project in sample-nuxt
90
+ ? Project name: sample-nuxt
91
+ ? Programming language: JavaScript
92
+ ? Package manager: Npm
93
+ ? UI framework: Vuetify.js
94
+ ? Nuxt.js modules: (Press <space> to select, <a> to toggle all, <i> to invert selection)
95
+ ? Linting tools: (Press <space> to select, <a> to toggle all, <i> to invert selection)
96
+ ? Testing framework: None
97
+ ? Rendering mode: Single Page App
98
+ ? Deployment target: Server (Node.js hosting)
99
+ ? Development tools: (Press <space> to select, <a> to toggle all, <i> to invert selection)
100
+ ```
101
+ 次に以下のファイルを作ったり、置き換えたりします。
102
+
103
+ **pages/index.vue**
104
+ ```vue
105
+ <template>
106
+ <v-container>
107
+ <counter />
108
+ </v-container>
109
+ </template>
110
+
111
+ <script>
112
+ import Counter from "~/components/Counter.vue";
113
+
114
+ export default {
115
+ components: {
116
+ Counter,
117
+ },
118
+ };
119
+ </script>
120
+ ```
121
+ **components/Counter.vue**
122
+ ```vue
123
+ <template>
124
+ <v-card>
125
+ <v-card-title>{{ count }}</v-card-title>
126
+ <v-card-actions>
127
+ <v-btn text @click="increment()">increment</v-btn>
128
+ <v-btn text @click="push()">push</v-btn>
129
+ </v-card-actions>
130
+ </v-card>
131
+ </template>
132
+
133
+ <script>
134
+ import { mapMutations, mapGetters } from 'vuex'
135
+
136
+ export default {
137
+ name: "Counter",
138
+ computed: {
139
+ count: function () {
140
+ return this.$store.getters['counterstore/count'];
141
+ },
142
+ },
143
+ methods: {
144
+ increment: function () {
145
+ console.log("vue_component: this.$vuetify=" + this.$vuetify);
146
+ this.$store.commit("counterstore/increment");
147
+ },
148
+ push: function () {
149
+ this.$store.commit("counterstore/push", this.$vuetify);
150
+ },
151
+ },
152
+ };
153
+ </script>
154
+ ```
155
+ **store/counterstore.js**
156
+ ```javascript
157
+ export const state = () => ({
158
+ counter: 0
159
+ })
160
+
161
+ export const mutations = {
162
+ increment(state) {
163
+ state.counter++;
164
+ console.log("store_increment: this.$vuetify =" + this.$vuetify);
165
+ },
166
+ push(state, args) {
167
+ // 無理矢理受け渡す
168
+ this.$vuetify = args;
169
+ console.log("store_push: this.$vuetify is set!");
170
+ }
171
+ }
172
+
173
+ export const getters = {
174
+ count(state) {
175
+ if (this != null) {
176
+ console.log('store_getters: this.$vuetify =' + this.$vuetify);
177
+ } else {
178
+ console.log('store_getters: this =' + this);
179
+ }
180
+ return state.counter;
181
+ },
182
+ }
183
+ ```
184
+ 動かすと、初期画面がこんな感じになります。
185
+ ![イメージ説明](088602bcf8b96781446acb0d27a9343d.png)
186
+
187
+ consoleの表示は、
188
+ ```text
189
+ store_getters: this =undefined
190
+ ```
191
+ な感じです。gettersでは呼出元が関数呼出してるので、thisが入っていません。
192
+ まずgettersを使うとthis.$vuetifyは無理なのですよ。
193
+
194
+ 次にINCREMENTボタンを押します。するとconsoleは
195
+ ```text
196
+ vue_component: this.$vuetify=[object Object]
197
+ store_increment: this.$vuetify =undefined
198
+ store_getters: this =undefined
199
+ ```
200
+ と出ています。vueコンポーネントからはthis.$vuetifyが入っていますが、生成時にプラグインからそういうのを入れるカラクリがないstoreにはthis.$vuetifyが入っていないのが分かります。
201
+
202
+ 仕方がないので実装したPUSHボタンを押すと...
203
+ ```text
204
+ store_push: this.$vuetify is set!
205
+ ```
206
+ 引数で無理矢理コンポーネントから参照してたvuetifyのインスタンスを渡されるので、storeにvuetifyが設定されます。
207
+
208
+ 再度INCREMENTを押すと...
209
+ ```text
210
+ vue_component: this.$vuetify=[object Object]
211
+ store_increment: this.$vuetify =[object Object]
212
+ store_getters: this =undefined
213
+ ```
214
+ store側のincrementでthis.$vuetifyが入ってることが分かります。getterでは相変わらず見えませんが...
215
+
216
+ つまり、**Vueコンポーネントでは見えるthis.$vuetifyも、Vuexからはインスタンス生成しただけでは見えない**ということです。またgettersからはどうやっても見えません(Vue.prototype.$vuetifyも駄目でした。今はそういう渡し方じゃないのかも)。で、それを伝えようとしたのが最初のコードだったわけです。なお、この辺TypeScriptだとまた事情が変わるかもです。そのクラス表記はさらに不明です。
217
+
218
+ 長文失礼しました。詳しい部分じゃないので、間違ってたら指摘お願いします。