元标记未在视图源中更新-角度

时间:2018-08-30 09:59:23

标签: angular angular6 meta-tags angular-universal

我正在尝试更新角度6和角度univarsal中的元标记,但是它仅在检查元素中更改,而在视图页面源中却没有变化,因此它与主页上的相同。

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

当我使用npm run build:ssr && npm run serve: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

1 个答案:

答案 0 :(得分:0)

const create_request = ( url, payload ) => request('POST', url, { json: payload } )
.then((res) => {
  let response = JSON.parse(res.getBody('utf8'));
  DialogflowHelper.logout_user(auth_token);
  if(response['IsSuccess'] == true){
    return "Success String"
  }
  else if(response['Description']){
    return response['Description']
  }
  else{
    return BOOKING_ENGINE_FAILURE_MESSAGE;
  }
});

create_request().then( str => {
  // do something with the string we returned in the previous promise handler.
});