Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASK] How to call notify from js files #34

Open
christhofer opened this issue Dec 7, 2018 · 0 comments
Open

[ASK] How to call notify from js files #34

christhofer opened this issue Dec 7, 2018 · 0 comments

Comments

@christhofer
Copy link

christhofer commented Dec 7, 2018

From the readme, to show the notification we can use

this.$notify({
  message: 'Welcome',
  type: 'success'
})

how to do this in .js files? e.g I have api.js and when the API call returns error, I want to show the notification.
Tried these but it doesn't work

import Vue from 'vue'
// some API call
Vue.prototype.$notify({message: 'some error', type: 'danger'})
/* Cannot read property 'notify' of undefined */
import Vue from 'vue'
import Notify from 'vue-notifyjs'
import '@/assets/css/themify-icons.css'
import 'vue-notifyjs/themes/default.css'

// some API call
Notify({message: 'some error', type: 'danger'})
/* vue_notifyjs__WEBPACK_IMPORTED_MODULE_2___default(...) is not a function */

Also I'm thinking of putting the initialization in a single js file (notification.js), which then imported to main.js

import Vue from 'vue'

import Notify from 'vue-notifyjs'
import '@/assets/css/themify-icons.css'
import 'vue-notifyjs/themes/default.css'

Vue.use(Notify, {
	timeout         : 3000,
	verticalAlign   : 'bottom',
	horizontalAlign : 'right',
})

Vue.prototype.$notif = {
	success: (message, title = 'Success') => {
		Notify({
			message,
			title,
			type : 'success',
			icon : 'fas fa-check-circle fa-fw',
		})
	},
	error: (message, title = 'Error') => {
		Notify({
			message,
			title,
			type : 'danger',
			icon : 'fas fa-times-circle fa-fw',
		})
	},
	warning: (message, title = 'Warning') => {
		Notify({
			message,
			title,
			type : 'warning',
			icon : 'fas fa-exclamation-circle fa-fw',
		})
	},
	info: (message, title = 'Info') => {
		Notify({
			message,
			title,
			type : 'info',
			icon : 'fas fa-info-circle fa-fw',
		})
	},
}

export default {}

So that in the template I can just call
this.$notif.success("message")

or in the Vuex modules / api.js
Vue.prototype.$notif.error(error.response.data)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant