Build a module that includes both components and services

Currently, I am working on an Ionic 3 application and found the need for an accordion component similar to NgBootstrap's. Unfortunately, the Ionic framework does not offer such a component. This led me to take on the challenge of creating one myself, allowing me to further explore Angular 5 in the process.

To provide some context for my query, my custom accordion component is structured within the following file tree:

components/
   my-accordion/
      my-accordion.component.html
      my-accordion.component.css
      my-accordion.component.ts
      my-accordion.service.ts

In order for my accordion to function as intended, I require a specific service that facilitates communication between all instances of the component. When one item within the accordion is opened, I want the others to automatically close, ensuring only one item is displayed at a time. Leveraging the RxJS library has proven effective for achieving this interaction.

It's worth noting that the proper functionality of my accordion hinges on both the Component and Service working in tandem. Without the service, the accordion will not perform optimally. My goal is to package this accordion into a module that can be shared across different applications for versatile reuse.

My initial instinct was to place the accordion in the SharedModule, but according to the ngModules FAQs:

The SharedModule should not have providers for reasons explained previously. Nor should any of its imported or re-exported NgModules have providers.

Faced with this restriction, I considered incorporating the accordion into the CoreModule. However, the documentation explicitly states:

Consider making CoreModule a pure services module with no declarations.

Given these constraints, my question arises: Where should I house a component AND its associated service (for inter-component communication) to ensure seamless reusability across various Ionic/Angular applications?

Answer №1

Why start from scratch when you can utilize existing resources? Ionic offers an accordion component that is compatible with Ionic 3 applications.

Check it out here: Ionic Accordion

Q:

If I need to reuse a component and its service for sibling communication in another Ionic/Angular application, where should they be placed?

A:

The solution is straightforward with Ionic. Simply create a provider using the CLI command

ionic generate provider MyAccordion
. That's all there is to it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Prevent display of host attributes in Angular 2

My Angular component features a title property. // Here is the code snippet for the component: @Input('title') public title: string // This is how you can use the component in your code: <dialog [title]="bar"></foo> However, when t ...

Is the slow loading time of the Dev browser due to the large size of the vendor.js file

When using Angular 8.0, my browser takes around 15 seconds to load with ng serve. However, the browser only takes about 4 seconds to load when using ng serve --prod. One of the main reasons for the slow loading time in development is a vendor.js file tha ...

Transforming the elements within an array of objects into individual key-value pairs and then adding them to a fresh array

Hello, I am diving into the world of Typescript/Javascript and encountering some challenges when it comes to manipulating arrays of data. Please bear with me as I navigate through my learning curve. Imagine having an array of objects structured like this: ...

The sequence of activities within the Primeng Multiselect component

Lately, I've encountered an issue while using the multiselect component from the primeng library in Angular. Everything was going smoothly until I noticed a strange problem with the order of events. Here is an example that showcases the issue: https:/ ...

Differentiate the array type within an object in TypeScript

I understand how to specify the type for a variable. let members: string[] = [] // this example works flawlessly My goal is to have an array of strings within an object. How can I structure it correctly? const team = { name: String, members<st ...

Using type values in TypeScript

I am trying to assign interfaces as values within a config object: export interface RouterConfig { startEvents?: typeof RouterEvent[]; completeEvents?: typeof RouterEvent[]; } The intended usage is as follows: private config: RouterConfig = { star ...

In TypeScript, the term "Generic" is defined as a string that is generalized

There is a general function in my code that I have simplified to the example below: type GenericDelegate<Type extends "firstType" | "secondType"> = { type: Type; deleteItems: (query: { query: { [n in Type]: string } }) => P ...

I'm struggling with finding an answer to this question: "What is the most effective way to conduct a

I'm experimenting with a file upload. I decided to encapsulate the FileReader() inside an observable based on information I found in this discussion thread: onFileSelected(event: any) { this.importJsonFileToString(event.target.files[0]) .p ...

Exploring Array Iteration with RxJS' Filter and Map Functions

Can someone help me figure out the correct way to iterate over an array using map and filter in my Angular project? Currently, both methods expect a single value, but I am dealing with an Observable that contains an array. Any guidance on how to do this co ...

How to retrieve data from the resolve function in an Angular 2 component

How can I access the resolved data in an Angular 2 migrated component? I have an Angular JS app.config set up with routing that loads the necessary Angular 2 migrated component. .state('users.list', { url: '/users/list', templ ...

Retrieving and adding the content of a CSRF token created by Django within Angular applications

I am encountering difficulties with the CSRF integration between Django and Angular. I previously posted a more general inquiry here. The issue is that authenticated users cannot send post requests from the front-end, although they can do so successfully v ...

EventListener cannot be removed

My TypeScript class is structured like this: class MyClass { let canvas: any; constructor(canvas: any) { this.canvas = canvas; this.canvas.requestPointerLock = this.canvas.requestPointerLock; document.exitPointerLock = ...

What is the best way to adjust the output size for ngx-webcam?

I am looking to determine the smallest possible size for an image that is captured. From my testing with ngx-webcam, I have found that I can adjust the minimum height of the output image based on the display height of the webcam. Is there a method to set ...

The argument supplied in Angular using TypeScript is incompatible: a 'string' type cannot be assigned to a parameter expecting an 'Element' type

I'm facing 3 errors with Typescript in angular when working with D3js elements. I tried to create a mouseover event to display tag and value data corresponding to the bar graph, but encountered issues. I attempted declaring strings and even added "noI ...

Obtain unfinished designs from resolver using GraphQL Code Generator

In order to allow resolvers to return partial data and have other resolvers complete the missing fields, I follow this convention: type UserExtra { name: String! } type User { id: ID! email: String! extra: UserExtra! } type Query { user(id: ID! ...

Dimensions of Doughnut Chart in Chart.js

In my Angular project, I currently have two versions. The old production version uses an outdated version of ng2-charts, while I am working on upgrading it. Interestingly, I noticed a strange difference when using the doughnut chart from ng2-charts. When ...

Angular 2 is throwing an error, stating that Observable is not defined

I'm currently working with Observable and ChangeDetectionStrategy to notify other components about any changes that occur. However, I am encountering an issue where the Observable object addItemStream is coming up as undefined. Can anyone spot what mi ...

What is the best way to create a general getter function in Typescript that supports multiple variations?

My goal is to create a method that acts as a getter, with the option of taking a parameter. This getter should allow access to an object of type T, and return either the entire object or a specific property of that object. The issue I am facing is definin ...

Angular Paginated Table with Sort and Filter Functionality

I am just beginning to explore Angular and have covered the basics. I'm looking to develop a Table that will retrieve data from the server for loading. What approach would be best? I need to pull data from the server based on the number of r ...

Issues with MC-Cordova-Plugin on Ionic and Angular Setup

Recently, I integrated a plugin for Ionic from this repository: https://github.com/salesforce-marketingcloud/MC-Cordova-Plugin After successfully configuring it for iOS, I encountered difficulties on Android where the plugin seems to be non-existent. It ...