zoukankan      html  css  js  c++  java
  • 前端路由的简单使用

    function Router() {
    this.routes = {}
    this.currentHash = ''
    }

    var noop = function () {}

    // 路由注册
    Router.prototype.route = function (hash, cb) {
    this.currentHash = hash
    this.routes[this.currentHash] = cb || noop
    }

    // 路由刷新
    Router.prototype.refresh = function () {
    let hash = location.hash || '#position'
    this.currentHash = hash
    this.routes[this.currentHash]()
    this.switchTabbar()
    }

    // tabbar switch
    Router.prototype.switchTabbar = function () {
    let hashs = ['#position', '#search', '#profile']
    let index = hashs.indexOf(this.currentHash)
    $('nav li')
    .eq(index)
    .addClass('active')
    .siblings()
    .removeClass('active')
    }

    // 路由切换监听
    Router.prototype.init = function () {
    window.addEventListener('load', this.refresh.bind(this))
    window.addEventListener('hashchange', this.refresh.bind(this))
    }

    export default Router
     
     
     
     
    import Router from './utils/router'
    import homeController from './controllers/home'
    import positionController from './controllers/position'
    import searchController from './controllers/search'
    import profileController from './controllers/profile'
    // shaojun

    homeController.render()

    const router = new Router()
    router.init()
    router.route('#position', positionController.render)
    router.route('#search', searchController.render)
    router.route('#profile', profileController.render)
  • 相关阅读:
    推荐一本书 改善你的视力:跟眼镜说再见
    Gentoo中gcc4.1.2到gcc4.3.2的升级
    msbuild学习的一些相关链接
    SqlServer 2005安装问题
    Gentoo linux中安装php5运行环境
    sql 时间函数(全)
    asp.net中的对话框
    win7 资源管理器指向我的电脑
    C/C++ 位操作 总结
    【转】Java字节序转换
  • 原文地址:https://www.cnblogs.com/carolavie/p/9801131.html
Copyright © 2011-2022 走看看