【问题标题】:stimulus- How to get a variable set on connect刺激 - 如何在连接时设置变量
【发布时间】:2020-12-02 12:43:15
【问题描述】:

我正在尝试进入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
  }

}

我希望能够在连接时获取此值,因为我想知道它是否已更改。

【问题讨论】:

    标签: stimulusjs


    【解决方案1】:

    您的代码在connect() 函数的范围内声明了const 变量。但是您应该改用 this (Stimulus Controller) 属性:

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

    【讨论】:

    • 谢谢,这真的很有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2015-10-14
    • 2022-01-06
    • 2021-03-06
    相关资源
    最近更新 更多