angular - 尝试在带有 IWindow 接口(interface)的 Angular2 中使用 speechSynthesis

标签 angular interface window angular2-services speech-synthesis

我正在尝试使用 speechSynthesis http://blog.teamtreehouse.com/getting-started-speech-synthesis-api

首先我用一个界面扩展了窗口:

窗口界面.ts

export interface IWindow extends Window {
  webkitSpeechRecognition: any;
  speechSynthesis: any;
}

接下来我做了一个窗口服务:

窗口服务.ts

import { Injectable } from '@angular/core';
import { IWindow } from '../interfaces/window-interface';

function _window() : IWindow {
   return window;
}
@Injectable()
export class WindowService {
   get nativeWindow() : any {
      return _window();
   }
}

现在在组件中,我正在尝试实现它....没有成功..

应用组件

import { Component, OnInit, ViewChild } from '@angular/core';
import { WindowService } from '../../providers/window.service';
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['.app.component.scss']
})
export class AppComponent implements OnInit {
   constructor(private _router: Router, private _window: WindowService ) { }

   sayIt() {
      var utterance = new SpeechSynthesisUtterance('Hello World');
      this._window.speechSynthesis.speak(utterance);
   }
}

TS 错误:

Cannot find name 'SpeechSynthesisUtterance'.)
Property 'speechSynthesis' does not exist on type 'WindowService'.)

我还使用这篇关于语音识别的文章作为引用:Angular2: Web Speech API - Voice recognition

仍然出现错误—— 类型“Window”上不存在属性“speechSynthesis”

最佳答案

可以这样操作

 var msg = new SpeechSynthesisUtterance("hello world");
 (<any>window).speechSynthesis.speak(msg)

从链接中获得帮助

SpeechRecognition and SpeechSynthesis in TypeScript

关于angular - 尝试在带有 IWindow 接口(interface)的 Angular2 中使用 speechSynthesis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42802007/

相关文章:

angular - 如何从有 promise 的函数返回可观察的

c - 在库中隐藏库

database - Golang 使用接口(interface)实现数据库函数

c++ - 等待某个网站被访问

javascript - 使用 JavaScript 或 jQuery 唯一标识当前打开的每个单独的浏览器窗口?

c++ - 检查窗口标题是否可见

angular - 检测到循环依赖项中的警告 - Angular Cli

使用 Subscribers 和 subscribe.first() 时 Angular2+ 内存泄漏

javascript - NGRX:创建多个 reducer react 的 "global"操作的正确方法?

java - 在接口(interface)中添加默认和静态方法的原因