请将文档作为第一个元素

时间:2017-11-08 11:14:41

标签: angular

我刚刚从版本4升级到Angular 5,我在控制台中收到此错误:

Error: Please have <head> as the first element in your document
    at ServerTransferState.inject (/Users/michaelwilson/Code/project/dist/server.js:183316:23)
    at SafeSubscriber._next (/Users/michaelwilson/Code/project/dist/server.js:170489:27)
    at SafeSubscriber.__tryOrUnsub (/Users/michaelwilson/Code/project/dist/server.js:17319:16)
    at SafeSubscriber.next (/Users/michaelwilson/Code/project/dist/server.js:17266:22)
    at Subscriber._next (/Users/michaelwilson/Code/project/dist/server.js:17206:26)
    at Subscriber.next (/Users/michaelwilson/Code/project/dist/server.js:17170:18)
    at FirstSubscriber._emitFinal (/Users/michaelwilson/Code/project/dist/server.js:117823:25)
    at FirstSubscriber._emit (/Users/michaelwilson/Code/project/dist/server.js:117806:14)
    at FirstSubscriber._next (/Users/michaelwilson/Code/project/dist/server.js:117785:18)
    at FirstSubscriber.Subscriber.next (/Users/michaelwilson/Code/project/dist/server.js:17170:18)

我正在使用Angular Universal,我的index.html确实将head作为文档中的第一个元素:

<!DOCTYPE html>
 <html>
  <head>
   ...

我在哪里错了?

1 个答案:

答案 0 :(得分:2)

正如@estus所指出的,这是我的项目特有的问题(它使用universal-starter时出现错误:

const head = document.head;

if (head.name !== 'head') {
  throw new Error('Please have <head> as the first element in your document');
}

我所要做的就是改变它:

const head = document.head;

if (head.tagName !== 'HEAD') {
  throw new Error('Please have <head> as the first element in your document');
}

错误消失了:)