Метатег не обновляется в исходном представлении - угловой - PullRequest
0 голосов
/ 30 августа 2018

Я пытаюсь обновить метатег в angular 6 и angular univarsal, но он изменяется только в элементе Inspect, а не в источнике страницы просмотра. Он остается тем же, что и на домашней странице.

Homepage.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');
    }

}

about.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 })
  }

}

server.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}`);
});

Предупреждения, которые выдаются мне при запуске server.js с использованием команды npm run build: ssr && npm run serve: ssr, приведены ниже. ПРЕДУПРЕЖДЕНИЕ в ./node_modules/@angular/platform-server/fesm5/platform-server.js 410: 26-50 "экспорт" ttHttpInterceptingHandler "не найден в" @ angular / common / http " @ ./node_modules/@angular/platform-server/fesm5/platform-server.js @ ./server.ts

ПРЕДУПРЕЖДЕНИЕ в ./node_modules/@angular/platform-server/fesm5/platform-server.js 852: 35-51 "экспорт" ViewportScroller "не найден в" @ angular / common " @ ./node_modules/@angular/platform-server/fesm5/platform-server.js @ ./server.ts

ПРЕДУПРЕЖДЕНИЕ в ./node_modules/@angular/platform-server/fesm5/platform-server.js 852: 63-84 "экспорт" NullViewportScroller "не найден в" @ angular / common " @ ./node_modules/@angular/platform-server/fesm5/platform-server.js @ ./server.ts

1 Ответ

0 голосов
/ 30 августа 2018
       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');
        }
        }
      }
...