主 Vue 实例中的自定义 js 库(scrollMonitor)与内部组件共享 [英] Custom js library(scrollMonitor) inside main Vue instance to be shared with inner components

查看:22
本文介绍了主 Vue 实例中的自定义 js 库(scrollMonitor)与内部组件共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 Vue.js 问题,通常我试图在我的 .vue 实例中使用scrollMonitor"函数(通过 ma​​in.js 导入),但它给了我一个典型的this".scrollMonitor 不是函数的错误

This is Vue.js question, generally I'm trying to use 'scrollMonitor' function inside of my .vue instance(imported via main.js) but it gives me a typical 'this.scrollMonitor is not a function' error

mounted () {
    let watcher = this.$scrollMonitor(this.$refs.nicer)
}

ma​​in.js 中 ScrollMonitor 库似乎已正确导入(控制台显示预期内容):

In main.js ScrollMonitor library seems to be properly imported(console shows what's expected):

import scrollMonitor from 'scrollmonitor'
Vue.use(scrollMonitor)
console.log(scrollMonitor)

同样的主要目标是在 .vue 文件中使用 scrollMonitor 功能(在 vue 组件实例中).对不起,如果我在这里遗漏了一些愚蠢的东西 - 我已经在那个文件中使用了一些其他的库,比如 Vue-Resource 所以问题不在文件路径"中,而是在我使用 scrollMonitor 功能的方式中,非常感谢任何帮助,谢谢!

Again main goal is using scrollMonitor functionality inside of .vue file(in vue component instance). Sorry if I'm missing something silly here - I'm already using some other libraries like Vue-Resource in that file so issue is not in 'filepath' but rather in the way I'm using scrollMonitor functionality, any help is much appreciated, thank you !

推荐答案

您是否在使用普通的 javascript 库并尝试 Vue.use 它?那真的行不通.Vue.use 仅适用于设计用于 Vue 的插件.将库导入到需要的组件中,然后在那里使用它.

Are you using a plain javascript library and trying to Vue.use it? That won't really work. Vue.use will only work with plugins designed to work with Vue. Import the library into the component that needs and and just use it there.

scrollMonitor(this.$refs.nicer)

这篇关于主 Vue 实例中的自定义 js 库(scrollMonitor)与内部组件共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆