@@ -15,8 +15,8 @@ SSR をしているとき、基本的にはアプリケーションの「スナ
15
15
import Vue from ' vue'
16
16
import Vuex from ' vuex'
17
17
Vue .use (Vuex)
18
- // Assume we have a universal API that returns Promises
19
- // and ignore the implementation details
18
+ // Promise を返すユニバーサルなアプリケーションを想定しています
19
+ // また、実装の詳細は割愛します
20
20
import { fetchItem } from ' ./api'
21
21
export function createStore () {
22
22
return new Vuex.Store ({
@@ -25,8 +25,8 @@ export function createStore () {
25
25
},
26
26
actions: {
27
27
fetchItem ({ commit }, id ) {
28
- // return the Promise via store.dispatch() so that we know
29
- // when the data has been fetched
28
+ // store.dispatch() を使って Promise を返します
29
+ // そうすればデータがフェッチされたときにそれを知ることができます
30
30
return fetchItem (id).then (item => {
31
31
commit (' setItem' , { id, item })
32
32
})
@@ -51,27 +51,27 @@ import { createRouter } from './router'
51
51
import { createStore } from ' ./store'
52
52
import { sync } from ' vuex-router-sync'
53
53
export function createApp () {
54
- // create router and store instances
54
+ // ルーターとストアのインスタンスを作成します
55
55
const router = createRouter ()
56
56
const store = createStore ()
57
- // sync so that route state is available as part of the store
57
+ // ルートのステートをストアの一部として利用できるよう同期します
58
58
sync (store, router)
59
- // create the app instance, injecting both the router and the store
59
+ // アプリケーションのインスタンスを作成し、ルーターとストアの両方を挿入します
60
60
const app = new Vue ({
61
61
router,
62
62
store,
63
63
render : h => h (App)
64
64
})
65
- // expose the app, the router and the store.
65
+ // アプリケーション、ルーター、ストアを露出します
66
66
return { app, router, store }
67
67
}
68
68
```
69
69
70
70
## ロジックとコンポーネントとの結び付き
71
71
72
- ではデータをプリフェッチするアクションをディスパッチするコードはどこに書けばよいでしょうか ?
72
+ ではデータをプリフェッチするアクションをディスパッチするコードはどこに置けばよいでしょうか ?
73
73
74
- フェッチする必要があるデータはアクセスしたルートによって決まります。またそのルートによってどのコンポーネントがレンダリングされるかも決まります。実のところ、与えられたルートに必要とされるデータは、そのルートでレンダリングされるコンポーネントに必要とされるデータでもあるのです。したがって、データをフェッチするロジックはルートコンポーネントの中に書くのが自然でしょう 。
74
+ フェッチする必要があるデータはアクセスしたルートによって決まります。またそのルートによってどのコンポーネントがレンダリングされるかも決まります。実のところ、与えられたルートに必要とされるデータは、そのルートでレンダリングされるコンポーネントに必要とされるデータでもあるのです。したがって、データをフェッチするロジックはルートコンポーネントの中に置くのが自然でしょう 。
75
75
76
76
ルートコンポーネントではカスタム静的関数 ` asyncData ` が利用可能です。この関数はそのルートコンポーネントがインスタンス化される前に呼び出されるため ` this ` にアクセスできないことを覚えておいてください。ストアとルートの情報は引数として渡される必要があります:
77
77
@@ -83,11 +83,11 @@ export function createApp () {
83
83
<script >
84
84
export default {
85
85
asyncData ({ store, route }) {
86
- // return the Promise from the action
86
+ // アクションから Promise が返されます
87
87
return store .dispatch (' fetchItem' , route .params .id )
88
88
},
89
89
computed: {
90
- // display the item from store state.
90
+ // ストアのステートから item を表示します
91
91
items () {
92
92
return this .$store .state .items [this .$route .params .id ]
93
93
}
@@ -112,7 +112,7 @@ export default context => {
112
112
if (! matchedComponents .length ) {
113
113
reject ({ code: 404 })
114
114
}
115
- // call asyncData() on all matched route components
115
+ // マッチしたルートコンポーネントすべての asyncData() を呼び出します
116
116
Promise .all (matchedComponents .map (Component => {
117
117
if (Component .asyncData ) {
118
118
return Component .asyncData ({
@@ -121,11 +121,10 @@ export default context => {
121
121
})
122
122
}
123
123
})).then (() => {
124
- // After all preFetch hooks are resolved, our store is now
125
- // filled with the state needed to render the app.
126
- // When we attach the state to the context, and the `template` option
127
- // is used for the renderer, the state will automatically be
128
- // serialized and injected into the HTML as window.__INITIAL_STATE__.
124
+ // すべてのプリフェッチのフックが解決されると、ストアには、
125
+ // アプリケーションをレンダリングするために必要とされるステートが入っています。
126
+ // ステートを context に付随させ、`template` オプションがレンダラーに利用されると、
127
+ // ステートは自動的にシリアライズされ、HTML 内に `window.__INITIAL_STATE__` として埋め込まれます
129
128
context .state = store .state
130
129
resolve (app)
131
130
}).catch (reject)
@@ -156,31 +155,31 @@ if (window.__INITIAL_STATE__) {
156
155
157
156
``` js
158
157
// entry-client.js
159
- // ...omitting unrelated code
158
+ // 関係のないコードは除外します
160
159
router .onReady (() => {
161
- // Add router hook for handling asyncData.
162
- // Doing it after initial route is resolved so that we don't double-fetch
163
- // the data that we already have. Using router.beforeResolve() so that all
164
- // async components are resolved.
160
+ // asyncData を扱うためにルーターのフックを追加します。これは初期ルートが解決された後に実行します
161
+ // そうすれば(訳注: サーバーサイドで取得したために)既に持っているデータを冗長に取得しなくて済みます
162
+ // すべての非同期なコンポーネントが解決されるように router.beforeResolve() を使います
165
163
router .beforeResolve ((to , from , next ) => {
166
164
const matched = router .getMatchedComponents (to)
167
165
const prevMatched = router .getMatchedComponents (from)
168
- // we only care about none-previously-rendered components,
169
- // so we compare them until the two matched lists differ
166
+ // まだレンダリングされていないコンポーネントにのみ関心を払うため、
167
+ // 2つのマッチしたリストに差分が表れるまで、コンポーネントを比較します
170
168
let diffed = false
171
169
const activated = matched .filter ((c , i ) => {
172
170
return diffed || (diffed = (prevMatched[i] !== c))
173
171
})
174
172
if (! activated .length ) {
175
173
return next ()
176
174
}
177
- // this is where we should trigger a loading indicator if there is one
175
+ // もしローディングインジケーターがあるならば、
176
+ // この箇所がローディングインジケーターを発火させるべき箇所です
178
177
Promise .all (activated .map (c => {
179
178
if (c .asyncData ) {
180
179
return c .asyncData ({ store, route: to })
181
180
}
182
181
})).then (() => {
183
- // stop loading indicator
182
+ // ローディングインジケーターを停止させます
184
183
next ()
185
184
}).catch (next)
186
185
})
@@ -199,9 +198,8 @@ if (window.__INITIAL_STATE__) {
199
198
beforeMount () {
200
199
const { asyncData } = this .$options
201
200
if (asyncData) {
202
- // assign the fetch operation to a promise
203
- // so that in components we can do `this.dataPromise.then(...)` to
204
- // perform other tasks after data is ready
201
+ // データが準備できた後に、コンポーネント内で `this.dataPromise.then(...)` して
202
+ // 他のタスクを実行できるようにするため、Promise にフェッチ処理を割り当てます
205
203
this .dataPromise = asyncData ({
206
204
store: this .$store ,
207
205
route: this .$route
0 commit comments