【发布时间】:2020-09-28 01:09:17
【问题描述】:
我的索引和服务插件的结构如下:
service.ts
declare interface Params {
title: string;
description?: string;
type?: string;
duration?: number;
}
export default class ServiceToast {
public toastRef: any; // component
public constructor(modalRef: any) {
this.toastRef = modalRef;
console.log(this.toastRef);
}
public open(params: Params) {
this.toastRef.open(params);
}
}
基本上我已经创建了一个接收组件的服务,以便它与组件轻松交互。
index.ts:
import _Vue from 'vue';
import Toast from '@/components/_includes/layouts/Toast.vue';
import ServiceToast from './service';
const ToastPlugin = {
install: (Vue: typeof _Vue, options?: any) => {
Vue.mixin({
created() {
Vue.prototype.$toast = new ServiceToast(Toast);
},
});
},
};
export default ToastPlugin;
我在哪里安装插件并使用上面显示的服务。
这里我调用插件并使其成为对应的 Vue.use 但是当我想在任何组件中调用它时:
<a @click="$toast.open({ title: 'Hola mundo' })">Hola, dame click!</a>
我收到以下错误:“TypeError:this.toastRef.open 不是函数”
En el shims-vue-plugin.d.ts:
/* eslint-disable */
import Vue from 'vue';
import { AxiosInstance } from 'axios';
import 'webpack-env';
import { FieldFlagsBag } from 'vee-validate';
import { SnackbarProgrammatic as Snackbar, DialogProgrammatic as Dialog } from 'buefy';
import ServiceToast from './app-config/toast/service';
declare module 'vue/types/vue' {
interface Vue {
$auth: any;
$axios: AxiosInstance;
veeFields: FieldFlagsBag;
$dialog: typeof Dialog;
$snackbar: typeof Snackbar;
$toast: ServiceToast;
}
}
declare namespace NodeJS {
interface Process extends __WebpackModuleApi.NodeProcess {
server: boolean;
}
}
有谁知道它可能是什么?或者我因为找不到错误而错过了:/
【问题讨论】:
-
@PeiroPajares 您是否在控制台中检查了
this.$toast的值是多少?我不确定你在导入什么import Toast from '@/components/_includes/layouts/Toast.vue'?能分享一下 github/codesandbox 项目吗?
标签: typescript vue.js plugins vuejs2