在类里面main.js
:
function readImage() {
if (window.File && window.FileList && window.FileReader) {
var files = event.target.files; //FileList object
for (let i = 0; i < files.length; i++) {
var file = files[i];
if (!file.type.match('image')) continue;
//* * * call function in storage class
uploadPhoto(file);
然后我启动了一个简单的新 js 文件,将上传功能移入其中。
在storage.js
function uploadPhoto(file)
{
// create ref for fireStorage etc
var storageRef = firebase.storage().ref();
// ...more code using firebase with error every time i use it
错误:
在类里面
storage
:uploadPhoto is defined but never used
每次使用
firebase
时都会出错:not defined
,如果我使用firebase
在main.js
class 它确实有效 - 没有错误(那么为什么不在 storage.js 中呢?)
在我的 html
两者都已定义:
//firebase is defined here...
<script src="javascripts/main.js"></script>
<script src="javascripts/storage.js"></script>
我需要在 storage.js
中导入一些东西吗?让它识别世界其他地方?
最佳答案
为了防止在评论中进行过多的讨论,我将仔细阅读所有已经说过的内容,然后向您解释其含义。
- 您描述的第一个错误是
uploadPhoto is defined but never used
.这意味着您定义uploadPhoto
的位置, 这是 永远不会被调用——这很好,因为你是 在main.js
中调用它.您可以忽略此错误。
You should not be able to get such an error without additional tooling, such as a Linter, that checks your code before it runs. Tools like those would most probably give you these warnings in your editor, and not inside a browser.
- 您提到的第二个错误是您可以使用
firebase
在你的main.js
文件,但不在另一个文件中。这不应该是这种情况,很可能是基于firebase
的事实。当您尝试在storage.js
中使用它时尚不可用 ,即它仍在加载。要解决此问题,请在调用storage.js
中的函数时进行说明以及您的程序流程如何执行。
关于javascript - 在函数/类之间传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53773671/