我正在尝试更新 Angular 6 和 Angular Univarsal 中的元标记,但它仅在检查元素中发生变化,而不是在 View 页面源中发生变化,它与主页上的保持不变。
主页.ts 。 。 .
import { SeoserviceService } from "./../../services/seoservice.service";
constructor(private metaService: Meta, public router: Router, public _seoService: SeoserviceService) {
}
ngOnInit() {
this._seoService.updateTitle('Home');
//Updating Description tag dynamically with title
this._seoService.updateDescription('Home Page Description');
}
}
关于.ts 。 。 .
import { SeoserviceService } from "./../../services/seoservice.service";
constructor(private metaService: Meta, public _seoService: SeoserviceService) {
}
ngOnInit() {
this._seoService.updateTitle('About');
//Updating Description tag dynamically with title
this._seoService.updateDescription('About Page Description');
}
SeoserviceService.ts
import { Injectable } from '@angular/core';
import { Meta, Title } from '@angular/platform-browser';
@Injectable()
export class SeoserviceService {
constructor(private title: Title, private meta: Meta) { }
updateTitle(title: string) {
this.title.setTitle(title);
}
updateDescription(desc: string) {
this.meta.updateTag({ name: 'description', content: desc })
}
}
服务器.ts
// These are important and needed before anything else
import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import { renderModuleFactory } from '@angular/platform-server';
import { enableProdMode } from '@angular/core';
import * as express from 'express';
import { join } from 'path';
import { readFileSync } from 'fs';
// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();
// Express server
const app = express();
const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist');
// Our index.html we'll use as our template
const template = readFileSync(join(DIST_FOLDER, 'browser', 'index.html')).toString();
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main.bundle');
const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');
app.engine('html', (_, options, callback) => {
renderModuleFactory(AppServerModuleNgFactory, {
// Our index.html
document: template,
url: options.req.url,
// DI so that we can get lazy-loading to work differently (since we need it to just instantly render it)
extraProviders: [
provideModuleMap(LAZY_MODULE_MAP)
]
}).then(html => {
callback(null, html);
});
});
app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));
// Server static files from /browser
app.get('*.*', express.static(join(DIST_FOLDER, 'browser')));
// All regular routes use the Universal engine
app.get('*', (req, res) => {
res.render(join(DIST_FOLDER, 'browser', 'index.html'), { req });
});
// Start up the Node server
app.listen(PORT, () => {
console.log(`Node server listening on http://localhost:${PORT}`);
});
当我使用 npm run build:ssr && npm runserve:ssr 命令运行 server.js 时出现的警告如下 ./node_modules/@angular/platform-server/fesm5/platform-server.js 中的警告 410:26-50“在‘@angular/common/http’中找不到导出‘ɵHttpInterceptingHandler’ @ ./node_modules/@angular/platform-server/fesm5/platform-server.js @ ./server.ts
./node_modules/@angular/platform-server/fesm5/platform-server.js 中的警告 852:35-51“在‘@angular/common’中找不到导出‘ViewportScroller’ @ ./node_modules/@angular/platform-server/fesm5/platform-server.js @ ./server.ts
./node_modules/@angular/platform-server/fesm5/platform-server.js 中的警告 852:63-84“在‘@angular/common’中找不到导出‘ɵNullViewportScroller’ @ ./node_modules/@angular/platform-server/fesm5/platform-server.js @ ./server.ts
最佳答案
import { isPlatformBrowser, isPlatformServer } from
'@angular/common';
import { PLATFORM_ID, Inject } from '@angular/core';
export class Component
{
constructor(@Inject(PLATFORM_ID) private platformId: Object)
{
this.PutJquery()
}
PutJquery()
{
if (isPlatformBrowser(this.platformId)) {
var authData = $.cookie('data');
}
}
}
关于angular - 元标记未在查看源代码中更新 - Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52094234/