2017-06-22 64 views
0

我在更新之中,以角4.1.3角断裂,迁移至4.1.3

我们之前使用BroweserDomAdapter重新调整我们的角度服务之一,但已经看到了由角队弃用,我正在努力弄清楚重写它之后的问题。

我在做什么错:

import { Injectable }  from '@angular/core'; 
import { Component, Inject } from '@angular/core'; 
import { DOCUMENT } from '@angular/platform-browser'; 
import { Title }    from '@angular/platform-browser'; 
import { BrowserDomAdapter } from '@angular/platform-browser/src/browser/browser_adapter'; 

@Injectable() 
export class SeoService { 
/** 
* Angular 2 Title Service 
*/ 

/** 
* <head> Element of the HTML document 
*/ 
private headElement: HTMLElement; 
/** 
* <meta name="description"> Element of the document head 
*/ 
private metaDescription: HTMLElement; 
/** 
* <meta name="robots"> Element of the document head 
*/ 
private robots: HTMLElement; 
dom:any; 
/** 
* Inject the Angular 2 Title Service 
* @param titleService 
*/ 
constructor (@Inject(DOCUMENT) private titleService: Title){ 
    this.titleService = titleService; 

    /** 
    * get the <head> Element 
    * @type {any} 
    */ 
    this.dom = document; 

    this.headElement = this.dom.getSelection('head'); 
    this.metaDescription = this.getOrCreateMetaElement('description'); 
    this.robots = this.getOrCreateMetaElement('robots'); 
} 

public getTitle(): string { 
    return this.titleService.getTitle(); 
} 

public setTitle(newTitle: string) { 
    this.titleService.setTitle(newTitle + ' | Stareable'); 
} 

public getMetaDescription(): string { 
    return this.metaDescription.getAttribute('content'); 
} 

public setMetaDescription(description: string) { 
    this.metaDescription.setAttribute('content', description); 
} 

public getMetaRobots(): string { 
    return this.robots.getAttribute('content'); 
} 

public setMetaRobots(robots: string) { 
    this.robots.setAttribute('content', robots); 
} 

/** 
* get the HTML Element when it is in the markup, or create it. 
* @param name 
* @returns {HTMLElement} 
*/ 
private getOrCreateMetaElement(name: string): HTMLElement { 
    let el: HTMLElement; 
    el = this.dom.getSelection('meta[name=' + name + ']'); 
    if (el === null) { 
     el = this.dom.createElement('meta'); 
     el.setAttribute('name', name); 
     this.headElement.appendChild(el); 
    } 
    return el; 
    } 
} 

这里是控制台的错误:

错误错误:未捕获的(在承诺):类型错误:this.titleService.setTitle不是一个函数 类型错误:this.titleService.setTitle不是一个函数 在SeoService.setTitle

谢谢!

回答

0

解决您的构造函数:

constructor (@Inject(DOCUMENT) private document: any, private titleService: Title){ 

    /** 
    * get the <head> Element 
    * @type {any} 
    */ 
    this.dom = document; 

    this.headElement = this.dom.getSelection('head'); 
    this.metaDescription = this.getOrCreateMetaElement('description'); 
    this.robots = this.getOrCreateMetaElement('robots'); 
} 

声明在构造函数私人PARAMS是整个组件可见。

+0

现在越来越:this.metaDescription.setAttribute不是一个函数 – hisairnessag3

+0

如何重构getOrCreateMetaElement();? – hisairnessag3

1

我不得不改变了原生的JavaScript函数 “getSelection” 的方法:

this.headElement = this.dom.getElementsByTagName("head")[0]; 

和:

el = this.dom.querySelector("meta[name=\'"+ name + "\']"); 

后,一切又合作。