Vue M Message
vue-m-message is a lightweight and configurable message/toast plugin designed specifically for Vue.js applications. The current stable version, v4.0.2, exclusively supports Vue 3, having been fully refactored with TypeScript since v4.0.0. For Vue 2 projects, users must install the v3.x series. It offers various message types (info, success, error, warning, loading), supports custom icons, HTML content, and provides 7 distinct positioning options. It also allows for programmatic closing and setting global default options, differentiating itself through its explicit Vue 3 focus, modern composition API integration, and flexible content rendering, including VNode support, without significant external dependencies.
Common errors
-
[Vue warn]: Failed to resolve component: message
cause This typically occurs when attempting to use vue-m-message v4.x with a Vue 2 project, or if the `Message` plugin has not been correctly registered with `app.use()` in a Vue 3 project.fixIf using Vue 2, downgrade to `npm install vue-m-message@3`. If using Vue 3, ensure you have called `app.use(Message)` in your main application entry point before mounting the app. -
TypeError: Cannot read properties of undefined (reading 'info')
cause The `Message` object or its static methods (like `info`, `success`) are being called before the `vue-m-message` plugin has been imported or initialized, or the `Message` object is not correctly referenced.fixVerify that `import Message from 'vue-m-message'` is present and correct, and that `app.use(Message)` has been executed before any calls to `Message.info()` or other methods. Ensure you are calling `Message.info()` directly, not `this.$message.info()` unless you've configured a global property. -
Message styles are not applied, messages appear unstyled.
cause The required CSS file for `vue-m-message` has not been imported into your project.fixAdd `import 'vue-m-message/dist/style.css'` to your application's main entry file (e.g., `main.ts` or `main.js`).
Warnings
- breaking Version 4.x of vue-m-message is exclusively compatible with Vue 3. Projects using Vue 2 must install the v3.x series.
- breaking The library was fully refactored with TypeScript starting from v4.0.0. While providing better type safety, this might introduce minor type-related changes or require stricter type adherence in consuming TypeScript applications.
- gotcha The functionality of the `closable` and `duration` attributes was modified in version 4.0.1. Specifically, `duration: -1` now ensures a message remains open indefinitely until manually closed (if `closable` is true), and `closable` enables the close button.
Install
-
npm install vue-m-message -
yarn add vue-m-message -
pnpm add vue-m-message
Imports
- Message
import { Message } from 'vue-m-message'import Message from 'vue-m-message'
- CSS
import 'vue-m-message/dist/style.css'
- Message.info
this.$message.info('Your message here')Message.info('Your message here') - MessageOptions (Type)
import type { MessageOptions } from 'vue-m-message'
Quickstart
import { createApp, h } from 'vue';
import Message from 'vue-m-message';
import 'vue-m-message/dist/style.css';
const App = {
setup() {
const showRichMessage = () => {
Message.info(() => h('div', [
'Here is a ',
h('strong', 'rich content'),
' message for ',
h('a', { href: 'https://github.com/mengdu/m-message', target: '_blank' }, 'Vue M Message'),
' plugin. It will stay open until manually closed.'
]), {
title: 'Welcome to Vue M Message!',
duration: -1, // -1 means it needs to be closed manually
iconURL: 'https://avatars.githubusercontent.com/u/11366654?s=40&v=4', // Custom icon
closable: true // Allow manual closing
});
};
// Display a message shortly after the app mounts
setTimeout(showRichMessage, 1000);
return () => h('div', [
h('h1', 'Vue M Message Quickstart'),
h('p', 'Check the top-right corner for a message.'),
h('button', { onClick: showRichMessage, style: { padding: '10px 20px', fontSize: '16px' } }, 'Show Message Again')
]);
}
};
const app = createApp(App);
// Install the message plugin globally
app.use(Message, {
name: '$mMessage', // Optional: customize the global property name
defaultOptions: {
position: 'top-right',
zIndex: 2000
}
});
app.mount('#app');