美文网首页
vue-router

vue-router

作者: DragonRat | 来源:发表于2018-09-06 13:33 被阅读0次

    作者:烨竹

    参考:https://router.vuejs.org/zh/

    简介:引用官方文档

    Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌。包含的功能有:

    • 嵌套的路由/视图表
    • 模块化的、基于组件的路由配置
    • 路由参数、查询、通配符
    • 基于 Vue.js 过渡系统的视图过渡效果
    • 细粒度的导航控制
    • 带有自动激活的 CSS class 的链接
    • HTML5 历史模式或 hash 模式,在 IE9 中自动降级
    • 自定义的滚动条行为

    基础用法和理解

    相关用例:

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    
    Vue.use(VueRouter)
    
    import App from './components/App'
    import Home from './components/Home'
    import Hello from './components/Hello'
    import UsersIndex from './components/UsersIndex'
    
    
    const router = new VueRouter({
        mode: 'history',
        routes: [
            {
                path: '/',
                name: 'home',
                component: Home
                
            },
            {
                path: '/hello',
                name: 'hello',
                component: Hello,
            },
            {
                path: '/users',
                name: 'users.index',
                component: UsersIndex,
            },    
        ],
    
    });
    
    const app = new Vue({
        el: '#app',
        components: { App },
        router,
    });
    
    <template>
        <div class="users">
            <div v-if="error" class="error">
                <p>{{ error }}</p>
            </div>
    
            <ul v-if="users">
                <li v-for="{ id, name, email } in users">
                    <strong>Name:</strong> {{ name }},
                    <strong>Email:</strong> {{ email }}
                </li>
            </ul>
    
            <div class="pagination">
                <button :disabled="! prevPage" @click.prevent="goToPrev">上一页</button>
                {{ paginatonCount }}
                <button :disabled="! nextPage" @click.prevent="goToNext">下一页</button>
            </div>
        </div>
    </template>
    <script>
    import axios from 'axios';
    
    const getUsers = (page, callback) => {
        const params = { page };
    
        axios
            .get('/api/users', { params })
            .then(response => {
                callback(null, response.data);
            }).catch(error => {
                callback(error, error.response.data);
            });
    };
    
    export default {
        data() {
            return {
                users: null,
                meta: null,
                links: {
                    first: null,
                    last: null,
                    next: null,
                    prev: null,
                },
                error: null,
            };
        },
        computed: {
            nextPage() {
                if (! this.meta || this.meta.current_page === this.meta.last_page) {
                    return;
                }
    
                return this.meta.current_page + 1;
            },
            prevPage() {
                if (! this.meta || this.meta.current_page === 1) {
                    return;
                }
    
                return this.meta.current_page - 1;
            },
            paginatonCount() {
                if (! this.meta) {
                    return;
                }
    
                const { current_page, last_page } = this.meta;
    
                return `${current_page} *** ${last_page}`;
            },
        },
        beforeRouteEnter (to, from, next) {
            getUsers(to.query.page, (err, data) => {
                next(vm => vm.setData(err, data));
            });
        },
        // when route changes and this component is already rendered,
        // the logic will be slightly different.
        beforeRouteUpdate (to, from, next) {
            this.users = this.links = this.meta = null
            getUsers(to.query.page, (err, data) => {
                this.setData(err, data);
                next();
            });
        },
        methods: {
            goToNext() {
                this.$router.push({
                    query: {
                        page: this.nextPage,
                    },
                });
            },
            goToPrev() {
                this.$router.push({
                    name: 'users.index',
                    query: {
                        page: this.prevPage,
                    }
                });
            },
            setData(err, { data: users, links, meta }) {
                if (err) {
                    this.error = err.toString();
                } else {
                    this.users = users;
                    this.links = links;
                    this.meta = meta;
                }
            },
        }
    }
    </script>
    

    相关文章

      网友评论

          本文标题:vue-router

          本文链接:https://www.haomeiwen.com/subject/ldcxgftx.html