我需要在AngularJS应用中使用Javascript库,但是在初始化angular应用之前,我已经在index.html中需要它。
这是库:https://github.com/lancedikson/bowser
通常这样使用:
const browser = Bowser.getParser(window.navigator.userAgent);
console.log(`The current browser name is "${browser.getBrowserName()}"`);
// The current browser name is "Internet Explorer"
问题是,在脚本标记中定义了库之后:
<script src="lib/bower/..../bowser.js"></script>
我可以像这样在index.html中的下一个脚本标记中立即使用它吗?
<script>
const browser = Bowser.getParser(window.navigator.userAgent);
...
</script>
目前,Bowser尚未定义。
答案 0 :(得分:1)
检查Bowser
代码后,好像没有将其大写。此外,您正在提供的Bowser库文件可能不打算以这种方式使用。一种解决方案是从CDN提供Bowser的内置版本:
<script src="https://cdn.jsdelivr.net/npm/bowser@2.5.3/es5.min.js"></script>
<script>
const browser = bowser.getParser(window.navigator.userAgent);
//...
</script>