How to create Constant.ts

How to create Constant.ts for sharing it between modules (components). Should it be injected or use another way? Please help.

PS: “dotnet new Aurelia” project

I have applicationConstants.js in the application root that I just inject as required. Works fine for my needs.

Sorry, my questions is not properly sound, I want to ask how I can inject correctly this file?

So I have something like this:

applicationConstants.js

export const Titles = [
    { title: "Mr" },
    { title: "Mrs" },
    { title: "Ms" },
    { title: "Miss" },
    { title: "Dr" },
    { title: "Prof" },
    { title: "Master" },
    { title: "Rev" },
    { title: "Sir" },
    { title: "Lady" },
    { title: "Lord" },
    { title: "Mx" }
];

addUser.js

import { Titles } from "applicationConstants";

@inject(Titles)
export class AddUser {
    constructor(Titles) {
        this.titles = Titles;
    }
}

Then I can just repeat.for through this.titles in the view to put them in a select list.

Thank you very match! This is what I need.

Unfortunately this is not work for me. I’m trying to inject something like that:

export const AppSettings = {
    BASE_URL: "http://localhost:64481",
    GET_QUIZ_URL: "/api/data"
}

but the AppSetting is undefined:

2018-07-19_0848

It is in one line, @inject(ApplicationConstants, AppSettings)
The order must match the order of constructor arguments.

This is work great! Thanks!

image

But what about @autoinject()

The “emitDecoratorMetadata”: already true

import { autoinject, inject } from 'aurelia-dependency-injection';
import { AppSettings } from "common/app-settings";
import { HttpClient } from 'aurelia-fetch-client';

@autoinject()
export class QuizService {

    settings : any;

    constructor(private httpClient: HttpClient, AppSettings) {
        this.httpClient = httpClient;
        this.settings = AppSettings;
    }
}

Can I use @autoinect?

@inject vs static @inject {…}

What is the best (preferred) way to do injection?

@autoinject is typescript only I believe - I’m not using typescript, but I’d probably use it if I was. Seems like an excellent idea to me. As for @inject and static inject, I don’t think there’s any difference really - just a matter of preference. I assume it’s handled exactly the same behind the scenes.

1 Like

Thank you very match! You save my day!

1 Like