dkokorev90 / vue-view

DEPRECATED, YOU SHOULD USE VUE-ROUTER WHICH IS THE OFFICIAL VUEJS' ROUTER
11 stars 0 forks source link

vue-view

Routing directive for Vue.js (v0.11), inspired by angular ui-router. This plugin uses for routing page.js. Based on v-component. You can use v-transition, keep-alive, wait-for, transition-mode.

All your routes you declare on the $root Vue object. You can use plain routes or states, like in ui-router.

Getting started

Vue.use(vueView);

* Put the `<div v-view></div>` in your main template
* Declare your routes on the `$root` VM of your app

## Usage

```html
<body>
    <div v-view></div>
</body>

vue-view extends the v-component.

plain routes

Pass routes option to the router.

var Vue = require('vue'),
    vueView = require('vue-view');

Vue.use(vueView);

var app = new Vue({
    el: '#demo',
    router: {
        routes: {
            '/': {
                component: 'home',
                beforeEnter: function(ctx, next) {
                    // do somthing
                    next();
                },
                // with name of function from methods
                onEnter: 'onEnter',
                beforeLeave: function(ctx) {
                    // do somthing
                }
            },
            '/user/:id': {
                component: 'user',
                data: {
                    type: 'full'
                }
            },
            // with webpack async loading
            '/async': {
                component: function(cb) {
                    require(['./views/async1/async1.js'], function(mod) {
                        cb(mod);
                    });
                }
            },
            // with webpack async loading and bundle loader
            '/async': {
                component: require('bundle!./views/async2/async2.js')
            },
            default: '/'
        },
        options: {
            hashbang: true
        }
    },
    methods: {
        onEnter: function(ctx) {
            // do somthing
        }
    },
    events: {
        'router:beforeEnter': function(to, from) {
        },
        'router:onEnter': function(to, from) {
        },
        'router:beforeLeave': function() {
        }
    }
});

With plain routes you can use new methods Vue.go(path) for pushState and Vue.redirect(path) for replaceState in your code.

states

Pass states option to the router.

var Vue = require('vue'),
    vueView = require('vue-view');

Vue.use(vueView);

var app = new Vue({
    el: '#demo',
    router: {
        states: {
            'home': {
                url: '/',
                component: 'home',
                beforeEnter: function(ctx, next) {
                    // do somthing
                    next();
                },
                // with name of function from methods
                onEnter: 'onEnter',
                beforeLeave: function(ctx) {
                    // do somthing
                }
            },
            'user': {
                url: '/user/:id',
                component: 'user',
                data: {
                    type: 'full'
                }
            },
            // with webpack async loading
            'async1': {
                url: '/async1',
                component: function(cb) {
                    require(['./views/async1/async1.js'], function(mod) {
                        cb(mod);
                    });
                }
            },
            // with webpack async loading and bundle loader
            'async2': {
                url: '/async2',
                component: require('bundle!./views/async2/async2.js')
            },
            default: 'home'
        },
        options: {
            activeClass: 'activeState', // optional, default: 'active'
            hashbang: true
        }
    },
    methods: {
        onEnter: function(ctx) {
            // do somthing
        }
    },
    events: {
        'router:beforeEnter': function(to, from) {
        },
        'router:onEnter': function(to, from) {
        },
        'router:beforeLeave': function() {
        }
    }
});

With states you can use new methods Vue.go(state, routeParams, queryParams) for pushState and Vue.redirect(state, routeParams, queryParams) for replaceState in your code. And directive v-go (like ui-sref in angular ui-router, see example below).

Vue.go and Vue.replace params:

If url regexp has not route params (/user, without :id) and you use Vue.go('user', { id: 2 }) then routeParams become a queryParams => '/user?id=2'.

Route properties

When you pass your routes to the $root, you can pass several properties:

// route (state 'home')
module.exports = {
    url: '/home',
    component: function(cb) {
        require(['./home.js'], function(mod) {
            cb(mod);
        });
    },
    wait: true
};

// VM
module.exports = {
    name: 'home',
    template: '<div>home</div>',
    compiled: function() {
        setTimeout(function() {
            this.$data.someAsyncData = someAsyncData;

            this.$emit('dataLoaded');
        }.bind(this), 200);
    }
};

Also routes or states option has default property. Pass default: <state> or default: <path>, depending on what you use.

Options

You can pass a options hash to the router – it is a page.js options, and own options:

Events

The router will emit events on your $root VM: router:beforeEnter, router:onEnter, router:beforeLeave.

Location context

When the router emits an events router:beforeEnter and router:onEnter, 2 parameters are passed: toLocation and fromLocation. Also these parameters are passed to ctx of beforeEnter and onEnter middleware. It is an object containing some properties:

$router

$router parameter passed to current VM $data with some properties:

$router has method isState(stateName), check if the current state is stateName, returns boolean.

this.$data.$router.isState('home')

v-go directive

Note: to use this directive you should use states

This directive adds href attribute with correct url to links, and adds click event listener (with Vue.go method inside) to another DOM elements (div, span etc.), so you can use it with all DOM elements. Also v-go adds active class or custom class from options.activeClass to current <a> element, which state is active.

Usage

======= Some ideas were taken from vue-route.