Search code examples
aurelia

Aurelia send config/parameters on initialisation aurelia-app


Is there any way to send let's say config parameters when initializing an Aurelia app?

<div aurelia-app="my-app/main"></div>

This is what I do to start my aurelia app and then

<script>
  System.import('aurelia-bootstrapper');
</script>

I'd like to do something like:

<div aurelia-app="my-app/main" param1="something" param2="false"></div>

Right now I'm setting them as data-param1 attributes and then getting the values in the controller like Element.dataset.param1

Is there a better way to do that?


Solution

  • I found a way to do it that I think it works pretty well. As I'm using SystemJS I found out I can do something like this when initialising my Aurelia app:

    System.set('my-config', System.newModule({
      Params: {
        param1: true,
        param2: 'something',
        param3: [
          {
            name: 'whatever',
          },
        ]
      }
    }));
    
    System.import('aurelia-bootstrapper');
    

    And then, the Params module is available in the whole app, so I can import it and use it:

    import {inject} from 'aurelia-framework';
    import {Params} from 'my-config';
    
    @inject(Params)
    export class MyComponent {
      constructor(params) {
        //params.param1 etc...
      }
    }
    

    Pretty handy in my opinion. I hope it helps!