Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
How can I pass a value to init methods on import?

Let's assume I have component like this:

'use strict';

export default () => ({
    selected: false,
    init(selectedIndex: -1)
    {

    }

});

and I import it like this (using AlpineJS 3):

import Alpine from 'alpinejs'

window.Alpine = Alpine

window.Components = {};

import Tab from "./components/Tab";
window.Components.Tab = Tab;

Alpine.start();

How can I now pass value to init method?

When I have:

<div x-data="Components.Tab(0)">...</div>

value is obviously not passed to init method.

Although there is info in docs how to register component from a bundle and info how to pass initial parameters, there is no info how to pass initial parameters to component registered from a bundle

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As you can see in the documentation of the init function:

If your component contains an init() method

you cannot add parameters to the init() method (notice the two brackets that mean no parameters).

The parameters have to be added to the component itself. So your component definition should be:

'use strict';

export default (initialSelected = -1) => ({
    selected: initialSelected,
    init()
    {

    }

});

BTW, I noticed that your selected field is boolean while your selectedIndex parameter is numerical. In my corrected code above I just used numerical for both.

Also, you have used this in your code:

window.Components.Tab = Tab;

But the documentation you have linked to says you need to use Alpine.data('Tab', Tab) to register from a bundle.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!