简体   繁体   English

为什么javascript没有include,require或import函数?

[英]Why doesn't javascript have an include, require, or import function?

Sticking with the concept of separating html, from php and javascript per best coding practices, I'm finding it quite inconvenient to include my javascript files like this: <script src="../../lib/javascript/jquery.min.js"></script> 坚持按照最佳编码实践将html与php和javascript分开的概念,我发现包括这样的javascript文件非常不便: <script src="../../lib/javascript/jquery.min.js"></script>

The reason is that I'm refactoring a large project that isn't easily maintained at the moment. 原因是我正在重构目前尚不容易维护的大型项目。 Part of this includes separating a lot out into more managable chunks: 其中一部分包括将很多部分分成更多可管理的块:

在此处输入图片说明

Here's the main folder of concern: 这是需要关注的主要文件夹:

在此处输入图片说明

Long story short, I'm having difficulty figuring out how to include and sort of nest my javascript, in a similar fashion to php import for example. 长话短说,我很难弄清楚如何包括和嵌套我的JavaScript,例如,类似于php import的方式。 For example, my logon page uses users.js to run a function to check the user credentials; 例如,我的登录页面使用users.js运行一个函数来检查用户凭据; and users.js uses alerts.js to display any problems. 并且users.js使用Alerts.js来显示任何问题。 The problem is that, currently, I only know how to include and link all these files together using html in the login page like so: 问题是,目前,我只知道如何在登录页面中使用html将所有这些文件包括在内并将其链接在一起,如下所示:

<script src="../../lib/javascript/jquery.min.js"></script>
<script src="../../lib_bh/javascript/alerts.js"></script>
<script src="../../lib_bh/javascript/users.js"></script>

But this doesn't seem right since alerts is only used by users.js functions on this page and not the page itself. 但这似乎不正确,因为警报仅由此页面上的users.js函数使用,而不由页面本身使用。 I could use the above script tags in user.js to fix this, but then I'm turning what was intended to be a js library into an html file, and that doesn't seem right ether. 我可以在user.js中使用上面的脚本标签来解决此问题,但是随后我将原本打算将js库转换为html文件,这似乎不正确。 I've been looking at ES6 syntax and it looks promising, but there isn't a lot of talk about it so I'm concerned (and confused) about how to use it. 我一直在研究ES6语法,它看起来很有前途,但是关于它的讨论不多,所以我担心(也很困惑)如何使用它。 Basically, how do I nest my javascript so that I can separate functions and code of different types to be used throughout all my pages in a dynamic way. 基本上,我该如何嵌套我的JavaScript,以便可以动态地分离要在我所有页面中使用的不同类型的函数和代码。 Heck, I would much rather include jquery instead of using the script tag too... reading about doing that caused concerns ( https://github.com/DefinitelyTyped/DefinitelyTyped/issues/6315 ) so I feel like I'm at a brick wall. 哎呀,我宁愿包括jquery而不是也使用script标签...阅读有关这样做引起关注的信息( https://github.com/DefinitelyTyped/DefinitelyTyped/issues/6315 ),所以我感觉自己在砖墙。 Thanks. 谢谢。

I think I sort of answered my own question. 我想我已经回答了自己的问题。 If you use a callback and pass the data back, it fixes this programming issue. 如果使用回调并将数据传回,则可以解决此编程问题。 Makes it so that including all javascript code references directly in the page make sense. 做到这一点,这样就可以在页面中直接包含所有javascript代码引用。 ie Thanks to the callback function, one js file never need be dependent on another. 即由于有了回调函数,一个js文件不再需要依赖另一个文件。

You can use HTML Imports: 您可以使用HTML导入:

<link rel="import" href="include.html">

Then in the referenced file (here: include.html ), put all the Javascript and CSS files you need. 然后在引用的文件(这里: include.html )中,放入您需要的所有Javascript和CSS文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM