我有一个简单的Angular 4应用,我想在Angular Universal(nodeJs服务器端渲染)的服务器端运行。我按照以下步骤在angular-cli帮助下配置Angular Universal,直到尝试使用angular环境,一切都很好。当我尝试访问环境常量中的属性时,它可以完美地在客户端(使用ng serve)呈现,但在服务器端(ts-node src/server.ts)则呈现以下错误:
Error: Cannot find module 'environments/environment'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Dev\code-carama\src\app\auth.service.ts:3:1)
at Module._compile (module.js:571:32)
at Module.m._compile (C:\Dev\code-carama\node_modules\ts-node\src\index.ts:385:23)
at Module._extensions..js (module.js:580:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\Dev\code-carama\node_modules\ts-node\src\index.ts:388:12)
at Module.load (module.js:488:32)
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v7.10.0
npm ERR! npm v4.2.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! code-carama@0.0.0 start: …Run Code Online (Sandbox Code Playgroud) 我有一个服务,我在其中定义了一个全局observable,如下所示:
@Injectable()
export class XYZService {
country: Observable<any>;
isBrowser: boolean;
constructor(private http: Http,
private httpClient: HttpClient,
private router: Router,
@Inject(PLATFORM_ID) private platformId: any) {
this.isBrowser = isPlatformBrowser(platformId);
if(this.isBrowser && !this.country) {
this.country = this.http.get('https://ipinfo.io')
.map(res => res.json())
.catch(() => {
return Observable.of({'country': 'SG'});
});
};
}
getLocation(): Observable<any> {
return this.country;
}
Run Code Online (Sandbox Code Playgroud)
现在在我的几个组件中,我在构造函数或ngOnInit中调用getLocation函数,如下所示:
this.XYZService.getLocation()
.subscribe(res => {
this.country = res['country'];
});
Run Code Online (Sandbox Code Playgroud)
我的期望是对ipinfo.io的请求只会进行一次.然而,这没有发生.从网络日志中我可以看到对ipinfo的请求正在进行多次.看起来是一些时间问题.如果我在构造函数中添加调试器或console.log语句,则只调用一次.但是,发送了多个请求.
我正在尝试使用dotnet核心创建一个Angular 7 SSR来托管它,默认模板使用angular 5,并且MS有文档来使SSR在其上运行(效果很好),我尝试通过命令行将其升级到angular 7并开始一个新的角度项目并实施SSR之后,但我总是遇到相同的问题,ng build和ng build:SSR在命令行上都可以正常工作,但是当我从VS运行它时,它超时了(我认为引发错误后,与该问题无关):
The thread 0x6608 has exited with code 0 (0x0).
The thread 0x1134 has exited with code 0 (0x0).
The thread 0x54bc has exited with code 0 (0x0).
Run Code Online (Sandbox Code Playgroud)
在此之后(https://github.com/aspnet/Templating/issues/593),我对NG5 SSR(https://go.microsoft.com/fwlink/?linkid=864501)所做的更改是:
startup.cs
options.BootModulePath = $"{spa.Options.SourcePath}/dist-server/main.bundle.js";
Run Code Online (Sandbox Code Playgroud)
至
options.BootModulePath = $"{spa.Options.SourcePath}/dist-server/main.js";
Run Code Online (Sandbox Code Playgroud)
再次将SSR项目添加到angular.json
"ssr": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:server",
"options": {
"outputPath": "dist-server",
"main": "src/main.server.ts",
"tsConfig": "src/tsconfig.server.json"
},
"configurations": {
"production": {
"optimization": false,
"outputHashing": "media", …Run Code Online (Sandbox Code Playgroud) 我在Plesk面板 (apache) 上运行Angular Universal时遇到问题:
我已经安装了nodejs:
npm run build:ssr在本地主机上执行此操作,并将其dist从本地主机复制到httpdocsplesk 上的目录。package.json上httpdocs;我在 Plesk 中单击
Run script并尝试运行serve:ssr,但没有任何反应。
尝试通过执行安装 Angular Universal 时会出现此错误add @nguniversal/express-engine --clientProject [project name]。
我正在使用 Angular 7。我尝试运行npm install @schematics/angular@7.2.0,但没有成功。
什么可能导致此失败?