【问题标题】:Angular 2: Ts: Nested functionsAngular 2:Ts:嵌套函数
【发布时间】:2017-10-26 04:38:20
【问题描述】:

我正在研究一个函数,有人可以就如何指定函数之外的函数提供建议吗? 在 if 语句中,我想调用 otherfunction()。

@Injectable()
export class menuService {
    constructor (){}
    testing(){ console.log('something')}
    loadwidget(){


           // not able to call this function
           this.testing()

    }
}

我得到的错误是“this.testing is not a function”

ERROR TypeError: this.testing is not a function
    at Object.menuService.loadwidget (http://localhost:3000/main.bundle.js:755:14)
    at Object.eval [as handleEvent] (ng:///AppModule/tbuttonsComponent.ngfactory.js:36:41)
    at handleEvent (http://localhost:3000/vendor.dll.js:13146:138)
    at callWithDebugContext (http://localhost:3000/vendor.dll.js:14354:42)
    at Object.debugHandleEvent [as handleEvent] (http://localhost:3000/vendor.dll.js:13942:12)
    at dispatchEvent (http://localhost:3000/vendor.dll.js:10121:21)
    at http://localhost:3000/vendor.dll.js:10711:38

https://plnkr.co/edit/XCHsu19UhR9wWxz4VLOx?p=preview

【问题讨论】:

    标签: javascript angular types


    【解决方案1】:

    这正是你应该如何访问一个类中的其他方法,关闭this 关键字。如果您完全按照上面写的那样使用您的类,那么问题是variableName 没有在customfunction() 中的任何地方定义,所以它会出错。因此,otherfunction() 中的 console.log() 语句永远不会有机会运行。

    编辑:我查看了您添加的 Plunker,结果发现这是一个范围界定问题。我更新了menuService 类,使用箭头函数将this 隐式绑定到menuService,第三个按钮开始按预期工作:

    export class menuService {
      constructor (){
        // I moved this into the constructor and updated loadingwidget below
        this.menu = [{
          id: 1,
          loadingwidget: () => { this.loadwidget(); },
        }];
      }
    
      testing(){ 
        console.log('something');
        alert('something');
      }
    
      loadwidget(){
        this.testing();
      }
    }
    

    这是您的 Plunker 的工作版本:https://plnkr.co/edit/sF08cccRb2b0xkfTspVV?p=preview

    【讨论】:

    • 可以注射吗?
    • @ChrisTarasovs 看看我的编辑,这不是@injectable 问题,而是this 未绑定到menuService 的问题。
    【解决方案2】:

    这不是因为 Injectable,您也不需要 Injectable,因为您没有向服务中注入任何东西。所以你可以省略它。但是我可以看到像这样的代码中很少有问题,

     menu = [
          {
            id = 1,
            loadingwidget: this.loadwidget
          }
        ]
    

    应该在哪里

    menu = [
          {
            id:1,
            loadingwidget: this.loadwidget
          }
        ]
    

    它没有正确编译。它工作得很好。

    【讨论】:

      【解决方案3】:

      您定义的方式应该可以工作。如果otherfunction() 未被调用,请检查您的 if 语句,可能比较不会返回您期望的结果。

      请看这个 plunker 示例:https://plnkr.co/edit/4MWYDdeS5cCTDHI64HO2?p=preview

      【讨论】:

      • 我已经完全按照我的方式进行了更新,我知道它不是一个函数,但我正确地调用了它。
      • 我已经更新了 plunker 示例,该示例也适用于您的代码。您会与您的代码进行比较吗?另请查看您调用 loadwidget() 的位置。
      • 这里更接近我所拥有的代码:plnkr.co/edit/XCHsu19UhR9wWxz4VLOx?p=preview
      • 所以我从一个对象加载菜单,而不是 onclick 我加载了一个在对象中定义的函数,并且在该函数中有一个我想调用的函数
      • 您尝试将方法作为参数提供给对象。这就是我在 JavaScript 中的做法,但在 Typescript 中我会利用类的好处。不要在 menuService 中使用数组,而是在 App.ts 中创建一个 menuServices 数组并将其用于您的小部件。因为每个 menuService 对象都有自己的参数。例如 plunker:plnkr.co/edit/Lrx37LFTitDLXETcont9?p=preview。如果您想使用带有他自己逻辑的 html 对象,我建议您创建指令:syntaxsuccess.com/viewarticle/recursive-treeview-in-angular-2.0
      猜你喜欢
      • 2018-10-13
      • 2017-07-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 2018-03-31
      • 2018-06-11
      • 1970-01-01
      相关资源
      最近更新 更多