|
| 1 | +import Vue = require("vue"); |
| 2 | +import { ComponentOptions } from "vue"; |
| 3 | + |
| 4 | +import VueRouter = require("../index"); |
| 5 | +import { Route, RouteRecord, RedirectOption } from "../index"; |
| 6 | + |
| 7 | +Vue.use(VueRouter); |
| 8 | + |
| 9 | +const Home = { template: "<div>home</div>" }; |
| 10 | +const Foo = { template: "<div>foo</div>" }; |
| 11 | +const Bar = { template: "<div>bar</div>" }; |
| 12 | + |
| 13 | +const Hook: ComponentOptions<{ a: string } & Vue> = { |
| 14 | + template: "<div>hook</div>", |
| 15 | + |
| 16 | + beforeRouteEnter (route, redirect, next) { |
| 17 | + route.params; |
| 18 | + redirect("/"); |
| 19 | + next(vm => { |
| 20 | + vm.a = "done"; |
| 21 | + }); |
| 22 | + }, |
| 23 | + |
| 24 | + beforeRouteLeave (route, redirect, next) { |
| 25 | + route.params; |
| 26 | + redirect("/"); |
| 27 | + next(); |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +const router = new VueRouter({ |
| 32 | + mode: "history", |
| 33 | + base: "/", |
| 34 | + linkActiveClass: "active", |
| 35 | + scrollBehavior: (to, from, savedPosition) => { |
| 36 | + if (from.path === "/") { |
| 37 | + return { selector: "#app" }; |
| 38 | + } |
| 39 | + |
| 40 | + if (to.path === "/child") { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (savedPosition) { |
| 45 | + return savedPosition; |
| 46 | + } |
| 47 | + }, |
| 48 | + routes: [ |
| 49 | + { path: "/", name: "home", component: Home, children: [ |
| 50 | + { |
| 51 | + path: "child", |
| 52 | + components: { |
| 53 | + default: Foo, |
| 54 | + bar: Bar |
| 55 | + }, |
| 56 | + meta: { auth: true }, |
| 57 | + beforeEnter (route, redirect, next) { |
| 58 | + route.params; |
| 59 | + redirect({ name: "home" }); |
| 60 | + next(); |
| 61 | + } |
| 62 | + }, |
| 63 | + { |
| 64 | + path: "children", |
| 65 | + redirect: to => { |
| 66 | + to.params; |
| 67 | + return "/child"; |
| 68 | + } |
| 69 | + } |
| 70 | + ]}, |
| 71 | + { path: "/home", alias: "/" }, |
| 72 | + { path: "*", redirect: "/" } |
| 73 | + ] |
| 74 | +}); |
| 75 | + |
| 76 | +const App: Vue = router.app; |
| 77 | +const mode: string = router.mode; |
| 78 | + |
| 79 | +const route: Route = router.currentRoute; |
| 80 | +const path: string = route.path; |
| 81 | +const name: string | undefined = route.name; |
| 82 | +const hash: string = route.hash; |
| 83 | +const query: string = route.query["foo"]; |
| 84 | +const params: string = route.params["bar"]; |
| 85 | +const fullPath: string = route.fullPath; |
| 86 | +const redirectedFrom: string | undefined = route.redirectedFrom; |
| 87 | +const meta: any = route.meta; |
| 88 | +const matched: RouteRecord[] = route.matched; |
| 89 | + |
| 90 | +matched.forEach(m => { |
| 91 | + const path: string = m.path; |
| 92 | + const components: { |
| 93 | + [key: string]: ComponentOptions<Vue> | typeof Vue |
| 94 | + } = m.components; |
| 95 | + const instances: { [key: string]: Vue } = m.instances; |
| 96 | + const name: string | undefined = m.name; |
| 97 | + const parant: RouteRecord | undefined = m.parent; |
| 98 | + const redirect: RedirectOption | undefined = m.redirect; |
| 99 | +}); |
| 100 | + |
| 101 | +router.beforeEach((route, redirect, next) => { |
| 102 | + route.params; |
| 103 | + redirect("/"); |
| 104 | + next(); |
| 105 | +}); |
| 106 | + |
| 107 | +router.afterEach(route => { |
| 108 | + route.params; |
| 109 | +}); |
| 110 | + |
| 111 | +router.push({ |
| 112 | + path: "/", |
| 113 | + params: { |
| 114 | + foo: "foo" |
| 115 | + }, |
| 116 | + query: { |
| 117 | + bar: "bar" |
| 118 | + }, |
| 119 | + hash: "hash" |
| 120 | +}); |
| 121 | +router.replace({ name: "home" }); |
| 122 | + |
| 123 | +router.go(-1); |
| 124 | +router.back(); |
| 125 | +router.forward(); |
| 126 | + |
| 127 | +const Components: ComponentOptions<Vue> | typeof Vue = router.getMatchedComponentes(); |
| 128 | + |
| 129 | +const vm = new Vue({ |
| 130 | + router, |
| 131 | + template: ` |
| 132 | + <div id="app"> |
| 133 | + <h1>Basic</h1> |
| 134 | + <ul> |
| 135 | + <li><router-link to="/">/</router-link></li> |
| 136 | + <li><router-link to="/foo">/foo</router-link></li> |
| 137 | + <li><router-link to="/bar">/bar</router-link></li> |
| 138 | + </ul> |
| 139 | + <router-view class="view"></router-view> |
| 140 | + </div> |
| 141 | + ` |
| 142 | +}).$mount("#app"); |
| 143 | + |
| 144 | +vm.$router.push("/"); |
| 145 | +vm.$route.params; |
0 commit comments