我对 Angular 5.2.10TransferState模块有一个小问题。目前我正在使用 Angular Universal(最新版本)来使我的网站 SEO 友好。一切工作正常,除了一件事......页面首先加载 - 在服务器端,然后内容消失并加载到浏览器端(屏幕)。
我已经创建了用于TransferState实施的服务(您可以在下面看到它)。
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import "rxjs/add/operator/map";
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/share';
import { Observable } from 'rxjs/Observable';
import { TransferState, makeStateKey, DomSanitizer, SafeHtml } from '@angular/platform-browser';
interface pageData {
banner: string;
data: any;
html: SafeHtml;
text: string;
title: string;
}
@Injectable()
export class ServerService {
constructor(private http: HttpClient, private state: TransferState, private sanitizer: DomSanitizer) { }
getResponse(url): Observable<any> { …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的 Angular 5 项目中设置渐进式 Web 应用程序。我还使用 Angular Universal 进行服务器端渲染。
我在从 API 缓存数据时遇到问题。我做了一个 Rest API,看起来像https://example.net/getContent/param_1/param_2/param_3。其中param_1是路由参数中的页面名称,param_2是 lang url,param_3是 lang 代码。在 ngsw-config.json 中我这样做:
"dataGroups": [{
"name": "api-performance",
"urls": [
"https://example.net/getMenus/**",
"https://example.net/getContent/**",
"https://example.net/getLayout/**",
"https://example.net/getFooter/**"
],
"cacheConfig": {
"maxSize": 10000,
"maxAge": "3d",
"strategy": "performance"
}
}]
Run Code Online (Sandbox Code Playgroud)
我认为它应该缓存每个请求,例如“ https://example.net/getMenus/anything/anything/anything/ ”,但事实并非如此。我无法离线运行应用程序,服务工作线程之前不会预加载所有页面数据。如何让它发挥作用?如何预加载所有页面的所有 api 调用?也许动态 api 调用会产生问题?
这是我来自软件和示例组件的代码。
应用程序模块
// Core
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { …Run Code Online (Sandbox Code Playgroud) 我正在尝试从不同的server.js文件(不同的目录、配置等)在同一端口和服务器上运行 2 个 NodeJS 实例。我的服务器提供商给了我一个vhost针对不同域运行的信息,这就是问题所在。如何在 NodeJS Express 应用程序中处理它?我试过vhost从https://github.com/expressjs/vhost像这样使用:
const app = express();
const vhost = require('vhost');
app.use(vhost('example1.org', app));
// Start up the Node server
app.listen(4100, () => {
console.log(`Node server listening on 4100`);
});
Run Code Online (Sandbox Code Playgroud)
对于这样的第二个应用程序:
const app = express();
const vhost = require('vhost');
app.use(vhost('example2.org', app));
// Start up the Node server
app.listen(4100, () => {
console.log(`Node server listening on 4100`);
});
Run Code Online (Sandbox Code Playgroud)
但是当我尝试运行第二个实例时,我得到了EADDRINUSE ::: 4100,所以 vhost 在这里不起作用。
你知道如何解决吗?