javascript 在 Ionic 中哪里包含 js 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26496451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to include js libraries in Ionic?
提问by FalsePozitive
thanks for your time! I am battling to get any scripts running in the Ionic framework's sidemenu starter template. I have placed them everywhere (except where they're supposed to be, clearly) but to no avail. I have tried loading them before ionic and also using a lower version of jquery. I have built a decent mobile site with jquery that I've pushed through phonegap and it works perfectly but I would like to use Ionic as a personal challenge. However, I can't seem to get any of the scripts I'm using to work.
谢谢你的时间!我正在努力让任何脚本在 Ionic 框架的 sidemenu starter 模板中运行。我已经把它们放在任何地方(除了它们应该在的地方,很明显)但没有用。我试过在 ionic 之前加载它们,也尝试过使用较低版本的 jquery。我已经使用 jquery 构建了一个不错的移动网站,我已经通过 phonegap 推送了它并且它运行良好,但我想使用 Ionic 作为个人挑战。但是,我似乎无法获得我用来工作的任何脚本。
Do I include them in the main index file and if so, do they get loaded when each template instantiates? Or must I include them in each of the templates? If you could please be quite specific as to where in the folders I should place them (if indeed there is a specific place) and also where I should call the scripts.
我是否将它们包含在主索引文件中,如果是,它们是否会在每个模板实例化时加载?或者我必须将它们包含在每个模板中?如果您可以非常具体地说明我应该将它们放在文件夹中的哪个位置(如果确实有一个特定的位置)以及我应该在哪里调用脚本。
I installed ionic using this yeoman generator https://github.com/diegonetto/generator-ionicif that helps.
如果有帮助,我使用这个 yeoman 生成器https://github.com/diegonetto/generator-ionic安装了 ionic 。
If I have note been clear enough please comment and I will elaborate further.
如果我注意到的足够清楚,请发表评论,我会进一步详细说明。
Thank you for your patience - J
感谢您的耐心等待 - J
回答by Ali Cheaito
I know this is old, but for those who are struggling with this for Ionic 2, add script files to src/assets/scripts, then refer to them via a script tag in src/index.html (not the one in www). When it builds, everything in src/assets will be copied to www/assets, including your scripts. index.html will also be copied to www.
我知道这是旧的,但对于那些为 Ionic 2 苦苦挣扎的人来说,将脚本文件添加到 src/assets/scripts,然后通过 src/index.html 中的脚本标签(而不是 www 中的标签)引用它们。构建时,src/assets 中的所有内容都将复制到 www/assets,包括您的脚本。index.html 也将被复制到 www。
<!-- cordova.js required for cordova apps -->
<script src="cordova.js"></script>
<script src="assets/scripts/openpgp/openpgp.js"></script>
<script src="assets/scripts/openpgp/openpgp.worker.js"></script>
回答by Marc Harry
I would add any scripts such as jquery either just before or just after the script tag for cordova. This is in index.html
located within the app/
directory of the project that running the yeoman generator created.
我会在cordova的脚本标记之前或之后添加任何脚本,例如jquery。它index.html
位于app/
运行创建的自耕农生成器的项目目录中。
<!-- Place scripts such as JQuery here -->
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<script src="scripts/config.js"></script>
<script src="scripts/app.js"></script>
<script src="scripts/controllers.js"></script>
Once you have placed the scripts here they will get called when the app runs, and libraries such as jquery will be available globally throughout the app. I hope this helps.
将脚本放置在这里后,它们将在应用程序运行时被调用,并且诸如 jquery 之类的库将在整个应用程序中全局可用。我希望这有帮助。
回答by Nditah
As nice listed by a user here https://github.com/ionic-team/ionic/issues/9422
正如用户在这里列出的那样好https://github.com/ionic-team/ionic/issues/9422
- Install latest ionic
- Start your project
- Go to /src/
- open the assets folder
- create a /js/ folder inside assets
- copy your .js inside this js folder
- goto /src/index.html
- Add your path
- Go to your typescript file of choice and declare var N;
- N.yourFunction
- ionic serve
- 安装最新的离子
- 开始你的项目
- 转到 /src/
- 打开资产文件夹
- 在资产中创建一个 /js/ 文件夹
- 将您的 .js 复制到这个 js 文件夹中
- 转到/src/index.html
- 添加您的路径
- 转到您选择的打字稿文件并声明 var N;
- N.yourFunction
- 离子服务