Ionic Uncaught(承诺):链接无效

Mrf*_*fet 7 ionic3

this.nav.push在Ionic中遇到了问题.我已经完成了登录/注册页面,但是当我登录时,我收到此错误消息.我必须从login.ts添加代码,例如home.ts(这是主页)?

运行时错误
未捕获(在承诺中):无效链接:HomePage


错误:未捕获(在承诺中):无效链接:主页d(http:// localhost:8100/build/polyfills.js:3:3991)at l(http:// localhost:8100/build/polyfills.js: 3:3244)at Object.reject(http:// localhost:8100/build/polyfills.js:3:2600)at NavControllerBase._fireError(http:// localhost:8100/build/main.js:45465:16) at NavControllerBase._failed(http:// localhost:8100/build/main.js:45453:14)位于http:// localhost:8100/build/main.js:45508:59 at t.invoke(http:// localhost:8100/build/polyfills.js:3:11562)在t.invoke的对象.onInvoke(http:// localhost:8100/build/main.js:4622:37)(http:// localhost:8100/build/polyfills.js:3:11502)在n.run(http:// localhost:8100/build/polyfills.js:3:6468)的http:// localhost:8100/build/polyfills.js:3: 3767 在t.invokeTask(HTTP://本地主机:8100 /构建/ polyfills.js:3:12256)在Object.onInvokeTask(HTTP://本地主机:8100 /构建/ main.js:4613:37)在t.在n.runTask(http:// localhost:8100/bu)的invokeTask(http:// localhost:8100/build/polyfills.js:3:12177)ild/polyfills.js:3:7153)

@edit:当我想登录我的应用程序时出现问题.这是登录的代码响应者.

login.ts

public login() {
    this.showLoading()
    this.auth.login(this.registerCredentials).subscribe(allowed => {
      if (allowed) {        
       this.nav.push('HomePage');
      } else {
        this.showError("Access Denied");
      }
    },
      error => {
        this.showError(error);
      });
    }
Run Code Online (Sandbox Code Playgroud)

auth-service.ts(这里是执行我的腰部的公共功能,我有通行证和电子邮件,我需要输入登录)

  public login(credentials) {
    if (credentials.email === null || credentials.password === null) {
      return Observable.throw("Please insert credentials");
    } else {
      return Observable.create(observer => {
        // At this point make a request to your backend to make a real check!
        let access = (credentials.password === "pass" && credentials.email === "email");
        this.currentUser = new User('Simon', 'saimon@devdactic.com');
        observer.next(access);
        observer.complete();
      });
    }
  }
Run Code Online (Sandbox Code Playgroud)

我不知道为什么这段代码错了.我的主页我的应用程序是home.ts和类是HomePage

ryl*_*xes 19

你需要做的是在'@Component'之前添加@IonicPage()并像这样导入'IonicPage': import {NavController, IonicPage} from 'ionic-angular';

然后你需要为主页创建一个模块,即在主目录中,使用以下内容创建home.module.ts

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { HomePage } from './home';

@NgModule({
  declarations: [
    HomePage
  ],
  imports: [
    IonicPageModule.forChild(HomePage),
  ],
  exports: [
    HomePage
  ]
})
export class HomePageModule {}
Run Code Online (Sandbox Code Playgroud)

并重新加载项目


小智 8

您的代码中唯一的问题是当您 在调用任何页面时this.nav.push('HomePage');删除 "引号",并且这样写.

this.nav.push(HomePage); 没有引号.

以上回答是针对ionic2,在ionic3中有懒惰加载,你可以像这样调用 this.nav.push('HomePage');