我似乎无法在带有sagas / generator函数的类中使用this
引用
export default class Foo {
constructor(name) {
this.name = name
}
*sayHello() {
yield put('@@actions/say Hello', this.name)
}
}
// In a Saga
const foo = new Foo('World')
yield call(foo.sayHello) // Crashes as `this` becomes undefined
我发现了一个小的hack,它将类本身作为参数传递,因此它可以读取类变量,但它感觉太乱了(而且不是线程安全的,但在我的情况下,它并不重要)< / p>
// Use special argument which is the class itself
*sayHello(self) {
yield put({type: '@@actions/SAY_HELLO', name: self.name})
}
call(foo.sayHello, foo) // Works