StimulusJS 如何在连接上设置实例变量
StimulusJS How to set an instance variable on connect
我正在尝试进入 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()
}
someFunction(){
console.log(this.fooValue) // undefined
}
}
我希望能够在连接时获取此值,因为我想知道它是否已更改。
您的代码在 connect()
函数的范围内声明了 const
变量。但是你应该使用 this
(Stimulus Controller) 属性 代替:
...
connect() {
this.fooValue = this.fooTarget.value
...
我正在尝试进入 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()
}
someFunction(){
console.log(this.fooValue) // undefined
}
}
我希望能够在连接时获取此值,因为我想知道它是否已更改。
您的代码在 connect()
函数的范围内声明了 const
变量。但是你应该使用 this
(Stimulus Controller) 属性 代替:
...
connect() {
this.fooValue = this.fooTarget.value
...