admin管理员组

文章数量:1353122

I am trying to get into 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
  }

}

I want to be able to get this value on connect as I want to know if it has changed.

I am trying to get into 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
  }

}

I want to be able to get this value on connect as I want to know if it has changed.

Share Improve this question edited May 28, 2022 at 20:34 ggorlen 57.9k8 gold badges114 silver badges157 bronze badges asked Aug 12, 2020 at 17:46 MZaragozaMZaragoza 10.1k9 gold badges74 silver badges116 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Your code declares const variable within the scope of connect() function. But you should use this (Stimulus Controller) property instead:

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

本文标签: javascriptStimulusJS How to set an instance variable on connectStack Overflow