0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Stimulus 別コントローラーのアクションを呼びたい

Posted at
div[data-controller="hoge fuga"]
  button data-action="fuga#fugaAction"
app/javascript/controllers/hoge.js
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="hoge"
export default class extends Controller {
  initialize() {
    this.element[this.identifier] = this 👈これが重要
  }

  hogeAction() {}
}
app/javascript/controllers/fuga.js
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="fuga"
export default class extends Controller {
  fugaAction() {
    this.element['hoge'].hogeAction() 👈これでhogeのアクションを呼べる
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?