简体   繁体   English

如何在创建VueJS实例之前加载外部托管(CDN)JS库*?

[英]How do I load an externally hosted (CDN) JS library *before* the VueJS instance is created?

I'm trying to integrate Material Design Lite in a VueJS application. 我正在尝试将Material Design Lite集成到VueJS应用程序中。 I found the following blog-post: 我找到了以下博文:

https://posva.net/js/2015/08/26/using-material-design-lite-with-vuejs https://posva.net/js/2015/08/26/using-material-design-lite-with-vuejs

Unfortunately, when I add this to my "main.js" file created from the vuejs cli tool I get the following error: 不幸的是,当我将这个添加到我从vuejs cli工具创建的“main.js”文件时,我收到以下错误:

ERROR in ./src/main.js

  ✘  http://eslint.org/docs/rules/no-undef  'componentHandler' is not defined  
  /data/src/main.js:474:5
      componentHandler.upgradeElement(this.el)

I pushed a branch to github so the code is visible. 我将一个分支推送到github,因此代码可见。 It contains: 它包含:

I'm fairly certain that MDL is loaded after the VueJS app is loaded, so the reference does not exist. 我很确定在加载VueJS应用程序加载了MDL,因此引用不存在。

How can I make sure VueJS is only loaded after the dependencies are available? 如何确保仅依赖项可用后才加载VueJS?

Or, as I'm using webpack to build the code: Is there another way to integrate MDL? 或者,因为我正在使用webpack来构建代码:是否有另一种方法来集成MDL? Maybe packing it up into the application? 也许将它打包到应用程序中?

That is a linting error not an actual code error. 这是一个linting错误而不是实际的代码错误。 The linter couldn't find a method/variable called componentHandler . linter找不到名为componentHandler的方法/变量。 Doesn't necessarily mean that the code doesn't work, try using window.componentHandler to get the linter to stop complaining. 并不一定意味着代码不起作用,尝试使用window.componentHandler来获取linter以停止抱怨。 You could also look into linter ignore rules to ignore that specific line and see if it builds then. 您还可以查看linter ignore规则以忽略该特定行,然后查看它是否构建。

The linter runs in the build process in the webpack vue template, so the build will fail since it couldn't find a the reference you used in your vue codebase. linter在webpack vue模板的构建过程中运行,因此构建将失败,因为它找不到您在vue代码库中使用的引用。 The linter doesn't know about the scripts you load externally in your html files. linter不知道你在html文件中外部加载的脚本。

As Bert said, I would also remove the defer 正如Bert所说,我也会删除defer

If you are unfamiliar with linters, all it does is look over your js code and check that you don't use undefined variables (like in this case), or use wrong indentation, etc. The vue template put the linter in the build process so you won't get a proper build until you have fixed everything the linter wants you to fix. 如果您不熟悉linters,它所做的只是查看您的js代码并检查您是否使用未定义的变量(如本例中),或使用错误的缩进等.vue模板将linter放入构建过程中因此,在修复了linter要求您修复的所有内容之前,您将无法获得正确的构建。 You could disable linting entirely, but I recommend against it as it improves overall code quality if you keep it around imo. 您可以完全禁用linting,但我建议不要使用它,因为它可以提高整体代码质量,如果你保持它的imo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 VueJS CDN 遍历外部 JS 文件中的数组? - How do I loop through an array in an external JS file using VueJS CDN? 加载CDN上托管的JS库的本地版本 - Load local versions of the JS libraries hosted on a CDN 如果库没有 CDN,如何在不下载的情况下包含 JS 库? - How do I include a JS library without downloading it, if library do not have a CDN? 外部托管时VueJS不起作用 - VueJS not working when hosted externally 如何在 CDN VueJS 应用程序中加载信号 - how to load signal in CDN VueJS application 如何将jquery库和js文件加载到wordpress主题中? - How do I load jquery library and a js file into wordpress theme? 如何在 Svelte/Sapper 中加载外部 JS 库? - How do I load an external JS library in Svelte/Sapper? 在Require.js优化期间如何使用本地文件,但在运行时如何使用CDN托管的版本? - How can I use a local file during Require.js optimisation, but a CDN-hosted version at runtime? 如何覆盖CDN(其他地方)中托管的javascript库所需的文件? - How to overwrite a file required by a javascript library hosted in a CDN (elsewhere)? 在VueJS中,如何根据我的JS中填充的内容在视图中使用load? - In VueJS, how do I use load in a view based on what is populated in my JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM