0

I am looking to have some type of a config file that I can use to store properties that are needed in multiple components. Let me give you an example:

number-one-component.ts

export class NumberOneComponent {
    view: any[];
    xAxis: number;
    yAxis: number;
    label: string;
    labelPosition: string;

    chartSize: number;
}

number-two.component.ts

export class NumberTwoComponent {
    view: any[];
    xAxis: number;
    yAxis: number;
    label: string;
    labelPosition: string;

    chartSize: number;
}

I tried to create a file that I imported into one of the files and then do Object.assign(this, tokenName) in the constructor. Only problem was that TS complained about those properties not being part of the component.

I tried:

chart-config.ts

export const ChartConfig = {
    view: any[];
    xAxis: number;
    yAxis: number;
    label: string;
    labelPosition: string;

    chartSize: number;
}

That, did not work. In the constructor of either of the files, I did:

constructor() {
  Object.assign(this, ChartConfig);
}

With no luck.

I would appreciate insight as to how I can do this so that I can have a configuration file to share between components that need those properties.

0

1 Answer 1

1

You can just do

export class NumberOneComponent {
    chartConfig = ChartConfig;
}

export class NumberTwoComponent {
    chartConfig = ChartConfig;
}

Then both components share the same config object.

in the template you would bind for example:

{{chartConfig.label}}

instead of

{{label}}
Sign up to request clarification or add additional context in comments.

1 Comment

AHHH!!! I was not binding it right in my template. This is exactly what I needed. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.