【问题标题】:Ionic 4 Router does not have navigate and navigateByUrlIonic 4 路由器没有 navigate 和 navigateByUrl
【发布时间】:2019-06-27 18:44:07
【问题描述】:

我在 Ionic 4 中实现路由器,基本上只是想从一个页面导航到另一个页面。

我实现的代码如下:

import { Component } from '@angular/core';
import { NavController, LoadingController } from '@ionic/angular';
import { Route } from '@angular/router';

@Component({
  selector: 'app-secure2fa',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
  introFlag; any = false;
  userName: String;
  loading: any;

  constructor(private route: Route)
  {

  }

  goRegister() {
    this.route.navigateByUrl('/secure-registration');
  }
}

安全注册页面是使用 Ionic CLI 命令创建的,因此它会自动添加到 app-routing.module 中,如下所示:

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

const routes: Routes = [
  { path: '', loadChildren: './tabs/tabs.module#TabsPageModule' },
  { path: 'location', loadChildren: './location/location.module#LocationPageModule' },
  { path: 'secure-registration', loadChildren: './secure-registration/secure-registration.module#SecureRegistrationPageModule' },
  { path: 'success-registration', loadChildren: './success-registration/success-registration.module#SuccessRegistrationPageModule' }
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {}

我在这里遇到的问题是没有找到 navigateByUrl 甚至是 navigation。错误如下:

“路线”类型上不存在属性“navigateByUrl”。

任何人都知道我是否在这里遗漏了什么。我正在使用 Ionic 4。

谢谢。

【问题讨论】:

    标签: typescript ionic-framework router angular-routing ionic4


    【解决方案1】:

    您正在导入 Route,您应该正在导入 Router

    import { Router } from '@angular/router';
    

    并将其注入您的班级。

    【讨论】:

    • 哦,我明白了。我在想 Router 或 Route 只是我声明的名称约定。感谢那。对不起,因为我是 Ionic 和 Typescript/Angular 的新手,所以犯了愚蠢的错误。
    猜你喜欢
    • 2016-10-25
    • 2019-01-12
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2019-08-11
    • 1970-01-01
    • 2019-08-30
    相关资源
    最近更新 更多