[英]Accessing functions defined in system.js from HTML
When using system.js, what's the right way to access a function from the global scope? 使用system.js时,从全局范围访问函数的正确方法是什么? For example, suppose we have this HTML:
例如,假设我们有以下HTML:
<form onsubmit="foo">
<input type="text" name="name" placeholder="name">
</form>
<script src="/lib/system.js"></script>
<script>
SystemJS.import("/src/foo.js");
</script>
And we have this javascript: 我们有这个javascript:
function foo(form) { console.log("here"); }
How do you get SystemJS to export foo
in such a way that it can be called when the form is submitted? 如何使SystemJS以这样的方式导出
foo
在提交表单时可以调用它?
what about: 关于什么:
<form onsubmit="return foo(this);">
<input type="text" name="name" placeholder="name">
</form>
<script src="/lib/system.js"></script>
<script>
SystemJS.import("/src/foo.js");
</script>
And foo
should return true
or false
in order to signal whether the form should be submitted by the action
attribute. 并且
foo
应该返回true
或false
,以指示是否应由action
属性提交表单。 As this is missing, I assume false
is right here. 由于缺少此内容,因此我认为此处为
false
。
I haven't used system.js but a brief look at their docs told me that the import
call returns a promise which resolves with the namespace of the module being imported. 我没有使用过system.js,但简单地看一下他们的文档告诉我,
import
调用返回一个promise,该promise将被导入的模块的名称空间解析。 Perhaps you can try something like: 也许您可以尝试类似的方法:
<form onsubmit="foo"> <input type="text" name="name" placeholder="name"> </form> <script src="/lib/system.js"></script> <script> SystemJS.import("/src/foo.js") .then(function(foo){ window.foo = foo; }); </script>
Or, if you want to avoid polluting the global namespace: 或者,如果您想避免污染全局名称空间:
<form id="foo-form"> <input type="text" name="name" placeholder="name"> </form> <script src="/lib/system.js"></script> <script> SystemJS.import("/src/foo.js") .then(function(foo){ document.getElementById('foo-form') .addEventListener('submit', function(ev){ foo(...arguments); ev.preventDefault(); return false; }); }); </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.