【问题标题】:Typescript: Unable to get value of the property 'block': object is null or undefined打字稿:无法获取属性“块”的值:对象为空或未定义
【发布时间】:2013-10-18 08:56:18
【问题描述】:

我为有效的电话号码编写代码。但是当我编译项目时,我有以下错误:

  1. 无法获取属性“block”的值:对象为 null 或未定义;
  2. 命令的输出 ""C:\Program Files (x86)\Microsoft SDKs\TypeScript\tsc" "C:..\Scripts\MyScripts\TS\BuildPhone.ts" "C:..\Scripts\MyScripts\ TS\ProductsViewModel.ts" "C:..\Scripts\MyScripts\TS\helloWorld.ts"" 代码为 1。

    // Interface
    interface IPhoneBuild
    {
        showPhone(): string;
        checkPhone(): boolean;
    }
    
    class Phone
    {
        Code: string;
        Number: string;
        Fax: boolean;
    }
    
    // Module
    module Phones
    {
        // Class
        export class PhoneBuild implements IPhoneBuild
        {
            private phone: Phone;
            private codes: string[];
    
            // Constructor
            constructor(public Number: string, public Code: string, public Codes: string[])
            {
                this.phone = this.buildPhone(Number, Code);
                this.codes = Codes;
            }
    
            //Private Methods
            private clearPhone(public reg: string)
            {
                var re = /\W/g;
                return reg.replace(re, "");
            }
    
            private buildPhone(public num: string, public code: string)
            {
                var p: Phone = { Code: "", Number: "", Fax: false };
    
                num = this.clearPhone(num);
    
                if (num.length == 6)
                {
                    p = { Code: code, Fax: false, Number: num };
                }
    
                if (num.length == 11)
                {
                    p = { Code: num.substring(0, 4), Fax: false, Number: num.substring(4)};
                }
    
                return p;
            }
    
            // Instance member
            public showPhone()
            {
                return this.phone.Code + this.phone.Number;
            }
    
            public checkPhone()
            {
                return this.phone.Number.length != 7 || this.phone.Code.length == 0;
            }
        }
    }
    

你怎么看?我需要帮助。

我找到了解决方法:删除方法中属性中的“public”:buildPhone 和 clearPhone。

【问题讨论】:

    标签: typescript tsc


    【解决方案1】:

    您不能将成员function 的参数声明为公开的。这仅对用作以下简写的构造函数有效:

    • 声明成员变量
    • 还将输入参数分配给成员变量

    【讨论】:

      【解决方案2】:

      我已经快速整理了你的代码。

      在构造函数中使用 publicprivate 时,可以避免从参数映射到类上的属性,因为 TypeScript 编译器会自动为您完成。

      您的方法参数不需要这些访问修饰符 - 它们不能是 publicprivate,因为它们的作用域是函数(即它们只在函数的持续时间内存在,不能在函数之外访问)函数)。

      下面的例子应该可以正常编译。

      // Interface
      interface IPhoneBuild
      {
          showPhone(): string;
          checkPhone(): boolean;
      }
      
      class Phone
      {
          Code: string;
          Number: string;
          Fax: boolean;
      }
      
      // Module
      module Phones
      {
          // Class
          export class PhoneBuild implements IPhoneBuild
          {
              private phone: Phone;
      
              // Constructor
              constructor(phoneNumber: string, code: string, public codes: string[])
              {
                  this.phone = this.buildPhone(phoneNumber, code);
              }
      
              //Private Methods
              private clearPhone(reg: string)
              {
                  var re = /\W/g;
                  return reg.replace(re, "");
              }
      
              private buildPhone(num: string, code: string)
              {
                  var phone: Phone = { Code: "", Number: "", Fax: false };
      
                  num = this.clearPhone(num);
      
                  if (num.length == 6)
                  {
                      phone.Code = code;
                      phone.Number = num;
                  }
      
                  if (num.length == 11)
                  {
                      phone.Code = num.substring(0, 4);
                      phone.Number = num.substring(4);
                  }
      
                  return phone;
              }
      
              // Instance member
              public showPhone()
              {
                  return this.phone.Code + this.phone.Number;
              }
      
              public checkPhone()
              {
                  return this.phone.Number.length != 7 || this.phone.Code.length == 0;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-11-01
        • 1970-01-01
        • 2012-06-11
        • 2013-04-24
        • 1970-01-01
        • 2023-03-25
        • 2014-02-03
        • 1970-01-01
        相关资源
        最近更新 更多