Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能否增加一个类似require.js的data-main #49

Closed
Yunhui opened this issue Oct 21, 2015 · 5 comments
Closed

能否增加一个类似require.js的data-main #49

Yunhui opened this issue Oct 21, 2015 · 5 comments

Comments

@Yunhui
Copy link

Yunhui commented Oct 21, 2015

如题,在require.js里面data-main属性指向的文件这个文件会第一个被require.js加载。这样的话,在开发过程中就可以少一个script标签引入config文件。

<script data-main="js/config" src="js/require.js"></script>
@errorrik
Copy link
Contributor

我觉得你对data-main的理解稍微有点问题。

main指的是入口模块,用于app启动的。类似于C里的main函数。通常情况下一个应用总需要一个模块作为启动入口,data-main正是为此诞生。

正确使用data-main的姿势是:

<head>
<script data-main="main" src="js/require.js"></script>
</head>

假设你要配置config,正确姿势应该是:

<head>
<script data-main="main" src="js/require.js"></script>
<script>
require.config({
});
</script>
</head>

不正确的姿势是:

html:
-----------
<head>
<script data-main="js/config" src="js/require.js"></script>
</head>

js/config.js:
-----------
require.config({});
require(['main'], function (main) {
});

@Yunhui
Copy link
Author

Yunhui commented Oct 21, 2015

确实是这样,但是随便举个例子而已。如果有data-main的话,我可以减少写一行入口模块的执行脚本

@errorrik
Copy link
Contributor

了解,有空的时候我实现下,看看体积具体增加了多少,再决定是否加入此特性。此issue先保持open状态

@Yunhui
Copy link
Author

Yunhui commented Oct 21, 2015

其实很早之前用require.js的第一思路是。

<script data-main="js/config" src="js/require.js"></script>

```javascript
// config.js
require.config({
    paths: {
        jquery:"lib/jquery.min"
    }
});

```javascript
// 当前页面的js
require(['jquery'], function($){
    console.log($)
});

然后直接就挂了。

@errorrik
Copy link
Contributor

挂是对的。因为页面上js执行时,你config.js还没回来,所以require(['jquery']压根就没法去正确的位置请求

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants