UNPKG

vue-notifications

Version:
454 lines (241 loc) 16.5 kB
<!DOCTYPE HTML> <html lang="" > <head> <meta charset="UTF-8"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Getting Started · GitBook</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="description" content=""> <meta name="generator" content="GitBook 3.2.3"> <link rel="stylesheet" href="gitbook/style.css"> <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css"> <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css"> <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css"> <meta name="HandheldFriendly" content="true"/> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png"> <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon"> <link rel="next" href="usage.html" /> <link rel="prev" href="installation.html" /> </head> <body> <div class="book"> <div class="book-summary"> <div id="book-search-input" role="search"> <input type="text" placeholder="Type to search" /> </div> <nav role="navigation"> <ul class="summary"> <li class="chapter " data-level="1.1" data-path="./"> <a href="./"> Introduction and WTF is it? </a> </li> <li class="chapter " data-level="1.2" data-path="installation.html"> <a href="installation.html"> Installation </a> </li> <li class="chapter active" data-level="1.3" data-path="getting-started.html"> <a href="getting-started.html"> Getting Started </a> </li> <li class="chapter " data-level="1.4" data-path="usage.html"> <a href="usage.html"> Usage </a> </li> <li class="chapter " data-level="1.5" data-path="what-you-shouldnt-do.html"> <a href="what-you-shouldnt-do.html"> What you shouldn't do </a> </li> <li class="chapter " data-level="1.6" data-path="advanced-setup.html"> <a href="advanced-setup.html"> Advanced Setup </a> </li> <li class="chapter " data-level="1.7" data-path="how-it-works.html"> <a href="how-it-works.html"> How it works </a> </li> <li class="chapter " data-level="1.8" data-path="browsers-support.html"> <a href="browsers-support.html"> Browsers support </a> </li> <li class="chapter " data-level="1.9" data-path="vuejs-versions-support.html"> <a href="vuejs-versions-support.html"> Vuejs Versions Support </a> </li> <li class="chapter " data-level="1.10" > <a target="_blank" href="https://github.com/se-panfilov/vue-notifications/releases"> Release Notes </a> </li> <li class="divider"></li> <li> <a href="https://www.gitbook.com" target="blank" class="gitbook-link"> Published with GitBook </a> </li> </ul> </nav> </div> <div class="book-body"> <div class="body-inner"> <div class="book-header" role="navigation"> <!-- Title --> <h1> <i class="fa fa-circle-o-notch fa-spin"></i> <a href="." >Getting Started</a> </h1> </div> <div class="page-wrapper" tabindex="-1" role="main"> <div class="page-inner"> <div id="book-search-results"> <div class="search-noresults"> <section class="normal markdown-section"> <h1 id="getting-started">Getting Started</h1> <p>Right after <a href="installation.html">installation</a>, please include <code>vue-notifications</code> lib into your project as following:</p> <pre><code class="lang-js"><span class="hljs-keyword">import</span> VueNotifications <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;vue-notifications&apos;</span> Vue.use(VueNotifications, options) </code></pre> <blockquote> <p><strong>Attention</strong>: By default <code>vue-notification</code> sends all messages <strong>to console</strong>. Just because <code>vue-notification</code> is a kind of bridge between your app and actual UI notification library. For example you can use <a href="https://github.com/se-panfilov/mini-toastr" target="_blank">mini-toastr</a> (<code>npm i mini-toastr --save</code>) as such UI library. Or any other if you want.</p> <p>That&apos;s the core idea - to have single notification interface but keep an ability to replace UI lib without change tons of code</p> </blockquote> <h2 id="setup-and-configuration-necessary">Setup and configuration (necessary)</h2> <p>As I said above - you have to use UI library that would draw actual notifications for you.</p> <p>For this example I will use <a href="https://github.com/se-panfilov/mini-toastr" target="_blank">mini-toastr</a></p> <h4 id="step-by-step-guide">Step-by-step guide</h4> <p>Let&apos;s do everything together</p> <blockquote> <p>If you don&apos;t want spend too much time with this shit - you can go ahead and copy-past whole code from the bottom of this page</p> </blockquote> <p>Anyway</p> <ul> <li>Import <code>vue-notifications</code></li> </ul> <p>Here we&apos;re including <code>vue-notifications</code> and <code>mini-toastr</code> in our project</p> <pre><code class="lang-js"><span class="hljs-keyword">import</span> VueNotifications <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;vue-notifications&apos;</span> <span class="hljs-keyword">import</span> miniToastr <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;mini-toastr&apos;</span> </code></pre> <p>P.S. don&apos;t forget to install <code>mini-toastr</code> (<code>npm i mini-toastr --save</code>)</p> <ul> <li>Setup types of the messages</li> </ul> <p>This one is mostly related to <code>mini-toastr</code>. We basically want <code>mini-toastr</code> to have these 4 types of messages. Basically &apos;error&apos; should be red and success - &apos;green&apos;</p> <pre><code class="lang-js"><span class="hljs-keyword">const</span> toastTypes = { success: <span class="hljs-string">&apos;success&apos;</span>, error: <span class="hljs-string">&apos;error&apos;</span>, info: <span class="hljs-string">&apos;info&apos;</span>, warn: <span class="hljs-string">&apos;warn&apos;</span> } </code></pre> <ul> <li>Activate <code>mini-toastr</code></li> </ul> <p>Here we make <code>mini-toasr</code> initialization with types from above</p> <pre><code class="lang-js">miniToastr.init({types: toastTypes}) </code></pre> <ul> <li>Map <code>vue-notification</code> to <code>mini-toastr</code></li> </ul> <p>We want our messages to be called via <code>vue-notification</code>but be shown by <code>mini-toastr</code>, So :</p> <pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toast</span> (<span class="hljs-params">{title, message, type, timeout, cb}</span>) </span>{ <span class="hljs-keyword">return</span> miniToastr[type](message, title, timeout, cb) } <span class="hljs-keyword">const</span> options = { success: toast, error: toast, info: toast, warn: toast } </code></pre> <p>This stuff will forward our messages, so in case of &apos;success&apos;, it will call <code>miniToastr.success(message, title, timeout, cb)</code>, in case of &apos;error&apos; it will call <code>miniToastr.error(message, title, timeout, cb)</code> and etc. Keep in mind that the types(like &quot;success&quot;, &quot;error&quot; and etc) could be whatever you want. In this example we just use default stuff for both libs.</p> <ul> <li>Activate the plugin </li> </ul> <p>Okay, now we have to pass our <code>options</code> into the plugin. Actually <code>vue-notification</code> has auto-install, but we want to pass options from above to it, so that&apos;s the case why do we do this manually</p> <pre><code class="lang-js">Vue.use(VueNotifications, options) </code></pre> <h4 id="all-together">All together</h4> <p>You can just copy-paste code below</p> <pre><code class="lang-js"><span class="hljs-keyword">import</span> VueNotifications <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;vue-notifications&apos;</span> <span class="hljs-comment">// Include mini-toastr (or any other UI-notification library)</span> <span class="hljs-keyword">import</span> miniToastr <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;mini-toastr&apos;</span> <span class="hljs-comment">// We shall setup types of the messages. (&apos;error&apos; type - red and &apos;success&apos; - green in mini-toastr)</span> <span class="hljs-keyword">const</span> toastTypes = { success: <span class="hljs-string">&apos;success&apos;</span>, error: <span class="hljs-string">&apos;error&apos;</span>, info: <span class="hljs-string">&apos;info&apos;</span>, warn: <span class="hljs-string">&apos;warn&apos;</span> } <span class="hljs-comment">// This step requires only for mini-toastr, just an initialization</span> miniToastr.init({types: toastTypes}) <span class="hljs-comment">// Here we are seting up messages output to `mini-toastr`</span> <span class="hljs-comment">// This mean that in case of &apos;success&apos; message we will call miniToastr.success(message, title, timeout, cb)</span> <span class="hljs-comment">// In case of &apos;error&apos; we will call miniToastr.error(message, title, timeout, cb)</span> <span class="hljs-comment">// and etc.</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toast</span> (<span class="hljs-params">{title, message, type, timeout, cb}</span>) </span>{ <span class="hljs-keyword">return</span> miniToastr[type](message, title, timeout, cb) } <span class="hljs-comment">// Here we map vue-notifications method to function abowe (to mini-toastr)</span> <span class="hljs-comment">// By default vue-notifications can have 4 methods: &apos;success&apos;, &apos;error&apos;, &apos;info&apos; and &apos;warn&apos;</span> <span class="hljs-comment">// But you can specify whatever methods you want.</span> <span class="hljs-comment">// If you won&apos;t specify some method here, output would be sent to the browser&apos;s console</span> <span class="hljs-keyword">const</span> options = { success: toast, error: toast, info: toast, warn: toast } <span class="hljs-comment">// Activate plugin</span> <span class="hljs-comment">// VueNotifications have auto install but if we want to specify options we&apos;ve got to do it manually.</span> Vue.use(VueNotifications, options) </code></pre> </section> </div> <div class="search-results"> <div class="has-results"> <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1> <ul class="search-results-list"></ul> </div> <div class="no-results"> <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1> </div> </div> </div> </div> </div> </div> <a href="installation.html" class="navigation navigation-prev " aria-label="Previous page: Installation"> <i class="fa fa-angle-left"></i> </a> <a href="usage.html" class="navigation navigation-next " aria-label="Next page: Usage"> <i class="fa fa-angle-right"></i> </a> </div> <script> var gitbook = gitbook || []; gitbook.push(function() { gitbook.page.hasChanged({"page":{"title":"Getting Started","level":"1.3","depth":1,"next":{"title":"Usage","level":"1.4","depth":1,"path":"usage.md","ref":"usage.md","articles":[]},"previous":{"title":"Installation","level":"1.2","depth":1,"path":"installation.md","ref":"installation.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"getting-started.md","mtime":"2019-05-27T14:52:57.446Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-08-01T12:24:20.020Z"},"basePath":".","book":{"language":""}}); }); </script> </div> <script src="gitbook/gitbook.js"></script> <script src="gitbook/theme.js"></script> <script src="gitbook/gitbook-plugin-search/search-engine.js"></script> <script src="gitbook/gitbook-plugin-search/search.js"></script> <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script> <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script> <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script> <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script> </body> </html>