Javascript Angular 4 Universal this.html.charCodeAt不是一个函数
我使用并集成了我所有的Angular 2代码。一切正常,但在终端我得到了这个奇怪的错误。不知道是什么问题 错误类型错误:this.html.charCodeAt不是函数 在Preprocessor.advance(/home/user/node/project/dist/server.js:152260:24) 在Tokenizer.消费(/home/user/node/project/dist/server.js:45694:30) 位于Tokenizer.getNextToken(/home/user/node/project/dist/server.js:45652:23) 在解析器处运行parsingloop(/home/user/node/project/dist/server.js:102429:36) 位于Parser.parseFragment(/home/user/node/project/dist/server.js:102384:10) 在Object.parseFragment(/home/user/node/project/dist/server.js:55136:19) 位于Parse5DomAdapter.setInnerHTML(/home/user/node/project/dist/server.js:53609:49) 位于Parse5DomAdapter.setProperty(/home/user/node/project/dist/server.js:53250:18) 在EmulatedEnvironmentationServerRender2.DefaultServerRender2.setProperty (/home/user/node/project/dist/server.js:54766:94) 在setElementProperty(/home/user/node/project/dist/server.js:9982:19) 其原因是server.js中的以下代码Javascript Angular 4 Universal this.html.charCodeAt不是一个函数,javascript,angular,angular-universal,Javascript,Angular,Angular Universal,我使用并集成了我所有的Angular 2代码。一切正常,但在终端我得到了这个奇怪的错误。不知道是什么问题 错误类型错误:this.html.charCodeAt不是函数 在Preprocessor.advance(/home/user/node/project/dist/server.js:152260:24) 在Tokenizer.消费(/home/user/node/project/dist/server.js:45694:30) 位于Tokenizer.getNextToken(/home
Preprocessor.prototype.advance = function () {
this.pos++;
if (this.pos > this.lastCharPos) {
if (!this.lastChunkWritten)
this.endOfChunkHit = true;
return $.EOF;
}
var cp = this.html.charCodeAt(this.pos); // Error Occurs here
//NOTE: any U+000A LINE FEED (LF) characters that immediately follow a U+000D CARRIAGE RETURN (CR) character
//must be ignored.
if (this.skipNextNewLine && cp === $.LINE_FEED) {
this.skipNextNewLine = false;
this._addGap();
return this.advance();
}
//NOTE: all U+000D CARRIAGE RETURN (CR) characters must be converted to U+000A LINE FEED (LF) characters
if (cp === $.CARRIAGE_RETURN) {
this.skipNextNewLine = true;
return $.LINE_FEED;
}
this.skipNextNewLine = false;
//OPTIMIZATION: first perform check if the code point in the allowed range that covers most common
//HTML input (e.g. ASCII codes) to avoid performance-cost operations for high-range code points.
return cp >= 0xD800 ? this._processHighRangeCodePoint(cp) : cp;
};
this.html typeof
正在返回“字符串”,但就在出错之前,它正在返回对象下方
{ treeAdapter:
{ createDocument: [Function],
createDocumentFragment: [Function],
createElement: [Function],
createCommentNode: [Function],
appendChild: [Function],
insertBefore: [Function],
setTemplateContent: [Function],
getTemplateContent: [Function],
setDocumentType: [Function],
setDocumentMode: [Function],
getDocumentMode: [Function],
detachNode: [Function],
insertText: [Function],
insertTextBefore: [Function],
adoptAttributes: [Function],
getFirstChild: [Function],
getChildNodes: [Function],
getParentNode: [Function],
getAttrList: [Function],
getTagName: [Function],
getNamespaceURI: [Function],
getTextNodeContent: [Function],
getCommentNodeContent: [Function],
getDocumentTypeNodeName: [Function],
getDocumentTypeNodePublicId: [Function],
getDocumentTypeNodeSystemId: [Function],
isTextNode: [Function],
isCommentNode: [Function],
isDocumentTypeNode: [Function],
isElementNode: [Function] } }
下面是我的universalserver.ts
import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import 'rxjs/Rx';
import * as express from 'express';
import { Request, Response } from 'express';
import { platformServer, renderModuleFactory } from '@angular/platform-server';
import { ServerAppModule } from './app/server-app.module';
import { ngExpressEngine } from '@nguniversal/express-engine';
import { ROUTES } from './routes';
import { enableProdMode } from '@angular/core';
enableProdMode();
const app = express();
const port = 4200;
const baseUrl = `http://localhost:${port}`;
app.engine('html', ngExpressEngine({
bootstrap: ServerAppModule
}));
app.set('view engine', 'html');
app.set('views', 'src');
app.use('/', express.static('dist', {index: false}));
// ROUTES.forEach((route: string) => {
app.get('/*', (req: Request, res: Response) => {
console.time(`GET: ${req.originalUrl}`);
res.render('../dist/index', {
req: req,
res: res
});
console.timeEnd(`GET: ${req.originalUrl}`);
});
// });
app.listen(port, () => {
console.log(`Listening at ${baseUrl}`);
});
- 节点:v6.9.2
- 角度:4.1.0
我在使用时也遇到了同样的错误
<div [innerHTML]="someHtml"></div>
但我用这样的*ngIf=“someHtml”解决了这个问题
<div *ngIf="someHtml" [innerHTML]="someHtml"></div>
希望这对你也有用 如果不显示
server.js
的内容,您的问题将无法解决。@ShanevandenBogaard抱歉,我刚刚更新了我的问题。我使用的是angular 4 universal Starter中给出的同一个server.ts文件。您能否检查预处理器实例中html
字段的类型。this.html的类型是string
,它包含整个渲染的htmlWhat version of Node正在运行?如果您运行的是旧版本,有没有理由不更新到最新版本?