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. 关于python中字典的一些总结

    1. 获取字典中的值,但是无异常 当在字典中取值的时候,可以使用如下两种方式: >>> d = {'name':'kel'} >>> d {'name': 'kel ...

  2. uoj #58. 【WC2013】糖果公园(树上莫队算法+修改操作)

    [题目链接] http://uoj.ac/problem/58 [题意] 有一棵树,结点有自己的颜色,若干询问:u,v路径上的获益,并提供修改颜色的操作. 其中获益定义为Vc*W1+Vc*W2+…+V ...

  3. spark 连接 mysql 数据库

    在所有master和slave上也要在spark/conf/spark-conf.sh里面设置driver的classpath,解决编译找不到driver的问题 http://www.iteblog. ...

  4. [转]对Android开发者有益的40条优化建议

    下面是开始Android编程的好方法: 找一些与你想做事情类似的代码 调整它,尝试让它做你像做的事情 经历问题 使用StackOverflow解决问题 对每个你像添加的特征重复上述过程.这种方法能够激 ...

  5. JavaEE5 Tutorial_Jsp,EL

      Jsp的各种元素在转化为servlet时处理是不一样的:指令,控制web容器如何处理页面脚本,被插入到生成的servlet里EL表达式,作为参数传递到解析器get/set Property,变成方 ...

  6. sphinx下的max_matches取值对SetLimits的影响

    使用PHP在客户端执行 $s -> SetLimits(0, 15, 1200); 传递的第三个参数,是服务器端设定当前查询的结果集大小为1200,但是运行结果,确实$s最终查询得到的结果为空值 ...

  7. INTEL XDK 真机调试

    需要安装 1.google服务框架 2.google play 3.app preview

  8. xml velocity模板

    . <?xml version="1.0" encoding="GBK"?> <PACKET type="REQUEST" ...

  9. HDU2955Robberies(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 题目是说一小偷偷东西,第i个物品的价值是M[i],被抓的概率是p[i],现在要使得在被抓的概率在P以下时的 ...

  10. HNC-全局联想脉络

    全局联想脉络—语义块 和句类 一个语义块包含 核心部分 和说明部分. 语义块分为: 主语义块:有 4 种:特征E.作用者A.对象B.内容C 辅语义块:有 7 种:条件.手段.工具.途径.参照.因.果. ...