diff --git a/vue-router/vue-router-test.ts b/vue-router/vue-router-test.ts new file mode 100644 index 000000000..4456b0a47 --- /dev/null +++ b/vue-router/vue-router-test.ts @@ -0,0 +1,113 @@ +/// + +Vue.use(VueRouter); + +namespace TestBasic { + "use strict"; + + var Foo = Vue.extend({ + template: "

This is foo!

", + route: { + canActivate(transition: vuerouter.Transition) { + return true; + } + } + }); + + var Bar = Vue.extend({ + template: "

This is bar!

" + }); + + var App = Vue.extend({}); + + var router = new VueRouter(); + + router.map({ + "/foo": { + component: Foo + }, + "/bar": { + component: Bar + } + }); + + router.start(App, "#app"); +} + +namespace TestAdvanced { + "use strict"; + + namespace App { + + export class App { + authenticating: boolean; + + data() { + return { + authenticating: false + }; + } + } + } + + namespace Inbox { + export class Index { + static route: vuerouter.TransitionHook = { + canActivate: function(transition) { + var n: number = transition.to.params.id; + transition.next(); + }, + activate: function() { + return new Promise((resolve) => { + resolve(); + }); + }, + deactivate: function({next}) { + next(); + } + }; + } + } + + namespace RouteConfig { + export function configRouter(router: vuerouter.Router) { + router.map({ + "/about": { + component: {}, + auth: false + }, + "*": { + component: {} + } + }); + + router.redirect({ + "/info": "/about", + "/hello/:userId": "/user/:userId" + }); + + router.beforeEach((transition) => { + if (transition.to.path === "/forbidden") { + router.app.authenticating = true; + setTimeout(() => { + router.app.authenticating = false; + alert(""); + transition.abort(); + }, 3000); + } else { + transition.next(); + } + }); + } + } + + import configRouter = RouteConfig.configRouter; + + const router = new VueRouter({ + history: true, + saveScrollPosition: true + }); + + configRouter(router); + router.start(App, "#app"); +} diff --git a/vue-router/vue-router.d.ts b/vue-router/vue-router.d.ts new file mode 100644 index 000000000..729e8f6ef --- /dev/null +++ b/vue-router/vue-router.d.ts @@ -0,0 +1,87 @@ +// Type definitions for vue-router 0.7.7 +// Project: https://github.com/vuejs/vue-router +// Definitions by: kaorun343 +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// + +declare namespace vuerouter { + + interface Transition { + from: $route; + to: $route; + next(data?: any): void; + abort(reason?: any): void; + redirect(path: string): void; + } + + interface RouterOption { + hashbang?: boolean; + history?: boolean; + abstract?: boolean; + root?: string; + linkActiveClass?: string; + saveScrollPosition?: boolean; + transitionOnLoad?: boolean; + suppressTransitionError?: boolean; + } + + interface RouterStatic { + new (option?: RouterOption): Router; + } + + interface RouteMapObject { + component: any; + subRoutes?: { [key: string]: RouteMapObject }; + [key: string]: any; + } + + interface Router { + + app: RootVueApp; + mode: string; + + start(App: any, el: string | Element): void; + stop(): void; + map(routeMap: { [path: string]: RouteMapObject }): void; + on(path: string, config: Object): void; + go(path: string | Object): void; + replace(path: string): void; + redirect(redirectMap: Object): void; + alias(aliasMap: Object): void; + beforeEach(hook: (transition: Transition) => any): void; + afterEach(hook: (transition: Transition) => any): void; + } + + interface $route { + path: string; + params: Params; + query: Query; + router: Router; + matched: string[]; + name: string; + [key: string]: any; + } + + interface TransitionHook { + data?(transition?: Transition): Thenable | void; + activate?(transition?: Transition): Thenable | void; + deactivate?(transition?: Transition): Thenable | void; + canActivate?(transition?: Transition): Thenable | boolean | void; + canDeactivate?(transition?: Transition): Thenable | boolean | void; + canReuse?: boolean | ((transition: Transition) => boolean); + } +} + +declare namespace vuejs { + interface ComponentOption { + route?: vuerouter.TransitionHook; + } +} + +declare var VueRouter: vuerouter.RouterStatic; + +declare module "vue-router" { + export = VueRouter; +}