Как получить доступ к $ bvToast в компоненте класса vue - PullRequest
0 голосов
/ 22 января 2020

Я довольно новичок в vue. js, но я могу кое-что выяснить. Я начал с обычного js, но теперь переключился на машинопись с компонентами стиля vue. Для стилей компонентов я использую bootstrap - vue.

в своем импортированном файле main.ts bootstrap, а также в хранилище vuex

...
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')

В магазине я динамически регистрирую 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;
      }
  }
}

, который все работает. Я могу получить экземпляр этого магазина в vue -компоненте, который генерирует уведомления, но у меня возникла проблема, создав тост в следующем vue компоненте.

Уведомление. 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 выдает мне следующую ошибку, хотя и компилируется. Но тосты не создавались и не показывались.

Свойство '$ bvToast' не существует для типа 'UserNotification'.Vetur (2339)

Я создал TestView, где я по-разному интегрировал $ bvToast, и он работает.

<template>
  <b-container fluid>
    <h1>This is the page for testing components and functionality</h1>
    <hr>
    <div>
      <h3>Trigger für 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>

Есть ли у вас какие-либо предположения о том, что я делаю неправильно? Спасибо вам

Проблема решена или лучше работает

Я не знаю почему, но это работает сейчас! без дальнейших изменений. Извините, я не могу больше это воспроизвести. Также webconsole также не показывает никаких ошибок.

Так что, похоже, я могу получить доступ к экземпляру vue в моем компоненте UserNotification. UserNotification расширяет Vue, поэтому это Vue экземпляр, а не VueX. Раскрытое решение из ответа не было необходимым.

Возможно ли, что только Vetur не распознает $ vbToast как свойство UserNotification или даже расширенный экземпляр Vue в контексте Typscript?

1 Ответ

2 голосов
/ 23 января 2020

Контекст this в VueX не является экземпляром Vue, поэтому this.$bvToast не доступен напрямую.

Однако существует обходной путь для доступа к основному экземпляру Vue в VueX. Вместо этого используйте this._vm.$bvToast.

См .:

...