Ember CLI Flash Messages
ember-cli-flash is a robust Ember addon that provides a simple yet highly configurable solution for displaying flash messages (e.g., growl, toast notifications) within Ember applications. It includes both a `flashMessages` service for managing messages and a `FlashMessage` component for rendering them. Currently in stable version 7.0.0, the library generally follows a yearly major release cycle, with minor and patch updates in between, aligning with Ember's LTS and general release schedule. Key differentiators include its promise-aware API, out-of-the-box styling support for frameworks like Bootstrap and Foundation, and comprehensive TypeScript definitions allowing for custom message fields with generics, making it type-safe and adaptable to complex notification requirements. It integrates well with modern Ember tooling, requiring Ember.js v4.12 or above and Embroider or ember-auto-import v2.
Common errors
-
Error: Could not find module `@ember/service` imported from `your-app/components/my-component`
cause Old Ember CLI versions or an incorrect setup preventing module resolution for `@ember/service`.fixEnsure your Ember application is compatible with Ember.js v4.12+ and uses `ember-auto-import v2` or Embroider. Update your `package.json` and install necessary dependencies. -
TypeError: this.flashMessages.success is not a function
cause The `flashMessages` service was not properly injected or is undefined in the current context.fixEnsure you have `import { service } from '@ember/service';` and are using `@service declare flashMessages: FlashMessagesService;` (or `@service flashMessages;` for JS) in your component or controller class. -
Error: Assertion Failed: The component `<FlashMessage::Component/>` expects a `message` argument, but it was not provided.
cause Incorrect usage of the `<FlashMessage::Component />` without a service providing messages, or attempting to pass a message directly when it expects to consume from the service.fixEnsure the `<FlashMessage::Component />` is placed in a template where it can access the `flashMessages` service implicitly (e.g., `application.hbs`), and that you are adding messages via `this.flashMessages.add()` or its convenience methods. -
Property 'onClose' does not exist on type 'FlashMessagesService'.
cause Attempting to use a custom `on*` property (like `onClose`) directly on the `FlashMessagesService` instance without declaring it in your custom flash message type for TypeScript.fixWhen using custom fields like `onClose`, define a generic type parameter for `FlashMessagesService` and `FlashMessage` component, e.g., `type MyFlashOptions = { onClose?: () => void }; @service declare flashMessages: FlashMessagesService<MyFlashOptions>;`.
Warnings
- breaking In `v4.0.0`, automatic injection of the `flashMessages` service was removed. Developers must now manually inject the service using `@service flashMessages: FlashMessagesService;`. Additionally, the `flash-message` component was converted to a Glimmer component, which might require template syntax adjustments if relying on classic Ember component behaviors.
- breaking Version `6.0.0` introduced significant internal modernizations, converting `FlashMessagesService` and `FlashObject` to native JavaScript classes. While the public API aimed to remain largely compatible, custom extensions or deep integrations relying on older Ember object models might require adjustments.
- breaking With `v5.0.0`, `@embroider/macros` became a peer dependency, meaning it must be explicitly installed by the consuming application. The addon's default blueprint was also removed, replacing it with explicit test helpers, which might affect initial setup or testing strategies.
- gotcha Version `7.0.0` fixed issues allowing custom `on*` properties and made `removeBy()` immediate. While a bug fix, developers relying on previous, potentially buggy `on*` property behavior or synchronous `removeBy()` might notice subtle changes in message lifecycle or removal timing.
Install
-
npm install ember-cli-flash -
yarn add ember-cli-flash -
pnpm add ember-cli-flash
Imports
- FlashMessagesService
import type { FlashMessagesService } from 'ember-cli-flash'; - service
import service from '@ember/service';
import { service } from '@ember/service'; - Component
const Component = require('@glimmer/component');import Component from '@glimmer/component';
Quickstart
import Component from '@glimmer/component';
import { service } from '@ember/service';
import { action } from '@ember/object'; // Or @glimmer/tracking for reactivity if needed
import type { FlashMessagesService } from 'ember-cli-flash';
export default class MyComponent extends Component {
@service declare flashMessages: FlashMessagesService;
@action
showSuccessMessage() {
this.flashMessages.success('Data saved successfully!', {
timeout: 3000,
sticky: false,
type: 'success', // For Bootstrap/Foundation styling
// Custom options are also supported
user: 'currentUser',
priority: 100
});
}
@action
showErrorMessage() {
this.flashMessages.danger('Failed to save data. Please try again.', {
timeout: 5000,
sticky: true,
showProgress: true
}).then(flashObject => {
console.log('Flash message dismissed or timed out:', flashObject.message);
});
}
@action
clearAllMessages() {
this.flashMessages.clearMessages();
}
}
// In your application template (e.g., application.hbs), typically:
// <FlashMessage::Component />
// Ensure your app's CSS includes styles for .alert, .alert-success, etc.