No provider for NavParams error is coming In Ionic App

ktca8awb  于 2023-09-28  发布在  Ionic
关注(0)|答案(8)|浏览(179)

我正在将参数从一个组件传输到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值如下:uname = this.navParams.get('param 1');
这是我的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的提供程序。任何帮助都非常感谢。

kxeu7u2r

kxeu7u2r1#

你能试着加一下吗。希望能成功。如果你给我一个项目,我可以更好地帮助你。

@Component({
 selector: 'page-loginpage',
 templateUrl: 'loginpage.html',
 providers: [NavParams]
})
mcdcgff0

mcdcgff02#

我找到了一个解决方案,而不是在constructor中添加navParams
添加public navParams = new NavParams
例如

export class LoginpagePage {
  public navParams = new NavParams();
  todo: FormGroup;
  responseData: any;
  userData = { username: "", password: "" };

  constructor(public navCtrl: NavController) {};
}
um6iljoc

um6iljoc3#

我遇到了同样的问题,我必须做两件事来解决它:

  • 从app-routing-module.ts中删除模式路由条目
  • 在Modal的模块中更新以下条目,如下所示:
@NgModule({
     imports: [
       CommonModule,
       FormsModule,
       IonicModule
       ],
     declarations: [DetailsPage],
  entryComponents:[DetailsPage]
})

export class DetailsPageModule {}

通过这两个更改,我可以在de Modal ts文件中使用navParams:

ionViewWillEnter() {
   this.selectedItem = this.navParams.get('selectedItem');
   console.log(this.selectedItem);
 }
ny6fqffe

ny6fqffe4#

我用这个变体

const params = {
  phone,
  verificationId,
};
this.navCtrl.navigateForward("/forward-page", { state: params });

在Forward-Page

constructor(private router: Router) {
    const state = this.router.getCurrentNavigation().extras.state
    if (state) {
        this.phone = state.phone;
        this.verificationId = state.verificationId;
    }
}

工程伟大

ehxuflar

ehxuflar5#

使用ActivatedRoute接口从'@angular/router'简单地检索查询参数:

public constructor(private route: ActivatedRoute) {
    this.route.queryParams.subscribe(params => {
        this.firstname = params["param1"];
    });
}
hiz5n14c

hiz5n14c6#

你是否在@NgModule中导入了离子Angular 模块?

imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp, {

    })
  ],
68bkxrlz

68bkxrlz7#

对我来说(ionic7)是使用Angular的方式:

import { ActivatedRoute } from '@angular/router';
...
    constructor(
        private activeRoute: ActivatedRoute
    ) {}

    ngOnInit() {
        this.activeRoute.paramMap.subscribe(paramMap => {
            if (paramMap.has('id')) {            
                this.id = paramMap.get('id');
            }
        });
    }
htrmnn0y

htrmnn0y8#

我得出的结论是,app.component.ts是在应用程序中呈现的第一个东西。你不能导航到它,显然,你不能接收参数。

**第一种解决方案是:**我必须使用提供程序来存储您的用户数据,然后从提供程序设置您的侧菜单数据。
**第二个解决方案是:**使用下面的事件。

通过使用事件,我已经解决了我的问题,我的事件代码工作正常。

import { Events } from 'ionic-angular';

// first page (publish an event when a user is created)
constructor(public events: Events) {}
createUser(user) {
  console.log('User created!')
  this.events.publish('user:created', user, Date.now());
}

// second page (listen for the user created event after function is called)
constructor(public events: Events) {
  events.subscribe('user:created', (user, time) => {
    // user and time are the same arguments passed in `events.publish(user, time)`
    console.log('Welcome', user, 'at', time);
  });
}

相关问题