使用Mobx在其他函数中调用函数

问题描述 投票:1回答:1

在React Native中使用简单的Mobx Store,并试图弄清楚为什么我无法使用它。我有一个函数,我想调用另一个函数来inatlize数据库监听器

它非常简单

class Store {
 @action FirstFunction(){
  this.SecondFunction();
   // I also tried:
    SecondFunction();
   //neither worked
 }

 @action SecondFunction(){
   console.log("Second Function!");
 }

}

有任何想法吗?在mobx中这可能吗?无法弄清楚为什么它不会..

javascript mobx mobx-react
1个回答
0
投票

使用this有效,但你必须确保this的值是你期望的值。

示例(JSBin

class Store {
 @action firstFunction() {
   console.log("First Function!")
   this.secondFunction();
 }

 @action secondFunction(){
   console.log("Second Function!");
 }
}

const store = new Store();

console.log("This works:");
store.firstFunction();

console.log("This will throw an error:");
setTimeout(store.firstFunction, 1000);
© www.soinside.com 2019 - 2024. All rights reserved.