Using the Angular 2 router requires defining routes, passing them in to the RouterModule.forRoot and then importing the configured RouterModule into your main App Module.

Use the Wiki Search as example project.

Create a HomeComponent to contain every other components. Then in out app.component.html, we can just use router outlet to render the application:

app.component.ts:

import { Component } from '@angular/core';

@Component({
selector: 'app-root',
templateUrl: '<router-outlet></router-outlet>',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
}

app.routes.ts:

import {HomeComponent} from "./home/home.component";
import {RouterModule} from "@angular/router";
const routes = [
{path: '', component: HomeComponent}
]; export default RouterModule.forRoot(routes);

The defualt component is HomeComponent. Export this config to the app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import {HttpModule, JsonpModule} from '@angular/http'; import { AppComponent } from './app.component';
import { SearchBarComponent } from './home/search-bar/search-bar.component';
import { ResultListComponent } from './home/result-list/result-list.component';
import {SharedServiceModule} from "./home/shared/index";
import {CommonModule} from "@angular/common";
import {API_URL} from "./home/shared/constance.service";
import { MdButtonModule } from '@angular2-material/button';
import {MdInputModule} from "@angular2-material/input";
import {MdListModule} from "@angular2-material/list";
import {MdToolbarModule} from "@angular2-material/toolbar";
import { HomeComponent } from './home/home.component'; import appRoutes from './app.routes'; @NgModule({
declarations: [
AppComponent,
SearchBarComponent,
ResultListComponent,
HomeComponent
],
imports: [
MdButtonModule.forRoot(),
MdInputModule.forRoot(),
MdToolbarModule.forRoot(),
MdListModule.forRoot(),
appRoutes,
BrowserModule,
FormsModule,
CommonModule,
HttpModule,
JsonpModule,
SharedServiceModule.forRoot()
],
providers: [
{
provide: API_URL,
useValue: `https://en.wikipedia.org/w/api.php?callback=JSONP_CALLBACK`
}
],
bootstrap: [AppComponent]
})
export class AppModule { }

Github

[Angular 2 Router] Configure Your First Angular 2 Route的更多相关文章

  1. [Angular2 Router] Configure Auxiliary Routes in the Angular 2 Router - What is the Difference Towards a Primary Route?

    In this tutorial we are going to learn how we can can configure redirects in the angular 2 router co ...

  2. angular当router使用userhash:false时路由404问题

    angular当router使用userhash:false时路由404问题 安装iis urlrewrite2.0组件 在根目录下创建 Web.config <configuration> ...

  3. [从 0 开始的 Angular 生活]No.38 实现一个 Angular Router 切换组件页面(一)

    前言 今天是进入公司的第三天,为了能尽快投入项目与成为团队可用的战力,我正在努力啃官方文档学习 Angular 的知识,所以这一篇文章主要是记录我如何阅读官方文档后,实现这个非常基本的.带导航的网页应 ...

  4. 【Angular专题】——(1)Angular,孤傲的变革者

    目录 一. 漫谈Angular 二. 如果你还在使用Angularjs 三. 我计划这样学习Angular技术栈 一. 漫谈Angular Angular,来自Google的前端SPA框架,与Reac ...

  5. [Angular] Use Angular components in AngularJS applications with Angular Elements

    When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular El ...

  6. Angular 从入坑到挖坑 - Angular 使用入门

    一.Overview angular 入坑记录的笔记第一篇,完成开发环境的搭建,以及如何通过 angular cli 来创建第一个 angular 应用.入坑一个多星期,通过学习官方文档以及手摸手的按 ...

  7. Angular项目构建指南 - 不再为angular构建而犹豫不决(转)

    如果你不知道什么是Angular或者根本没听说过,那么我接下来所说的对你来说毫无益处,不过如果你打算以后会接触Angular或者干脆要涨涨姿势~读下去还是有点用的. Angular和它之前所出现的其余 ...

  8. [Angular2 Router] Use Params from Angular 2 Routes Inside of Components

    Angular 2’s ActivatedRoute allows you to get the details of the current route into your components. ...

  9. angular ui.router 路由传参数

    angular已经用了一段时间了,最近在做路由,做一下笔记. 路由跳转的时候进行穿参 ui.router方式 <a ui-sref="edit({id:5})"> 编辑 ...

随机推荐

  1. 游戏BI,起步了。

    思索许久,终于决定自己的发展将会是游戏的BI. 即说即做,本文是我未来BI工作的开端. 传统的游戏BI,只是将运营的工作数据化,流量的变现指标化.和网站类似,无外乎用户导入,流失,保有,付费,回访等等 ...

  2. 使用源码编译wxpython-基于python2.7

    1.前言 本文主要讲述在linux环境下进行编译wxpython,在windows下面安装wxpython很简单,只要下载,然后直接执行exe文件,下一步下一步即可安装,在linux下面,则具有很多步 ...

  3. bzoj 1806 [Ioi2007]Miners 矿工配餐(DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1806 [题意] 给定一个权在1..3内的序列,在保持相对位置不变的情况下拆分成两个序列 ...

  4. Python线程

    原文出处: AstralWind 1. 线程基础 1.1. 线程状态 线程有5种状态,状态转换的过程如下图所示: 1.2. 线程同步(锁) 多线程的优势在于可以同时运行多个任务(至少感觉起来是这样). ...

  5. Swift 可选值(Optional Values)介绍

    Optional的定义 Optional也是Objective-C没有的数据类型,是苹果引入到Swift语言中的全新类型,它的特点就和它的名字一样:可以有值,也可以没有值,当它没有值时,就是nil.此 ...

  6. JavaScript面向对象简介

    JavaScript面向对象简介 @(编程) [TOC] 1. 命名空间 命名空间是一个容器,它允许开发人员在一个独特的,特定于应用程序的名称下捆绑所有的功能. 在JavaScript中,命名空间只是 ...

  7. OWA修改密码注意事项

    Exchange搭建参考 http://yuelei.blog.51cto.com/202879/76302 http://543925535.blog.51cto.com/639838/d-37/p ...

  8. struts2 下载时报java.lang.IllegalStateException

    -----------------------------------------struts2 下载时报java.lang.IllegalStateException---------------- ...

  9. 69道java Spring面试题和答案

    http://www.jfox.info/69-dao-java-spring-mian-shi-ti-he-da-an 目录 Spring 概述 依赖注入 Spring beans Spring注解 ...

  10. SiteMesh学习入门

    http://www.java3z.com/cwbwebhome/article/article2/2962.html?id=1668 demo下载 简介:     sitemesh应用Decorat ...