Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* @flow */
  2. import { install } from './install'
  3. import { START } from './util/route'
  4. import { assert } from './util/warn'
  5. import { inBrowser } from './util/dom'
  6. import { cleanPath } from './util/path'
  7. import { createMatcher } from './create-matcher'
  8. import { normalizeLocation } from './util/location'
  9. import { supportsPushState } from './util/push-state'
  10. import { HashHistory } from './history/hash'
  11. import { HTML5History } from './history/html5'
  12. import { AbstractHistory } from './history/abstract'
  13. import type { Matcher } from './create-matcher'
  14. export default class VueRouter {
  15. static install: () => void;
  16. static version: string;
  17. app: any;
  18. apps: Array<any>;
  19. ready: boolean;
  20. readyCbs: Array<Function>;
  21. options: RouterOptions;
  22. mode: string;
  23. history: HashHistory | HTML5History | AbstractHistory;
  24. matcher: Matcher;
  25. fallback: boolean;
  26. beforeHooks: Array<?NavigationGuard>;
  27. resolveHooks: Array<?NavigationGuard>;
  28. afterHooks: Array<?AfterNavigationHook>;
  29. constructor (options: RouterOptions = {}) {
  30. this.app = null
  31. this.apps = []
  32. this.options = options
  33. this.beforeHooks = []
  34. this.resolveHooks = []
  35. this.afterHooks = []
  36. this.matcher = createMatcher(options.routes || [], this)
  37. let mode = options.mode || 'hash'
  38. this.fallback = mode === 'history' && !supportsPushState && options.fallback !== false
  39. if (this.fallback) {
  40. mode = 'hash'
  41. }
  42. if (!inBrowser) {
  43. mode = 'abstract'
  44. }
  45. this.mode = mode
  46. switch (mode) {
  47. case 'history':
  48. this.history = new HTML5History(this, options.base)
  49. break
  50. case 'hash':
  51. this.history = new HashHistory(this, options.base, this.fallback)
  52. break
  53. case 'abstract':
  54. this.history = new AbstractHistory(this, options.base)
  55. break
  56. default:
  57. if (process.env.NODE_ENV !== 'production') {
  58. assert(false, `invalid mode: ${mode}`)
  59. }
  60. }
  61. }
  62. match (
  63. raw: RawLocation,
  64. current?: Route,
  65. redirectedFrom?: Location
  66. ): Route {
  67. return this.matcher.match(raw, current, redirectedFrom)
  68. }
  69. get currentRoute (): ?Route {
  70. return this.history && this.history.current
  71. }
  72. init (app: any /* Vue component instance */) {
  73. process.env.NODE_ENV !== 'production' && assert(
  74. install.installed,
  75. `not installed. Make sure to call \`Vue.use(VueRouter)\` ` +
  76. `before creating root instance.`
  77. )
  78. this.apps.push(app)
  79. // main app already initialized.
  80. if (this.app) {
  81. return
  82. }
  83. this.app = app
  84. const history = this.history
  85. if (history instanceof HTML5History) {
  86. history.transitionTo(history.getCurrentLocation())
  87. } else if (history instanceof HashHistory) {
  88. const setupHashListener = () => {
  89. history.setupListeners()
  90. }
  91. history.transitionTo(
  92. history.getCurrentLocation(),
  93. setupHashListener,
  94. setupHashListener
  95. )
  96. }
  97. history.listen(route => {
  98. this.apps.forEach((app) => {
  99. app._route = route
  100. })
  101. })
  102. }
  103. beforeEach (fn: Function): Function {
  104. return registerHook(this.beforeHooks, fn)
  105. }
  106. beforeResolve (fn: Function): Function {
  107. return registerHook(this.resolveHooks, fn)
  108. }
  109. afterEach (fn: Function): Function {
  110. return registerHook(this.afterHooks, fn)
  111. }
  112. onReady (cb: Function, errorCb?: Function) {
  113. this.history.onReady(cb, errorCb)
  114. }
  115. onError (errorCb: Function) {
  116. this.history.onError(errorCb)
  117. }
  118. push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
  119. this.history.push(location, onComplete, onAbort)
  120. }
  121. replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
  122. this.history.replace(location, onComplete, onAbort)
  123. }
  124. go (n: number) {
  125. this.history.go(n)
  126. }
  127. back () {
  128. this.go(-1)
  129. }
  130. forward () {
  131. this.go(1)
  132. }
  133. getMatchedComponents (to?: RawLocation | Route): Array<any> {
  134. const route: any = to
  135. ? to.matched
  136. ? to
  137. : this.resolve(to).route
  138. : this.currentRoute
  139. if (!route) {
  140. return []
  141. }
  142. return [].concat.apply([], route.matched.map(m => {
  143. return Object.keys(m.components).map(key => {
  144. return m.components[key]
  145. })
  146. }))
  147. }
  148. resolve (
  149. to: RawLocation,
  150. current?: Route,
  151. append?: boolean
  152. ): {
  153. location: Location,
  154. route: Route,
  155. href: string,
  156. // for backwards compat
  157. normalizedTo: Location,
  158. resolved: Route
  159. } {
  160. const location = normalizeLocation(
  161. to,
  162. current || this.history.current,
  163. append,
  164. this
  165. )
  166. const route = this.match(location, current)
  167. const fullPath = route.redirectedFrom || route.fullPath
  168. const base = this.history.base
  169. const href = createHref(base, fullPath, this.mode)
  170. return {
  171. location,
  172. route,
  173. href,
  174. // for backwards compat
  175. normalizedTo: location,
  176. resolved: route
  177. }
  178. }
  179. addRoutes (routes: Array<RouteConfig>) {
  180. this.matcher.addRoutes(routes)
  181. if (this.history.current !== START) {
  182. this.history.transitionTo(this.history.getCurrentLocation())
  183. }
  184. }
  185. }
  186. function registerHook (list: Array<any>, fn: Function): Function {
  187. list.push(fn)
  188. return () => {
  189. const i = list.indexOf(fn)
  190. if (i > -1) list.splice(i, 1)
  191. }
  192. }
  193. function createHref (base: string, fullPath: string, mode) {
  194. var path = mode === 'hash' ? '#' + fullPath : fullPath
  195. return base ? cleanPath(base + '/' + path) : path
  196. }
  197. VueRouter.install = install
  198. VueRouter.version = '__VERSION__'
  199. if (inBrowser && window.Vue) {
  200. window.Vue.use(VueRouter)
  201. }