简体   繁体   English

Typescript-Angular 导出的枚举未定义

[英]Typescript-Angular exported enum is undefined

I have an Angular project with some components and some enums.我有一个 Angular 项目,其中包含一些组件和一些枚举。 I was able to successfully use the enums in one of my components, but now that I try to do the same in other components, undefined is returned.我能够在我的一个组件中成功使用枚举,但现在我尝试在其他组件中做同样的事情,返回未定义。

Component in which the enums work:枚举在其中工作的组件:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import * as moment from 'moment';
import { from } from 'rxjs';
import { WEEKLY_SMART_POINTS } from '../constants';
import { Activity } from '../dom/profile/Activity';
import { Gender } from '../dom/profile/Gender';
import { Goal } from '../dom/profile/Goal';
import { Profile } from '../dom/profile/Profile';
import { ProfileService } from '../services/profile.service';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.page.html',
  styleUrls: ['./profile.page.scss'],
})
export class ProfilePage implements OnInit {
  eGender = Gender;
  eActivity = Activity;
  eGoal = Goal;
  valid: boolean;

  currentProfile: Profile;
  savedProfile: Profile;

  constructor(private profileService: ProfileService, private router: Router) {
    this.currentProfile = this.createEmptyProfile();
    this.savedProfile = this.createEmptyProfile();
  }

  ionViewWillEnter() {
    this.currentProfile = this.createEmptyProfile();
    this.savedProfile = this.createEmptyProfile();
    from(this.profileService.loadSaved()).subscribe((profile) => {
      Object.assign(this.currentProfile, profile || this.createEmptyProfile());
      Object.assign(this.savedProfile, profile || this.createEmptyProfile());
    });
  }

  ngOnInit() {
    console.log(this.eGender); //this works
  }
...

Some components in which the enums return undefined:枚举返回未定义的一些组件:

import { Component, OnInit } from '@angular/core';
import { Portion } from '../dom/products/Portion';
import { Product } from '../dom/products/Product';
import { Nutrition } from '../dom/products/Nutrition';
import { Gender } from '../dom/profile/Gender';

@Component({
  selector: 'app-calculator',
  templateUrl: './calculator.page.html',
  styleUrls: ['./calculator.page.scss'],
})
export class CalculatorPage implements OnInit {
  smartPoints: number | string = '-';

  nutrition: Nutrition;
  portion: Portion;
  product: Product;

  valid: boolean;

  eGender: Gender;

  constructor() {
    this.nutrition = this.createEmptyNutrition();
    this.portion = this.createEmptyPortion();
    this.product = this.createEmptyProduct();
  }

  ionViewWillEnter() {
    this.nutrition = this.createEmptyNutrition();
    this.portion = this.createEmptyPortion();
    this.product = this.createEmptyProduct();
  }

  ngOnInit() {
    console.log(this.eGender); //returns undefined
  }
...
/* eslint-disable no-underscore-dangle */
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Portion } from '../dom/products/Portion';
import { Product } from '../dom/products/Product';
import { SubSize } from '../dom/products/SubSize';
import { ProductService } from '../services/product.service';

@Component({
  selector: 'app-product-detail',
  templateUrl: './product-detail.page.html',
  styleUrls: ['./product-detail.page.scss'],
})
export class ProductDetailPage implements OnInit {
  eSubSize: SubSize;
  product: Product;
  selectedSize: number;
  selectedSubSize: SubSize;
  selectedPortionId: string;

  constructor(
    private productService: ProductService,
    private route: ActivatedRoute
  ) {}

  ngOnInit() {
    console.log(this.eSubSize); //returns undefined
    this.getProduct();
  }

  sameOrder() {
    return 0;
  }

  private getDisplayPortion(): Portion {
    let portion: Portion;
    this.product.portions.forEach((p) => {
      if (p.default === true) {
        portion = p;
      }
    });
    return portion === undefined ? this.product.portions[0] : portion;
  }

  private async getProduct() {
    const id = this.route.snapshot.paramMap.get('id');
    this.product = await this.productService.getSystemProduct(id);
    const displayPortion = this.getDisplayPortion();
    this.selectedSize = displayPortion.size;
    this.selectedSubSize = SubSize.none;
    this.selectedPortionId = displayPortion._id;
  }
}

Some enums:一些枚举:

export enum Gender {
  m = 'Man',
  f = 'Vrouw',
}
export enum SubSize {
  none = '--',
  oneEight = '1/8',
  oneFourth = '1/4',
  oneThird = '1/3',
  threeEight = '3/8',
  half = '1/2',
  fiveEight = '5/8',
  twoThird = '2/3',
  threeForth = '3/4',
  sevenEight = '7/8',
}

I'm totally stuck, hopefully someone has the answer.我完全被困住了,希望有人能给出答案。

You used it correctly in ProfilePage .您在ProfilePage中正确使用了它。

eGender = Gender

On other faulty components, you just identified your type.;在其他有故障的组件上,您只是确定了您的类型。

eGender: Gender

eSubSize: SubSize

So you didn't assign a value to the variables.所以你没有为变量赋值。 That's why it gives an undefined error.这就是它给出未定义错误的原因。

So you should use = instead of :所以你应该使用=而不是:

eGender = Gender

eSubSize = SubSize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM