|
楼主 |
发表于 2018-4-25 09:40:41
|
显示全部楼层
第一步:生成一个带路由的项目
cd C:\xiangmu ====== ng new angular-routing-demo --routing
【angular-routing-demo居然是项目名:讲课老师估计是疯了
这个项目比别的项目多了一个路由配置文件app/app-routing.module.ts
并且在app.module.ts文件里已经引入了路由模块,并且已经注入】
【】
第二步:创建组件
=ng g component components/模块
第三步:在app-routing.module.ts文件里
import { HemoComponent } from './components/hemo/hemo.component
import { NewsComponent } from './components/news/news.component
import { UserComponent } from './components/user/user.component
第四步:配置路由
const routes: Routes = [
{
path:'hemo',
component:HemoComponent, //确定模块
children:[] //配置子组件的地方
},
{
path:'news',
component:NewsComponent, //确定模块
children:[] //配置子组件的地方
}
]
第五步:增加导航:
导航跳转的a标签不是href 而是routerLink="模块"
例如: <li><a routerLink="news" >新闻</a></li>
第六步:
第一种玩法 6.1设置空路由【即在没有链接时候默认显示的路由值】
{
path:"",
redirectTo:"hemo",
pathMatch:"full"
}
第二种玩法 6.2//设置匹配路由出错或者匹配不到路有时候加载的组件 可以替代上面的空路由
{
path:'**', //表示任意路由【去掉404了】
// component:HemoComponent, //确定模块
redirectTo:"hemo"
//redirectTo这个与component不同,这是默认跳转路由URL改变 ,comp是加载模块不改变url
}
|
|