angular路由跳转页面地址栏跳转但是页面没有变化

angular路由跳转页面地址栏跳转但是页面没有变化

出现这个问题基本就是路由配置错误

ng路由跳转需要在app.module.ts里面配置相对应的路由

ng generate component index/index-list //创建文件夹

打开app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { RouterModule, Routes } from '@angular/router';


import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LoginListComponent } from './login/login-list/login-list.component';
import {IndexListComponent} from './index/index-list/index-list.component';
const appRoutes: Routes = [
  { path: 'loginList', component: LoginListComponent , data: { title: 'login List' }},
  { path: 'index-list' , component: IndexListComponent, data: { title: 'index-list'} },
  { path: '',
    redirectTo: 'loginList',
    pathMatch: 'full'
  },
  { path: '**', component: LoginListComponent }
];

@NgModule({
  declarations: [
    AppComponent,
    LoginListComponent,
    IndexListComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true } // <-- debugging purposes only
    )
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

然后再html里面写点击跳转方法

routerLink="/index-list"

如下

  <button type="button" class="btn btn-block btncustom10" routerLink="/index-list">walk up</button>

如果在app.module.ts里面没有设置相应的路由配置,则会出现地址栏跳转但页面没有跳转的现象。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Angular 中,可以使用 Angular 路由器来实现页面之间的跳转。首先,需要在 app.module.ts 文件中引入 RouterModule 模块,然后在 app-routing.module.ts 文件中定义路由规则。 例如,要实现从首页跳转到详情页,可以在 app-routing.module.ts 文件中定义如下路由规则: ```typescript import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { HomeComponent } from './home.component'; import { DetailComponent } from './detail.component'; const routes: Routes = [ { path: '', component: HomeComponent }, { path: 'detail/:id', component: DetailComponent }, ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { } ``` 其中,`path` 表示页面的 URL 路径,`component` 表示对应的组件。 然后在 HomeComponent 中设置一个链接,点击链接跳转到详情页: ```html <a [routerLink]="['/detail', item.id]">查看详情</a> ``` 其中,`routerLink` 指令用于设置链接,`['/detail', item.id]` 表示跳转到详情页,并传递一个参数 `id`。 最后,在 DetailComponent 中可以通过 ActivatedRoute 服务获取传递过来的参数: ```typescript import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-detail', template: ` <h2>详情页</h2> <p>ID: {{ id }}</p> ` }) export class DetailComponent implements OnInit { id: number; constructor(private route: ActivatedRoute) { } ngOnInit() { this.id = +this.route.snapshot.paramMap.get('id'); } } ``` 其中,`ActivatedRoute` 服务用于获取当前路由参数,`snapshot.paramMap.get('id')` 表示获取参数 `id` 的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值