我正在尝试使用https://github.com/matthewhudson/current-device
在演示页https://thematthewhudson.com/current-device/上,我查看了页面的源代码,我看到了:
https://thematthewhudson.com/current-device/
<script src="https://unpkg.com/current-device/umd/current-device.min.js"></script>
<script type="text/javascript">
var device = device.default;
console.log("device.portrait() === %s", device.portrait());
console.log("device.landscape() === %s", device.landscape());
console.log("device.mobile() === %s", device.mobile());
console.log("device.tablet() === %s", device.tablet());
console.log("device.ipad() === %s", device.ipad());
console.log("device.ipod() === %s", device.ipod());
console.log("device.iphone() === %s", device.iphone());
console.log("device.android() === %s", device.android());
console.log("device.androidTablet() === %s", device.androidTablet());
console.log("device.blackberryTablet() === %s", device.blackberryTablet());
console.log("device.fxos() === %s", device.fxos());
console.log("device.fxosPhone() === %s", device.fxosPhone());
console.log("device.fxosTablet() === %s", device.fxosTablet());
console.log("device.meego() === %s", device.meego());
console.log("device.television() === %s", device.television());
device.onChangeOrientation(function (newOrientation) {
console.log("New orientation is " + newOrientation);
});
</script>
当我做同样的事情时:
我在控制台上收到此错误:
Uncaught TypeError: Cannot read property 'default' of undefined
at window.onload (fiddle.jshell.net/:206)
这是我所做的例子的小提琴:
https://jsfiddle.net/52g75e2s/5/
为什么我收到此未定义的错误?
是因为var device = device.default;
吗?
答案 0 :(得分:0)
这是一个非常小的脏HTML页面。将其保存在文件中并在浏览器中打开。看看控制台(没有其他输出)
<html>
<head>
</head>
<body>
<script src="https://unpkg.com/current-device/umd/current-device.min.js"></script>
<script type="text/javascript">
var device = device.default;
console.log("device.portrait() === %s", device.portrait());
console.log("device.landscape() === %s", device.landscape());
console.log("device.mobile() === %s", device.mobile());
console.log("device.tablet() === %s", device.tablet());
console.log("device.ipad() === %s", device.ipad());
console.log("device.ipod() === %s", device.ipod());
console.log("device.iphone() === %s", device.iphone());
console.log("device.android() === %s", device.android());
console.log("device.androidTablet() === %s", device.androidTablet());
console.log("device.blackberryTablet() === %s", device.blackberryTablet());
console.log("device.fxos() === %s", device.fxos());
console.log("device.fxosPhone() === %s", device.fxosPhone());
console.log("device.fxosTablet() === %s", device.fxosTablet());
console.log("device.meego() === %s", device.meego());
console.log("device.television() === %s", device.television());
device.onChangeOrientation(function (newOrientation) {
console.log("New orientation is " + newOrientation);
});
</script>
</body>
</html>