我为服务器端渲染克隆了一个有角度的通用项目,并且路由也可以正常工作,但是每次我单击查看源页面时都会遇到以下错误,并且页面无法加载。(我正在发送数据以在formdata中命中api )。
每当我点击查看源页面时,控制台都会显示此错误
错误{错误:未捕获(承诺):ReferenceError:FormData不是 定义ReferenceError:未定义FormData 在BodyWisdomComponent.getAllBodyWisdom(webpack:///./dist-server/main.095f1696d45b5b44a263.js?:1:10104)
我使用formdata将我的数据发送到api中的服务器:
var myFormData = new FormData();
myFormData.append('timezone', 'asia/kolkata');
myFormData.append('page_no', this.pageNumber);
我的webpack.config.js文件是:
const path = require('path');
const webpack = require('webpack');
const FormData = require('form-data');
var nodeExternals = require('webpack-node-externals');
/**
* This is a server config which should be merged on top of common config
*/
module.exports = {
entry: {
// This is our Express server for Dynamic universal
server: './server.ts',
// This is an example of Static prerendering (generative)
prerender: './prerender.ts'
},
resolve: { extensions: [".js", ".ts"] },
target: 'node',
externals: [/(node_modules|main\..*\.js)/],
output: {
// Puts the output at the root of the dist folder
path: path.join(__dirname),
filename: '[name].js'
},
mode: 'development',
node: {
__dirname: false
},
//externals: [/(node_modules|main\..*\.js)/],
/* externals: [nodeExternals({ whitelist: [/ngx-cookie/] })], */
plugins: [
new webpack.ContextReplacementPlugin(
// fixes WARNING Critical dependency: the request of a dependency is an expression
/(.+)?angular(\\|\/)core(.+)?/,
path.join(__dirname, 'src'), // location of your src
{} // a map of your routes
),
new webpack.ContextReplacementPlugin(
// fixes WARNING Critical dependency: the request of a dependency is an expression
/(.+)?express(\\|\/)(.+)?/,
path.join(__dirname, 'src'),
{}
)
],
};
我的server.ts文件是:
const domino = require('domino');
const fs = require('fs');
const path = require('path');
const template = fs.readFileSync(path.join(__dirname, '.', 'dist', 'index.html')).toString();
const win = domino.createWindow(template);
const files = fs.readdirSync(`${process.cwd()}/dist-server`);
import fetch from 'node-fetch';
const FormData = require('form-data');
win.fetch = fetch;
global['window'] = win;
global['document'] = win.document;
Object.defineProperty(win.document.body.style, 'transform', {
value: () => {
return {
enumerable: true,
configurable: true
};
},
});
global['CSS'] = null;
// global['XMLHttpRequest'] = require('xmlhttprequest').XMLHttpRequest;
global['Prism'] = null;
import 'reflect-metadata';
import 'zone.js/dist/zone-node';
import { enableProdMode } from '@angular/core';
import * as express from 'express';
import * as compression from 'compression';
import * as cookieparser from 'cookie-parser';
const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');
const mainFiles = files.filter(file => file.startsWith('main'));
const hash = mainFiles[0].split('.')[1];
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require(`./dist-server/main.${hash}`);
import { ngExpressEngine } from '@nguniversal/express-engine';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
const PORT = process.env.PORT || 4000;
import { ROUTES } from './static.paths';
enableProdMode();
const app = express();
app.use(compression());
app.use(cookieparser());
const redirectowww = false;
const redirectohttps = true;
const wwwredirecto = true;
app.use((req, res, next) => {
// for domain/index.html
if (req.url === '/index.html') {
res.redirect(301, 'https://' + req.hostname);
}
// check if it is a secure (https) request
// if not redirect to the equivalent https url
if (redirectohttps && req.headers['x-forwarded-proto'] !== 'https' && req.hostname !== 'localhost') {
// special for robots.txt
if (req.url === '/robots.txt') {
next();
return;
}
res.redirect(301, 'https://' + req.hostname + req.url);
}
// www or not
if (redirectowww && !req.hostname.startsWith('www.')) {
res.redirect(301, 'https://www.' + req.hostname + req.url);
}
// www or not
if (wwwredirecto && req.hostname.startsWith('www.')) {
const host = req.hostname.slice(4, req.hostname.length);
res.redirect(301, 'https://' + host + req.url);
}
next();
}
);
app.engine('html', ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [
provideModuleMap(LAZY_MODULE_MAP)
]
}));
app.set('view engine', 'html');
app.set('views', 'src');
app.get('*.*', express.static(path.join(__dirname, '.', 'dist')));
app.get(ROUTES, express.static(path.join(__dirname, '.', 'static')));
app.get('*', (req, res) => {
global['navigator'] = req['headers']['user-agent'];
const http = req.headers['x-forwarded-proto'] === undefined ? 'http' : req.headers['x-forwarded-proto'];
// tslint:disable-next-line:no-console
console.time(`GET: ${req.originalUrl}`);
res.render(
'../dist/index',
{
req: req,
res: res,
providers: [
{
provide: REQUEST, useValue: (req)
},
{
provide: RESPONSE, useValue: (res)
},
{
provide: 'ORIGIN_URL',
useValue: (`${http}://${req.headers.host}`)
}
]
},
(err, html) => {
if (!!err) { throw err; }
// tslint:disable-next-line:no-console
console.timeEnd(`GET: ${req.originalUrl}`);
res.send(html);
});
});
app.listen(PORT, () => {
console.log(`listening on http://localhost:${PORT}!`);
});
答案 0 :(得分:2)
将以下内容导入您的组件中,
import { Component, OnInit, Inject, PLATFORM_ID} from '@angular/core';
import { isPlatformBrowser, isPlatformServer } from '@angular/common';
@Component({
selector: 'example-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor(@Inject(PLATFORM_ID) private platformId: Object) {
}
infunction() {
if (isPlatformBrowser(this.platformId)) {
window.scroll()
}))
}
}