Stimulus: How to write inheritance with stimulus controller ?

Created on 6 Feb 2018  路  4Comments  路  Source: hotwired/stimulus

// javascript/packs/controllers/parent_controller.js
import { Controller } from "stimulus"
export class ParentController extends Controller {
  myParentFunction(){
  }
}
// javascript/packes/controllers/child_controller.js
import { ParentController } from "parent_controller.js"
export default class extends ParentController {
  connect(){
    console.log("child controller loaded!")
  }    
}                               



md5-6e492ea6350ff7622ab7ea88fa776925



Uncaught TypeError: Super expression must either be null or a function, not object

Most helpful comment

// javascript/packs/controllers/parent_controller.js
import { Controller } from "stimulus"
export class ParentController extends Controller {
  myParentFunction(){
  }
}
// javascript/packes/controllers/child_controller.js
import  ParentController from "parent_controller.js"
export default class extends ParentController {
  connect(){
    console.log("child controller loaded!")
  }    
}                               

Resolved, just remove brackets in child controller file.

All 4 comments

// javascript/packs/controllers/parent_controller.js
import { Controller } from "stimulus"
export class ParentController extends Controller {
  myParentFunction(){
  }
}
// javascript/packes/controllers/child_controller.js
import  ParentController from "parent_controller.js"
export default class extends ParentController {
  connect(){
    console.log("child controller loaded!")
  }    
}                               

Resolved, just remove brackets in child controller file.

@thooams Do I have to do anything specific. Because I am doing exactly the same thing but getting following error:

Error: Cannot find module "parent_controller.js"

That should probably be more like:

import ParentController from "./parent_controller"

How can you setup webpacker to inherit from the ParentController without calling import at the top of each file?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gerdriesselmann picture gerdriesselmann  路  3Comments

saneef picture saneef  路  4Comments

savroff picture savroff  路  4Comments

joeybeninghove picture joeybeninghove  路  4Comments

JanaDeppe picture JanaDeppe  路  4Comments