angular - 如何修复“无法获取未定义或空引用的属性'get'”
问题描述
我正在尝试创建应用程序。但是,我不断收到错误消息“无法在 ProfilePage.prototype.ngOnInit 处获取未定义或空引用的属性 'get'”。
我使用 ionic 4 作为我的框架。当我试图从我的主页导航到我的个人资料页面时发生此错误
HomePage.html:
<ion-header>
<ion-toolbar>
<ion-title>Home</ion-title>
<ion-buttons slot="end">
<ion-button routerLink="/profile">
<ion-icon slot="icon-only" name="person"></ion-icon>
</ion-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
profile.page.ts
import { Component, OnInit } from '@angular/core';
import { AlertController } from '@ionic/angular';
import { AuthService } from '../../services/user/auth.service';
import { ProfileService } from '../../services/user/profile.service';
import { Router } from '@angular/router';
@Component({
selector: 'app-profile',
templateUrl: './profile.page.html',
styleUrls: ['./profile.page.scss']
})
export class ProfilePage implements OnInit {
public userProfile: any;
public birthDate: Date;
constructor(
private alertCtrl: AlertController,
private authService: AuthService,
private profileService: ProfileService,
private router: Router
) {}
ngOnInit() {
this.profileService
.getUserProfile()
.get()
.then(userProfileSnapshot => {
this.userProfile = userProfileSnapshot.data();
this.birthDate = userProfileSnapshot.data().birthDate;
});
}
......
Homepage.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: './home.page.html',
styleUrls: ['./home.page.scss'],
})
export class HomePage implements OnInit {
constructor() { }
ngOnInit() {}
}
上面的代码预计允许用户进入个人资料页面。但是结果是“无法在 ProfilePage.prototype.ngOnInit 处获取未定义或空引用的属性 'get'”
解决方案
可能您this.profileService.getUserProfile()
只是返回 null 或 undefined。使用 chrome 开发工具 Source 选项卡检查它(使用 ctrl (cmd) + p 搜索文件)
推荐阅读
- angular - 结构类型和多态 - 添加超类型
- python - python 无法让代码正常工作
- laravel - Laravel 计数 > N
- vagrant - Vagrant Up (private_network) 错误:默认值:警告:连接中止。重试
- node.js - 为什么nodejs在处理路由请求后返回404?
- babeljs - 如何使汇总扩展`require`语句?
- javascript - JavaScript
不工作 - javascript - MeteorJS - 添加 SlickJS afterChange 事件
- laravel-5 - Laravel 在从 Gmail API 发送电子邮件时使用视图
- postgresql - DataGrip 无法识别 PostgreSQL 数据类型