gpt4 book ai didi

javascript - 重组 Meteor App

转载 作者:行者123 更新时间:2023-11-28 01:16:41 27 4
gpt4 key购买 nike

我正在重组我的 meteor 应用程序,突然每个逻辑(JS 脚本)与初始文件分离的 View (html 模板)都停止工作。

最初我的项目看起来像这样

-project
-.meteor
-client
-templateA.html
-templateB.html
-templateC.html
-client.js <-- contain JS for all template
-display.css
-server
-server.js
-model.js <--collections
-router.js <-- iron-router router

但是随着项目的增长,我很难在单个文件中管理整个 js 代码。所以我决定将其分成更小的模块。于是就变成了这样

 -project
-.meteor
-client
-views
-templateA.html
-tempplateA.js
-templateB.html
-templateB.js
-templateC.html
-client.js <---templateC JS is still in client.js
-stylesheets
-display.css

-server
-server.js
-model.js
-router.js

每个 View (模板)都不起作用。浏览器仅生成空白页面。不过,我设法生成了 templateC。所以我的推论是,templateC 可以工作,因为 JS 仍然在原始文件中。

我应该怎样做才能使其适用于其余模板?

最佳答案

/client/compatibility 是解决您问题的关键。

您在 JS 文件中定义的变量是该文件的本地变量。 See this section.

 

<小时/>

 

基本上,Meteor 使用 (function(){ ... })(); 模式包装每个单独的文件。这意味着当您定义:

var something = {};

在一个文件中,某些内容在另一个文件中未定义。如果您想创建一个在整个应用程序中共享的变量,您需要将其设为全局变量。幸运的是,这很简单 - 只需删除 var 关键字即可:

something = {};

现在您可以在代码中的任何位置使用something

关于javascript - 重组 Meteor App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23751043/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com