我使用yeoman angular-generator创建了一个测试Web应用程序,并将其部署到Firebase托管。当我在本地运行该应用程序时,它工作正常。部署后,Chrome 开发控制台会报告"警告:尝试多次加载角度"。当我使用"firebase serve"在本地运行时,我看到对供应商和脚本 js 文件的 GET 请求无休止地重复。如果我使用"grunt serve"在本地运行,则不会发生这种情况,并且站点运行正常。
以下是添加到页面的脚本:
<!-- build:js(.) scripts/vendor.js -->
<!-- bower:js -->
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/bootstrap-sass-official/assets/javascripts/bootstrap.js"></script>
<script src="bower_components/angular-animate/angular-animate.js"></script>
<script src="bower_components/angular-cookies/angular-cookies.js"></script>
<script src="bower_components/angular-messages/angular-messages.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
<script src="bower_components/angular-touch/angular-touch.js"></script>
<script src="bower_components/firebase/firebase.js"></script>
<!-- endbower -->
<!-- endbuild -->
<!-- build:js({.tmp,app}) scripts/scripts.js -->
<script src="scripts/app.js"></script>
<script src="scripts/services/firebaseService.js"></script>
<script src="scripts/controllers/mainview.js"></script>
<script src="scripts/controllers/signinview.js"></script>
<script src="scripts/controllers/signinout.js"></script>
<script src="scripts/directives/signinout.js"></script>
<!-- endbuild -->
以下是由 grunt 构建任务从此生成的脚本标记,其中包括角度生成器中默认包含的 concat、uglify、minify 任务:
<script src="scripts/vendor.566b3c64.js"></script>
<script src="scripts/scripts.ea0abd4c.js"></script>
当我使用 firebase 运行本地服务器时,我收到以下消息无休止地重复:
127.0.0.1 - - [11/Jul/2016:17:53:51 +0000] "GET /scripts/vendor.58eb8161.js?_=1468259614482 HTTP/1.1" 200 633643 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
127.0.0.1 - - [11/Jul/2016:17:53:51 +0000] "GET /scripts/scripts.3f5216cb.js?_=1468259614483 HTTP/1.1" 200 3300 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
127.0.0.1 - - [11/Jul/2016:17:53:52 +0000] "GET /scripts/vendor.58eb8161.js?_=1468259614484 HTTP/1.1" 200 633643 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
127.0.0.1 - - [11/Jul/2016:17:53:52 +0000] "GET /scripts/scripts.3f5216cb.js?_=1468259614485 HTTP/1.1" 200 3300 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
127.0.0.1 - - [11/Jul/2016:17:53:52 +0000] "GET /scripts/vendor.58eb8161.js?_=1468259614486 HTTP/1.1" 200 633643 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
127.0.0.1 - - [11/Jul/2016:17:53:52 +0000] "GET /scripts/scripts.3f5216cb.js?_=1468259614487 HTTP/1.1" 200 3300 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
127.0.0.1 - - [11/Jul/2016:17:53:52 +0000] "GET /scripts/vendor.58eb8161.js?_=1468259614488 HTTP/1.1" 200 633643 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
127.0.0.1 - - [11/Jul/2016:17:53:52 +0000] "GET /scripts/scripts.3f5216cb.js?_=1468259614489 HTTP/1.1" 200 3300 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
127.0.0.1 - - [11/Jul/2016:17:53:52 +0000] "GET /scripts/vendor.58eb8161.js?_=1468259614490 HTTP/1.1" 200 633643 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
127.0.0.1 - - [11/Jul/2016:17:53:52 +0000] "GET /scripts/scripts.3f5216cb.js?_=1468259614491 HTTP/1.1" 200 3300 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
我找到了问题的根源,即使我不明白到底发生了什么。我有一个自定义的角度指令,它引用了一个 html 文件的模板 URL。我相信 yeoman 角度生成器创建的文件夹结构的某些内容与我设置自定义指令的方式无关。我将模板移动到字符串中并将其直接放在自定义指令中,这解决了问题。就像我说的,我不明白为什么。我通过从头开始并一次添加一件事来找到这个解决方案,直到我再次看到问题。