【问题标题】:Ionic 4 beta 18 - ion-item and ion-button with routerLink don't change routeIonic 4 beta 18 - 带有 routerLink 的 ion-item 和 ion-button 不会改变路由
【发布时间】:2019-05-15 16:34:48
【问题描述】:

我刚刚从 ionic 4 beta 17 更新到 18,并引入了 <ion-item><ion-button> 的重大更改。 href 已替换为 routerLink

我将此添加到我的代码中,现在路由不再起作用。 大多数情况下,编译器会抛出错误并说 ion-item 中不存在 routerLink。

我做错了什么?还是棱角分明?

"@ionic/angular": "~4.0.0-beta.18",
"@ionic/core": "~4.0.0-beta.18",
"@angular/core": "^7.1.3",
"@angular/router": "~7.1.3",

<ion-item text-wrap class="nav-item" routerDirection="forward" 
        [routerLink]="p.url"
        *ngFor="let p of listOfPages; let i = index"
        [class.active-item]="checkActivePath(p.url)"
        [class.last-item]="i === (listOfPages.length - 1)">
    <ion-label class="nav-label">
        <ion-img [src]="p.icon" class="nav-icon"></ion-img>
        <div class="text-block">
            {{ p.title }}
        </div>
    </ion-label>
</ion-item>

【问题讨论】:

    标签: angular ionic-framework angular7 beta ionic4


    【解决方案1】:

    所以这就是最终对我有用的方法:

    1. 确保在每个模块中都包含 RouterModule
    2. 在我使用的每个嵌套模块中

      const routes: Routes = [{
          path: '',
          component: AboutUsPage
      }];
      
    3. 我也在那里导入了RouterModule.forChild(routes)

    4. 对于我在路径前添加/ 的路线。喜欢'/home'
    5. app-routing.ts 中我使用了{path: 'about-us/:id', component: AboutUsPage},

    【讨论】:

      【解决方案2】:

      在模板所在模块(使用routerLink的模块)的模块文件中导入RouterModule

      import { RouterModule } from '@angular/router';
      @NgModule({
          imports: [
              // ...
              RouterModule,
          ]
      

      【讨论】:

      • 谢谢。这给了我一些必要的见解。
      【解决方案3】:

      也许在 routerDirection 之后缺少 "?

      routerDirection="forward
      

      编辑:当您使用 Ionic 组件时,您应该使用 href。 RouterLink 用于非 Ionic 组件。

      【讨论】:

      • 感谢您的回答。我忘记了引号,但这不是问题。在 beta 18 的变更日志中,团队指出,出于一致性原因,他们更改为 [routerLink]
      猜你喜欢
      • 2019-10-14
      • 2018-09-25
      • 2023-03-17
      • 2019-09-29
      • 2019-11-22
      • 2018-03-13
      • 2020-11-18
      • 1970-01-01
      • 2020-10-31
      相关资源
      最近更新 更多