最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - StimulusJS How to set an instance variable on connect - Stack Overflow

programmeradmin2浏览0评论

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
...
发布评论

评论列表(0)

  1. 暂无评论