entry-server.js
1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import {createApp} from './app';
import createReport from 'report-error';
import {
SET_ENV,
} from 'store/yoho/types';
export default context => {
const reportError = createReport(context);
return new Promise((resolve, reject) => {
const {app, router, store} = createApp(context);
const {url} = context;
store.commit(SET_ENV, {context});
router.push(url);
router.onReady(() => {
const matched = router.getMatchedComponents();
if (matched.some(m => !m)) {
reportError(new Error('导航组件为空'));
router.push({name: 'error.500'});
return resolve(app);
}
if (!matched.length) {
return reject({code: 404, message: ''});
}
const asyncDataPromises = matched.map(({asyncData}) => {
try {
return asyncData && asyncData({store, router: router.currentRoute});
} catch (error) {
return Promise.reject(error);
}
}).filter(p => p);
Promise.all(asyncDataPromises)
.then(() => {
context.state = store.state;
return resolve(app);
}).catch(e => {
reportError(e);
if (e.code === 401) {
store.commit('SET_NEED_LOGIN', {needLogin: true});
}
context.state = store.state;
return resolve(app);
});
});
router.onError(e => {
reportError(e);
router.push({name: 'error.500'});
return resolve(app);
});
});
};