2017-06-22 5 views
0

に私たちは前にBroweserDomAdapterを使用しますがそれは、角度チームによって廃止されて見ていると、4.1.3角度角度ブレイク、移行4.1.3

に更新の真っ只中に私達の角度のいずれかのサービスを再加工しています私はそれを書き直した後の問題を把握するのに苦労しています。私が間違って何をやっている

:ここ

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

は、コンソールでのエラーです:

ERRORエラー:(約束で)キャッチされない:例外TypeError:this.titleService.setTitleが機能 TypeError例外ではありません:this.titleService.setTitleは関数ではありません 、SeoService.setTitle

ありがとうございました!

答えて

0

はあなたのコンストラクタを修正:あなたのコンストラクタでプライベートのparamsを宣言

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

は、コンポーネント全体に閲覧できます。

+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 + "\']"); 

そのすべてが再び働いた後。

関連する問題