我对如何在&#34; ons-button&#34;的HTML标记内使用require.js感到迷茫。 html标记项( login.html )。只要我将代码嵌入到require语句( index.html )中,我就能成功使用它。有没有办法直接在一个不是html的html文件中使用它。我尝试过使用<script src=""></script>
,但后来我不知道如何访问这些功能&#34; 返回&#34;通过require.js。任何帮助或方向都会很棒。
背景:我是require.js的新手。我有点工作,但仍然有一个重大问题。我有一个使用 loginMain.html 启动的登录cordova应用程序,但我希望尽可能保持我的代码组织在不同的文件中。因此,此cordova应用程序的主页是 login.html 。我正在尝试访问 auth.js 中的函数存储。
此zip文件中提供了这些文件:
http://www.filedropper.com/requirejsquestion
我认为相关的代码是相关的:
loginMain.html(不工作)
<!DOCTYPE HTML>
<html >
<head>
<!-- imports -->
<script>
ons.bootstrap();
</script>
</head>
<body>
<ons-navigator var="app.login" page="login.html">
</ons-navigator>
<script type="text/javascript">
require.config({ baseUrl: "js/" });
require(['auth'], function(auth) {
ons.ready(function() {});
});
</script>
</body>
</html>
login.html(不工作)
<ons-page>
<ons-toolbar>
<div class="center">Log In</div>
<div class="right"><ons-toolbar-button></ons-toolbar-button></div>
</ons-toolbar>
<div class="login-form">
<!-- other code -->
<ons-button modifier="quiet" id="googleplus" onclick="auth.googleReq()">
Sign in with Google
</ons-button>
<br>
<!-- other code -->
</div>
</ons-page>
我也在<body></body>
<body>
<script type="text/javascript">
var html = '<ons-navigator var="app.login" page="login.html"> \
</ons-navigator> \';
require.config({ baseUrl: "js/" });
require(['auth'], function(auth) {
document.write(html);
ons.ready(function() {});
});
</script>
</body>
auth.js(相信是正确的)
define(function(require) {
//initialize OAuth
OAuth.initialize('MYKEY');
return {
//MORE FUNCTIONS
googleReq: function() {
token = "none";
OAuth.popup('google', {
cache: true
})
.done(function(result) {
//use result.access_token in your API request
//or use result.get|post|put|del|patch|me methods (see below)
token = result.access_token;
return token;
})
.fail(function (err) {
//handle error with err
return token;
});
}
//MORE FUNCTIONS
};
});