我使用的是角4+网络包,我在nonTreeShakableModules const中添加了一个webpack.config.vendor.js插件:
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const merge = require('webpack-merge');
const treeShakableModules = [
'@angular/animations',
'@angular/common',
'@angular/compiler',
'@angular/core',
'@angular/forms',
'@angular/http',
'@angular/platform-browser',
'@angular/platform-browser-dynamic',
'@angular/router',
'font-awesome/css/font-awesome.css',
'zone.js',
];
const nonTreeShakableModules = [
'bootstrap',
'bootstrap/dist/css/bootstrap.css',
'es6-promise',
'es6-shim',
'event-source-polyfill',
'jquery',
'virtual-keyboard' //HERE
];在启动应用程序时,我得到了以下错误:
NodeInvocationException:打印失败是因为错误:jQuery需要一个带有文档的窗口
如果我刷新页面2-3次,错误就消失了.谢谢你的帮助!
发布于 2017-10-30 20:00:49
正如您的前一个问题的注释中所述,您不能在服务器端运行javascript代码,它依赖于window和其他一些情况(比如会话存储)和服务器端预呈现。
模板,如ASP.NET核心角网页模板与服务器端呈现启用。对于不需要会话存储、身份验证或访问浏览器组件或dom-tree的应用程序来说,这很好。
您必须通过从中删除asp-prerender-module="ClientApp/dist/app.module.server.ts"标记助手来禁用服务器侧预录制。
替换
<my-app asp-prerender-module="ClientApp/dist/app.module.server.ts"></my-app>使用
<my-app></my-app>当然,用应用程序的选择器替换my-app,通常是模板中的app。
或者,您必须有条件地运行代码,如这个GitHub问题中所指出的:
// boot-client.ts file
import 'ngx-charts';
// some.component.ts
import { isBrowser } from 'angular2-universal';
import * as $ from 'jquery';
// inside ngOnInit
if (isBrowser) {
$('body').hide(); // or whatever call you need to make
}以避免在服务器端预呈现上运行这样的代码。
https://stackoverflow.com/questions/47013168
复制相似问题