Javascript 在vue中调用插件方法
我想实现一个插件,在其中我定义了一个抛出错误的方法。该方法将用于不同的组件。例如,如果一个按钮被点击,或者一个给定的值是,因为你的插件创建了一个mixin,你不需要将插件导入到你的组件中,通常你不会使用任何插件。但是有一些语法错误和缺少的Javascript 在vue中调用插件方法,javascript,vue.js,plugins,error-handling,Javascript,Vue.js,Plugins,Error Handling,我想实现一个插件,在其中我定义了一个抛出错误的方法。该方法将用于不同的组件。例如,如果一个按钮被点击,或者一个给定的值是,因为你的插件创建了一个mixin,你不需要将插件导入到你的组件中,通常你不会使用任何插件。但是有一些语法错误和缺少的方法选项。试试这个: const throwingeror={} throwingerro.install=函数(Vue){ 米辛({ 方法:{//如果您错过了这个,`LogEvent`是一个方法 LogEvent(event,msg){//此参数名称错误 试
方法
选项。试试这个:
const throwingeror={}
throwingerro.install=函数(Vue){
米辛({
方法:{//如果您错过了这个,`LogEvent`是一个方法
LogEvent(event,msg){//此参数名称错误
试一试{
如果(事件){
抛出新错误(msg)
}
}捕获(错误){
console.error(“错误消息:”,error.Message);//此语法错误
}
}
}
});
}
Vue.use(Throwinger错误);
/*****应用程序*****/
新Vue({
el:“应用程序”,
安装的(){
this.LogEvent({},'test');//缺少方法名
}
});
Vue.config.productionTip=false代码>
单击以触发ThrowinError方法
谢谢你的回答,丹!经过一些小的改变后,它对我起了作用。我的插件声明了一些全局方法。在我尝试了你推荐的方法后,它停止了工作。解决方案是只导出整个插件。在main.js中,我必须导入并编写Vue.use(插件)
const throwingError = {}
throwingError.install = function (Vue){
Vue.mixin({
LogEvent(value, msg){
try {
if(event){
throw new Error(msg)
}
} catch (error) {
console.error("Error Message": error.message)
}
}
});
}
export default throwingError
import throwingError from "./plugin/throwingError
Vue.use(throwingError)
<template>
<div id="app">
<button v-on:click="LogEvent($event, 'hi')">
Click to trigger throwingError method
</button>
</div>
</template>
<script>
import throwingError from './plugins/throwingError'
export default{
mounted() {
throwingError(event, "test")
},
};
</script>