亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

angular路由跳轉,代碼自取

標簽:
AngularJS

    今天来个大家分享angular路由跳转的方法,话不多说直接上代码:

a标签进行跳转:

    1,创建组件ng g c homeng g c newsng g c product

    2,将组件引入到根组件中,并在declarations中进行配置。(如果是通过命令生成组件,原则上会配置,但需要检查一下)

import { NewsComponent } from './components/news/news.component';

import { HomeComponent } from './components/home/home.component';

import { ProductComponent } from './components/product/product.component';


 declarations: [

    AppComponent,

    NewsComponent,

    HomeComponent,

    ProductComponent

  ],

    3,在路由配置文件中引入我们创建的组件,在routes对象中配置路由。

import { HomeComponent } from './components/home/home.component';

import { NewsComponent } from './components/news/news.component';

import { ProductComponent } from './components/product/product.component';


const routes: Routes = [

  { path: 'home', component: HomeComponent },

  { path: 'news', component: NewsComponent },

  { path: 'product', component: ProductComponent },

];

    4,配置无路由匹配时,显示首页组件

const routes: Routes = [

  { path: '**', redirectTo: 'home' },

];

    5,通过a标签进行跳转(此时html文件)。[routerLink]属性是用来动态设置跳转的路由,routerLinkActive属性是用来设置路由被选中时的样式,需要在scss或者css文件中声明active类。

<header class="header">

  <!-- 动态路由 -->

  <a [routerLink]="[ '/home']" routerLinkActive="active">首页</a>

  <!-- 静态路由 -->

  <a routerLink= '/home' routerLinkActive="active">首页</a>


  <a [routerLink]="[ '/news']" routerLinkActive="active">新闻</a>


  <a [routerLink]="[ '/product']" routerLinkActive="active">商品</a>

</header>

<router-outlet></router-outlet>

    以上便是关于angular路由跳转的全部分享,大家学会了吗,更多内容干货可关注慕课网~


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消