惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Troy Hunt's Blog
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
B
Blog RSS Feed
S
Securelist
H
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Secure Thoughts
Spread Privacy
Spread Privacy
C
Check Point Blog
WordPress大学
WordPress大学
AWS News Blog
AWS News Blog
L
Lohrmann on Cybersecurity
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Privacy International News Feed
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
Blog — PlanetScale
Blog — PlanetScale
PCI Perspectives
PCI Perspectives
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Cyber Attacks, Cyber Crime and Cyber Security
Google Online Security Blog
Google Online Security Blog
The Hacker News
The Hacker News
宝玉的分享
宝玉的分享
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cisco Blogs
Last Week in AI
Last Week in AI
Webroot Blog
Webroot Blog
GbyAI
GbyAI
I
InfoQ
罗磊的独立博客
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
Latest news
Latest news
P
Palo Alto Networks Blog
博客园 - Franky
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Security Affairs
H
Hacker News: Front Page
P
Privacy & Cybersecurity Law Blog
小众软件
小众软件
The Register - Security
The Register - Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
V
Vulnerabilities – Threatpost
人人都是产品经理
人人都是产品经理
博客园_首页
aimingoo的专栏
aimingoo的专栏

博客园 - 【当耐特】

苹果 icloud 把我 ipad min 所有照片丢失 Wechart 饼图 - 【当耐特】 - 博客园 【开源】Skatch 正式发布 - 极速渲染抽象派草图 - 【当耐特】 【开源】小程序、小游戏和Web运动引擎 to2to 发布 - 【当耐特】 【开源】微信小程序、小游戏以及 Web 通用 Canvas 渲染引擎 - Cax - 【当耐特】 【开源】Qone 正式发布,使 javascript 支持 .NET LINQ - 【当耐特】 你不容错过的 腾讯 AlloyTeam Web 前端大会 看点完全剖析 腾讯 AlloyCrop 1.0 发布 - 【当耐特】 50行代码实现的高性能动画定时器 raf-interval - 【当耐特】 - 博客园 QQ日迹Omi实战开发,从0到1 - 【当耐特】 - 博客园 腾讯AlloyTeam正式发布pasition - 制作酷炫Path过渡动画 - 【当耐特】 腾讯AlloyTeam正式发布omi-cli脚手架 - 创建网站无需任何配置 - 【当耐特】 Omi树组件omi-tree编写指南 - 【当耐特】 - 博客园 腾讯AlloyTeam正式发布Canvas魔幻线条 - curvejs - 【当耐特】 Omi官方插件系列 - omi-transform介绍 - 【当耐特】 Omi架构与React Fiber - 【当耐特】 - 博客园 Omi框架Store体系的前世今生 - 【当耐特】 - 博客园 omi-cli新版发布-升级webpack2和支持sass生成组件局部CSS - 【当耐特】 - 博客园 Omi应用md2site-0.5.0发布-支持动态markdown拉取解析 - 【当耐特】 - 博客园
Omi新成员omi-router正式发布 - 【当耐特】 - 博客园
【当耐特】 · 2017-03-31 · via 博客园 - 【当耐特】

2017-03-31 09:26  【当耐特】  阅读(1371)  评论()    收藏  举报

原文链接-https://github.com/AlloyTeam/omi/blob/master/tutorial

omi-router

omi-routerOmi框架专属的router插件,文件尺寸轻量,使用简便,功能强大。用于Omi制作Web单页应用的首选解决方案。

单页的好处也是非常明显:

  • 无刷新加载页面内容
  • 无刷新前进和后退页面
  • 路由中的某个链接的传播和分享(别人看到的和你一样的状态)
  • 转场动画(a标签跳转不仅要白屏,而且没有转场动画)
  • 资源复用(单页中的许多资源一定是可以共用的,最典型的比如omi.js,如果不是单页的话,你需要加载多次)

好了,好处这么多,看看怎么安装使用吧~~

安装

CDN

可以直接通过Unpkg.com下载或引用cdn: https://unpkg.com/omi-router/dist/omi-router.js

<script src="https://unpkg.com/omi/dist/omi.js"></script>
<script src="https://unpkg.com/omi-router/dist/omi-router.js"></script>

NPM

npm install omi-router
import Omi from 'omi'
import OmiRouter from 'omi-router'

如果使用全局的 script 标签,则无须如此import便可使用。

开始

import Omi from 'omi'
import OmiRouter from 'omi-router'

import Home from './home.js'
import About from './about.js'
import User from './user.js'
import UserList from './user-list.js'

class App extends Omi.Component {
    install() {
        OmiRouter.init({
            routes: [
                {path: '/', component: Home},
                {path: '/about', component: About},
                {path: '/user-list', component: UserList},
                {path: '/user/:name/category/:category', component: User}
            ],
            renderTo: "#view",
            defaultRoute: '/'
        })
    }

    render() {
        return  `
        <div>
            <ul>
                <li><a omi-router to="/" >Home</a></li>
                <li><a omi-router to="/about" >About</a></li>
                <li><a omi-router to="/user-list" >UserList</a></li>
            </ul>
            <div id="view"> </div>
        </div>
        `
    }
}

Omi.render(new App(),"#__omi")

这里详细说下 OmiRouter.init 传递的配置参数的意义:

参数名 意义 是否必须
routes 路由配置。其中每一项中的path代表匹配规则,component代表渲染的组件 必须
renderTo 组件渲染的容器 必须
defaultRoute 如果第一次打开页面没携带hash,默认使用的地址 必须

再看下UserList:

import Omi from 'omi';

class UserList extends Omi.Component {

    render() {
        return  `
      	 <ul>
      	    <li><a omi-router to="/user/yanagao/category/js" >yanagao</a></li>
            <li><a omi-router to="/user/vorshen/category/html" >vorshen</a></li>
            <li><a omi-router to="/user/dntzhang/category/css" >dntzhang</a></li>
        </ul>
  		`;
    }
}

Omi.tag('UserList',UserList)

export default  UserList

动态匹配

模式 匹配路径 $route.params
/user/:name /user/dntzhang { username: 'dntzhang' }
/user/:name/category/:category /user/dntzhang/category/js { username: 'dntzhang', category: js }

注意: $route 会被挂载在$store下,$store会在根组件中注入,在组件树中的任何组件都可以通过 this.$store.$route.params 访问hash传递的数据。

接着上面例子

import Omi from 'omi'

class User extends Omi.Component {

    beforeRender(){
        let params =  this.$store.$route.params
        this.data.name = params.name
        this.data.category = params.category
        this.info = this.queryInfo(this.data.name)
        this.data.age = this.info.age
        this.data.sex = this.info.sex
    }

    queryInfo(name) {
        this.mockData = {
            'yanagao': {age: 18, sex: 'female'},
            'vorshen': {age: 20, sex: 'male'},
            'dntzhang': {age: 22, sex: 'male'}
        }
        return this.mockData[name]
    }

    back(){
        history.back()
    }

    render() {
        return  `
      	<div >
      	    <button onclick="back">back</button>
      	    <ul>
      	        <li>name:{{name}}</li>
      	        <li>age:{{age}}</li>
      	        <li>sex:{{sex}}</li>
      	        <li>category:{{category}}</li>
      	    </ul>
      	</div>
  		`
    }
}


Omi.tag('User',User)

export default  User

上面使用了beforeRender进行store到data的转换,beforeRender是生命周期的一部分。且看下面这张图:

beforeRender

注意:除了在constructor中不能读取到 this.$store, 在声明周期的任何其他函数中都能读取到 this.$store,非常便捷。

地址

相关