I am fairly new to vue.js but I have managed to figure out some things. I initially started with regular js, but then transitioned to typescript with class style vue components. For styling the components, I rely on bootstrap-vue.
In my main.ts file, I imported bootstrap along with the vuex store
...
import BootstrapVue from 'bootstrap-vue'
//use custom bootstrap styling
import '../src/bootstrap/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
Vue.use(BootstrapVue)
...//some more code
new Vue({
router,
store,
i18n,
render: h => h(App)
}).$mount('#app')
Within the store, I dynamically register a NotificationModule
NotificationModule.ts
//import node modules
import { Module, VuexModule, Mutation, Action } from "vuex-module-decorators";
import { store } from "@/store";
//import application modules
import i18n from '@/i18n';
import Notification from '../types/Notification';
import Message from '../types/Message';
import logger from '@/system/logger';
@Module({
dynamic: true,
store: store,
name: "notification",
namespaced: true,
})
export default class NotifcationModule extends VuexModule {
//notification types definition with according prompts
notificationTypes: Array<string> = ['info', 'success', 'warning', 'danger'];
//definition of the notification object
notification: Notification = {
variant: '',
prompt: '',
message: ''
}
/**
* prove the supported notification types
*/
get getSupportedNotificationTypes(): Array<string> {
return this.notificationTypes;
}
/**
* provide the notification
*/
get getNotification(): Notification {
return this.notification;
}
@Action
notify(msg: Message){
logger.warn(`called notify with [type:${msg.variant}]:[text:${msg.text}]`);
if(msg.variant === undefined || !Array.from(this.notificationTypes).includes(msg.variant)){
msg.variant = 'info';
}
//configure custom notification data
const notification = {
variant: msg.variant,
prompt: i18n.t(`notify.${msg.variant}`),
message: msg.text || 'No message provided.'
}
this.context.commit('setNotification', notification);
}
@Mutation
public setNotification(data: Notification) {
if (data) {
this.notification = data;
}
}
}
Everything is working fine so far. I can retrieve an instance of this store in the vue-component responsible for producing notifications. However, I am encountering an issue when trying to create a toast in the subsequent vue component.
Notification.vue
<template>
<b-container></b-container>
</template>
<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator';
import { getModule, VuexModule } from 'vuex-module-decorators';
import NotificationModule from '../../util-notification/components/NotificationModule';
import Notification from '../types/Notification';
import logger from '../../../system/logger';
@Component
export default class UserNotification extends Vue {
//get the instance of the NotificationModule
noticationInstance: NotificationModule = getModule(NotificationModule);
//watch the property 'notification' - accessed via getter method
@Watch('noticationInstance.getNotification')
onPropertyChange(notification: Notification, oldValue: string) {
logger.debug(`replaced [${JSON.stringify(oldValue)}] by [${JSON.stringify(notification)}]`);
//create a toast
this.$bvToast.toast(notification.message, {
title: notification.prompt || 'Info',
variant: notification.variant || 'warning',
solid: true
});
}
}
</script>
Vetur gives me an error within this file, even though it compiles successfully. Unfortunately, no toasts are being produced or shown.
Property '$bvToast' does not exist on type 'UserNotification'.Vetur(2339)
While testing a different approach in the TestView where I integrated $bvToast differently, it works perfectly there.
<template>
<b-container fluid>
<h1>This is the page for testing components and functionality</h1>
<hr>
<div>
<h3>Trigger for vuex notification test</h3>
<b-button @click="$bvToast.show('example-toast')" class="mb-2">Default</b-button>
</div>
<b-toast id="example-toast" title="BootstrapVue" static no-auto-hide>
Hello, world! This is a toast message.
</b-toast>
</b-container>
</template>
Any suggestions on what might be going wrong here? Thank you
Problem solved, everything's working now!
Surprisingly, it just started working without any further changes. Apologies, as I am unable to reproduce the issue anymore. Additionally, the web console shows no errors either.
It appears that I can access the vue instance within my UserNotification component. Since UserNotification extends Vue, it is a Vue instance and not VueX. The solution described in the answer turned out to be unnecessary.
Could it be possible that Vetur simply doesn't recognize $vbToast as a property of UserNotification or the extended Vue instance in the Typescript context?