我有一个 js 文件:
function library_f() {
}
function some_f() {
library_f();
}
function another_f() {
library_f();
}
但代码看起来很难看,我决定将我的 js 文件拆分为三个:
one.js:
function library_f() {
}
两个.js:
function some_f() {
library_f();
}
three.js:
function another_f() {
library_f();
}
但是现在我得到了错误
library_f() is not defined
如何设置包含我的 js 文件的顺序?
最佳答案
来自docs :
Files in the lib directory at the root of your application are loaded first. Files that match main.* are loaded after everything else.
Files in subdirectories are loaded before files in parent directories, so that files in the deepest subdirectory are loaded first (after lib), and files in the root directory are loaded last (other than main.*).
Within a directory, files are loaded in alphabetical order by filename.
These rules stack, so that within lib, for example, files are still loaded in alphabetical order; and if there are multiple files named main.js, the ones in subdirectories are loaded earlier.
但看起来这些函数无法访问,因为它们不是全局的。在 meteor 中,每个文件的变量/函数不能被另一个文件访问,除非变量或函数是全局的。
所以你需要这样声明你的函数:
library_f = library_f() {
...
}
以便其他文件可以访问它。变量也是如此:
var x = true; //Not accessible by other files
x = true; //Accessible by other files
var dothis = function () {...} //Not accessible by other files
dothis = function() {..} //Not accessible by other files
function dothis() {..} //Not accessible ny other files
关于javascript - Meteor 包含我的 js 文件的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16166509/