2

Say you have a standard TodoList app.

There is an app component, which "provides" a TodoStore service which manages(gets/sets/modifies) all the data.

You create AppComponent which "provides" this TodoStore and you inject this into your TodoListComponent. Everything works as expected.

Now, the customer, for some reason wants to have TWO TodoListComponent, side by side. Because components are meant to be reusable and composable, this is a valid use case.

But how would you provide a different TodoStore into the two components? Or is my design fundamentally flawed?

1 Answer 1

1

You can wrap your TodoListComponent with different components that provide different services.

@Component({
  selector: 'todo-list-1',
  providers: [{provide: TodoStore, useFactory: (...) => new TodoStore(...), deps: [...]}],
  template: '<todo-list></todo-list>'
})
class TodoList1Component {}

For each different store instance use a different wrapper component.

... depend on how your store needs to be created (you didn't provide the details).

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

2 Comments

Ah! very smart. I see that in your example, you used a factory for the provider. So if you had two different wrapper components, then am I correct in saying that you don't need a factory?
I just don't know how you create your different instances. They might need to be initialized with different parameters for example. If they are just created with new TodoStore() and all you need is 2 different instances then you can replace the whole providers: ... line with just providers: [TodoStore],

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.