GithubHelp home page GithubHelp logo

router's Introduction

Simple Router

A very simple router, just for the demo of weui

预览

image

https://progrape.github.io/router

使用

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>example</title>
</head>
<body>
    <div id="container"></div>
    <script src="path/to/router.js"></script>
</body>
</html>

JavaScript

var router = new Router({
    container: '#container'
});
var home = {
    url: '/',
    className: 'home',
    render: function (){
        return '<h1>home</h1>';
    }
};
var post = {
    url: '/post/:id',
    className: 'post',
    render: function (){
        var id = this.params.id;
        return '<h1>post</h1>';
    }
};
router.push(home).push(post).setDefault('/').init();

运行示例

git clone https://github.com/progrape/router
cd router
npm install
npm start

API

Router([option])

参数 option 是可选的,下面是该参数可选的属性。

属性 类型 默认值 描述
container String '#container' container 容器的选择器
enter String 'enter' 该页面出现时添加的类名,enterTimeout 为 0 时会被忽略
enterTimeout Number 0 在这个时间之后移除添加的 enter 类名
leave String 'leave' 该页面离开时添加的类名,lieaveTimeout 为 0 时会被忽略
leaveTimeout Number 0 在这个时间之后移除该页面的 DOM

实例方法

以下方法执行完毕后均返回实例本身。

push(route)

添加路由页面的配置。下面是 route 参数的属性。

属性 类型 描述
url String / 开头的 url,会体现在 hash,支持参数,如:/user/:userId/post/:posdId
className String 可选,该页面可以添加的额外类名,以便控制该页面下的样式
render function 页面渲染方法,该方法返回的字符串,将作为该页面的 HTML 内容
setDefault(url)

设置页面启动时默认跳转的 url。

init()

启动页面,在调用完 pushsetDefault 方法后调用,主要完成 hashchange 的事件监听和跳转默认页面的工作。

License

The MIT License(http://opensource.org/licenses/MIT)

请自由地享受和参与开源

router's People

Contributors

progrape avatar

Watchers

50066 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.