Angular 4服务器端渲染提供完全生成的HTML页面

我从Angular4开始。我试图将服务器端渲染整合到一些Angular 4免费模板中,这些模板需要在稍后的实际项目中进行整合。虽然应用程序正在成功构建,但在运行Angular应用程序时仍面临以下问题

    > ang_bs@0.0.0 start /Applications/Workspace/Angular/Ang_Bs
    > ts-node src/server.ts

    listening on http://localhost:4001!
    ERROR { ReferenceError: document is not defined
        at DefaultDomRenderer2.selectRootElement (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/platform-browser/bundles/platform-browser.umd.js:2818:72)
        at BaseAnimationRenderer.selectRootElement (/Applications/Workspace/Angular/Ang_Bs/packages/platform-browser/animations/src/animation_renderer.ts:166:62)
        at createElement (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:9220:23)
        at createViewNodes (/Applications/Workspace/Angular/Ang_Bs/packages/core/src/view/view.ts:320:1)
        at createRootView (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:12114:5)
        at Object.createProdRootView [as createRootView] (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:12788:12)
        at ComponentFactory_.create (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:9893:46)
        at ComponentFactoryBoundToModule.create (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:3365:29)
        at ApplicationRef_.bootstrap (/Applications/Workspace/Angular/Ang_Bs/packages/core/src/application_ref.ts:670:1)
        at /Applications/Workspace/Angular/Ang_Bs/packages/core/src/application_ref.ts:415:1
    __zone_symbol__currentTask:
    ZoneTask {
        _zone:
        Zone {
            _properties: [Object],
            _parent: [Object],
            _name: 'angular',
            _zoneDelegate: [Object] },
        runCount: 0,
        _zoneDelegates: null,
        _state: 'notScheduled',
        type: 'microTask',
        source: 'Promise.then',
        data: undefined,
        scheduleFn: undefined,
        cancelFn: null,
        callback: [Function],
        invoke: [Function] } }
    Unhandled Promise rejection: document is not defined ; Zone: <root> ; Task: Promise.then ; Value: { ReferenceError: document is not defined
        at DefaultDomRenderer2.selectRootElement (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/platform-browser/bundles/platform-browser.umd.js:2818:72)
        at BaseAnimationRenderer.selectRootElement (/Applications/Workspace/Angular/Ang_Bs/packages/platform-browser/animations/src/animation_renderer.ts:166:62)
        at createElement (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:9220:23)
        at createViewNodes (/Applications/Workspace/Angular/Ang_Bs/packages/core/src/view/view.ts:320:1)
        at createRootView (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:12114:5)
        at Object.createProdRootView [as createRootView] (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:12788:12)
        at ComponentFactory_.create (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:9893:46)
        at ComponentFactoryBoundToModule.create (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:3365:29)
        at ApplicationRef_.bootstrap (/Applications/Workspace/Angular/Ang_Bs/packages/core/src/application_ref.ts:670:1)
        at /Applications/Workspace/Angular/Ang_Bs/packages/core/src/application_ref.ts:415:1
    __zone_symbol__currentTask:
    ZoneTask {
        _zone:
        Zone {
            _properties: {},
            _parent: null,
            _name: '<root>',
            _zoneDelegate: [Object] },
        runCount: 0,
        _zoneDelegates: null,
        _state: 'notScheduled',
        type: 'microTask',
        source: 'Promise.then',
        data: undefined,
        scheduleFn: undefined,
        cancelFn: null,
        callback: [Function],
        invoke: [Function] } } ReferenceError: document is not defined
        at DefaultDomRenderer2.selectRootElement (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/platform-browser/bundles/platform-browser.umd.js:2818:72)
        at BaseAnimationRenderer.selectRootElement (/Applications/Workspace/Angular/Ang_Bs/packages/platform-browser/animations/src/animation_renderer.ts:166:62)
        at createElement (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:9220:23)
        at createViewNodes (/Applications/Workspace/Angular/Ang_Bs/packages/core/src/view/view.ts:320:1)
        at createRootView (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:12114:5)
        at Object.createProdRootView [as createRootView] (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:12788:12)
        at ComponentFactory_.create (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:9893:46)
        at ComponentFactoryBoundToModule.create (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:3365:29)
        at ApplicationRef_.bootstrap (/Applications/Workspace/Angular/Ang_Bs/packages/core/src/application_ref.ts:670:1)
        at /Applications/Workspace/Angular/Ang_Bs/packages/core/src/application_ref.ts:415:1

下面是我的package.json片段

“prestart”:“ng build --prod && ./node_modules/.bin/ngc”,

“start”:“ts-node src / server.ts”

任何帮助真的很感激!


您是否导入了@ angular / platform-b​​rowser来使用文档对象?

   import { DOCUMENT } from '@angular/platform-browser';

    @Injectable()
    export class MyService {
      constructor(@Inject(DOCUMENT) private document: any) {}
    }

否则,如果你使用document.getElementById()

你需要创建一个html元素作为导入

   HTMLElement el = document.getElementById()

..

否则,如果你在做动态DOM操作,最好是创建一个承诺来解析DOM加载。

链接地址: http://www.djcxy.com/p/31395.html

上一篇: Angular 4 server side rendering to serve a fully generated HTML page

下一篇: How to debug Angular universal?