【发布时间】:2020-08-06 06:51:41
【问题描述】:
我正在将参数从一个组件传输到 app.component.ts 但错误来了。错误:没有 NavParams 的提供程序。
这是我的 app.component.ts:
import { FrontPage } from "./../pages/front/front";
import { Component, ViewChild } from "@angular/core";
import { Nav, Platform, NavController, NavParams } from "ionic-angular";
import { StatusBar } from "@ionic-native/status-bar";
import { SplashScreen } from "@ionic-native/splash-screen";
import { HomePage } from "../pages/home/home";
import { ListPage } from "../pages/list/list";
import { ProductPage } from "../pages/product/product";
import { LoginpagePage } from "../pages/loginpage/loginpage";
@Component({
templateUrl: "app.html",
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
menuclick: boolean = true;
menuclick2: boolean = false;
rootPage: any = FrontPage;
uname: string;
pages: Array<{ title: string; component: any; name2: string }>;
constructor(
public platform: Platform,
public statusBar: StatusBar,
public splashScreen: SplashScreen,
public navParams: NavParams
) {
this.initializeApp();
this.uname = this.navParams.get("param1");
this.pages = [
{ title: "Home", component: FrontPage, name2: "home" },
{ title: "Product Categories", component: ProductPage, name2: "basket" },
{ title: "Merchandise", component: ProductPage, name2: "man" },
{ title: "My Orders", component: ProductPage, name2: "cart" },
];
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
});
}
openPage(page) {
this.nav.setRoot(page.component);
}
loginpage2() {
this.nav.push(LoginpagePage);
}
logoutClicked() {
console.log("Logout");
this.nav.pop();
}
}
在这个组件中,我得到这样的 navparam 值 this.uname = this.navParams.get('param1');
这是我的loginpage.ts:
import { Component } from "@angular/core";
import {
IonicPage,
NavController,
NavParams,
AlertController,
} from "ionic-angular";
import { RestapiProvider } from "../../providers/restapi/restapi";
import { ListPage } from "../list/list";
import { FrontPage } from "../front/front";
import { CartPage } from "./../cart/cart";
import { Validators, FormBuilder, FormGroup } from "@angular/forms";
import { MyApp } from "./../../app/app.component";
@IonicPage()
@Component({
selector: "page-loginpage",
templateUrl: "loginpage.html",
})
export class LoginpagePage {
todo: FormGroup;
responseData: any;
userData = { username: "", password: "" };
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public restProvider: RestapiProvider,
private formBuilder: FormBuilder,
private alertCtrl: AlertController
) {
this.todo = this.formBuilder.group({
username: ["", Validators.required],
password: ["", Validators.required],
});
}
ionViewDidLoad() {
console.log("ionViewDidLoad LoginpagePage");
}
getloginUsers() {
this.restProvider
.getUsers(this.userData, "user_Login")
.subscribe((data) => {
console.log(data);
if (data) {
this.responseData = data;
console.log(this.responseData.msg.name);
if (this.responseData.status === "success") {
this.navCtrl.push(MyApp, {
param1: this.responseData.msg.name,
});
} else {
this.presentAlert();
}
}
});
}
presentAlert() {
let alert = this.alertCtrl.create({
title: "Incorrect Username Or Password",
buttons: ["Dismiss"],
});
alert.present();
}
cardpage2() {
this.navCtrl.push(CartPage);
}
}
错误:NavParams 没有提供程序即将到来。非常感谢任何帮助。
【问题讨论】:
-
当您没有在提供程序数组中提供服务时,通常会发生此错误。请检查一下
-
@AnkurShah。提供者:[ NavParams ],我在 app.module.ts 中添加了这个。
-
@AnkurShah。您能否为此提供答案。请。
-
@AnkurShah。导入后出现此错误。无法解析 NavParams 的所有参数:(?)。
-
@Ragav 你能否提供给你一个错误的示例源项目
标签: angular ionic-framework ionic3