有一个如下开头的 service.js 文件:
import Access from "./controls/access.js"
import Menu from "./controls/menu.js"
import User from "./controls/user.js"
import Admin from "./controls/admin.js"
class Service
{ ...
PHP中有一个页面生成:
$uri = $this->proto.$this->name."/public/";
$img = $uri."images";
$css = $uri."styles";
$js = $uri."scripts";
$html = <<<HTML
<html>
<head>
<title>My service</title>
<link rel="shortcut icon" type="image/x-icon" href="$img/favicon.ico">
<link rel="stylesheet" type="text/css" href="$css/style.css"/>
<script type="module" src="$js/service.js"></script>
</head>
<body onload="new Service('$this->control','$this->action');"></body>
</html>
HTML;
header("content-type: text/html; charset: UTF-8");
exit($html);
在浏览器控制台中加载页面时,消息Service is not defined
. 如果您不在 js 中使用它并直接使用而不是import
加载所有脚本,那么一切正常。如果您只是将类型更改为并保留导入,则会收到错误消息。script type="application/javascript"
module
application/javascript
import declarations may only appear at top level of a module
如何正确地将脚本与导入连接(或以某种方式更改它)以使其工作?
你可以这样做:添加
在 service.js 结束时: