1 | import Vue from 'vue'
|
2 | import createDataStore from './create-data-store'
|
3 | import { setInitialData } from './utils'
|
4 |
|
5 | const isRouteComponent = (matched, current) => {
|
6 | let result
|
7 | for (const m of matched) {
|
8 | for (const key of Object.keys(m.instances)) {
|
9 | const instance = m.instances[key]
|
10 | if (instance === current) {
|
11 | result = true
|
12 | }
|
13 | }
|
14 | }
|
15 | return result
|
16 | }
|
17 |
|
18 | Vue.mixin({
|
19 | beforeCreate() {
|
20 | if (!this.$root.$options._isReamRoot) return
|
21 |
|
22 | this.$ream = this.$root
|
23 | this.$dataStore = this.$ream.$options.dataStore
|
24 | this.$isRouteComponent = isRouteComponent(this.$route.matched, this)
|
25 |
|
26 | setInitialData(this)
|
27 | },
|
28 | data() {
|
29 | return Object.assign({}, this.$initialData)
|
30 | }
|
31 | })
|
32 |
|
33 | const Root = {
|
34 | name: 'ReamRoot',
|
35 | render(h) {
|
36 | return h('router-view')
|
37 | }
|
38 | }
|
39 |
|
40 | const Error = {
|
41 | name: 'ReamError',
|
42 | functional: true,
|
43 | props: ['error'],
|
44 | render(
|
45 | h,
|
46 | {
|
47 | props: { error }
|
48 | }
|
49 | ) {
|
50 | return (
|
51 | <div>
|
52 | <h1>
|
53 | {error.code}: {error.message}
|
54 | </h1>
|
55 | {__DEV__ &&
|
56 | error.code === 404 &&
|
57 | error.errorPath === '/' && (
|
58 | <p>You must create pages/*.vue or export "router" in entry file!</p>
|
59 | )}
|
60 | </div>
|
61 | )
|
62 | }
|
63 | }
|
64 |
|
65 | export default ({ rootOptions, entry }, context) => {
|
66 | const { root = Root, error = Error } = entry
|
67 |
|
68 | const App = {
|
69 | dataStore: createDataStore(),
|
70 | data() {
|
71 | return {
|
72 | error: null
|
73 | }
|
74 | },
|
75 | render(h) {
|
76 | return h(
|
77 | 'div',
|
78 | {
|
79 | attrs: {
|
80 | id: '_ream'
|
81 | }
|
82 | },
|
83 | [
|
84 | this.actualError
|
85 | ? h(error, {
|
86 | props: {
|
87 | error: this.actualError
|
88 | }
|
89 | })
|
90 | : h(root)
|
91 | ]
|
92 | )
|
93 | },
|
94 | methods: {
|
95 | setError(error) {
|
96 | this.error = error
|
97 | }
|
98 | },
|
99 | computed: {
|
100 | actualError() {
|
101 | const error = context ? context.reamError : this.error
|
102 | if (error && error.errorPath) {
|
103 | return error.errorPath === this.$route.path ? error : null
|
104 | }
|
105 | return error
|
106 | }
|
107 | }
|
108 | }
|
109 |
|
110 | Object.assign(rootOptions, App)
|
111 | }
|
112 |
|
\ | No newline at end of file |