Mic*_*elT 0 javascript vue.js vuex vuejs2 bootstrap-vue
我对 vue.js 很陌生,但我可以弄清楚一些事情。我从普通的 js 开始,但现在切换到带有类样式 vue 组件的 typescript。对于组件样式,我使用 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')
Run Code Online (Sandbox Code Playgroud)
在商店中我动态注册了一个 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常。我可以在生成通知的 vue 组件中获取此商店的实例,但是在以下 vue 组件中创建 Toast 时遇到了问题。
通知.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>
Run Code Online (Sandbox Code Playgroud)
尽管它编译了,但在文件 Vetur 中,这里给了我以下错误。但没有生产和展示敬酒。
“UserNotification”类型上不存在属性“$bvToast”。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>
Run Code Online (Sandbox Code Playgroud)
你对我做错了什么有什么建议吗?谢谢
问题已解决或效果更好
我真的不知道为什么,但它现在有效!无需进一步更改。对不起,我不能再复制这个了。此外,webconsole 也没有显示任何错误。
所以看起来我可以在我的 UserNotification 组件中访问 vue 实例。UserNotification 扩展了 Vue,因此它是一个 Vue 实例而不是 VueX。答案中描述的解决方案是不必要的。
难道只有 Vetur 无法将 $vbToast 识别为 UserNotification 的属性,甚至无法识别打字稿上下文中的扩展 Vue 实例吗?
thisVueX的上下文不是 Vue 实例,因此this.$bvToast不能直接使用。
但是,有一种解决方法可以访问 VueX 中的主 Vue 实例。使用this._vm.$bvToast来代替。
看:
| 归档时间: |
|
| 查看次数: |
7118 次 |
| 最近记录: |