Stimulusjs 刺激-如何在connect上设置变量

Stimulusjs 刺激-如何在connect上设置变量,stimulusjs,Stimulusjs,我正试图进入刺激 import { Controller } from 'stimulus' export default class extends Controller { static targets = [ 'foo', ] connect() { const fooValue = this.fooTarget.value console.log(this.fooValue) # 7 this.someFunction() }

我正试图进入刺激

import { Controller } from 'stimulus'

export default class extends Controller {

  static targets = [
    'foo',
  ]

  connect() {
    const fooValue = this.fooTarget.value
    console.log(this.fooValue) # 7
    this.someFunction()
  }

  someFunction(){
    console.log(this.fooValue) # undefined
  }

}
我希望能够在connect上获取此值,因为我想知道它是否已更改。

您的代码在connect函数的范围内声明了const变量。但您应该改用此刺激控制器属性:

...
  connect() {
    this.fooValue = this.fooTarget.value
...
您的代码在connect函数的作用域内声明const变量。但您应该改用此刺激控制器属性:

...
  connect() {
    this.fooValue = this.fooTarget.value
...

谢谢这真的很有帮助谢谢这真的很有帮助