我正在尝试谷歌浏览器中的ES6模块。我想在单击按钮时启动alert()(在导入的函数中)。
js / notification.js加载良好,但是当我单击按钮时出现错误:
未捕获的ReferenceError:在HTMLButtonElement.onclick((index :: 24)<-index.html中按钮的行
index.html
<head>
<script type="module" src="js/main.js"></script>
</head>
<body>
<section id="container">
<button type="error" onclick="createNotification()">Create</button>
</section>
</body>
Run Code Online (Sandbox Code Playgroud)
js / main.js
import {createNotification} from './notification.js';
Run Code Online (Sandbox Code Playgroud)
js / notification.js
export function createNotification(type){
alert();
}
Run Code Online (Sandbox Code Playgroud) 我想了解两种get方法之间的区别,一种工作,另一种不是,但是我不明白为什么。
这不起作用:
fetch('https://glo3102lab4.herokuapp.com/fee958c0-c320-40d0-a750-218f2d7c1303/tasks', {
method: 'GET',
}).then(res => res.json)
.catch(error => {
console.error('Error:', error);
})
.then(response => {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
然后返回:
ƒjson(){[本地代码]}
这很好用:
fetch('https://glo3102lab4.herokuapp.com/fee958c0-c320-40d0-a750-218f2d7c1303/tasks').then(function(response){
response.json().then(function(data) {
console.log(data);
});
}).catch(function(error) {
console.log('Fetch Error:', error);
});
Run Code Online (Sandbox Code Playgroud)
然后返回:
{tasks:Array(4)}任务:(4)[{…},{…},{…},{…}] 原型:对象
如果您想尝试一下:
fetch('https://glo3102lab4.herokuapp.com/fee958c0-c320-40d0-a750-218f2d7c1303/tasks', {
method: 'GET',
}).then(res => res.json)
.catch(error => {
console.error('Error:', error);
})
.then(response => {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)