【问题标题】:ApiAIPromises is not defined ( ionic 3, dialogflow)ApiAIPromises 未定义(离子 3,对话框流)
【发布时间】:2018-11-10 17:16:11
【问题描述】:

我在我的 ionic 应用程序中使用了对话流。这是我的 .ts 文件。

    import { Component, NgZone } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

declare var ApiAIPromises: any;

@IonicPage()
@Component({
  selector: 'page-chat-box',
  templateUrl: 'chat-box.html',
})
export class ChatBoxPage {

  answer;

  constructor(public navCtrl: NavController,
      public navParams: NavParams,
      public ngZone: NgZone) 
      {
        ApiAIPromises.init({
        clientAccessToken: "xxxxxxxxxxxx"
      })
      .then((result) =>  console.log(result))

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ChatBoxPage');
  }
  goBack() {
    this.navCtrl.pop();
  }

  ask(question) {
    ApiAIPromises.requestText({
      query: question
    })
    .then(({result: {fulfillment: {speech}}}) => {
       this.ngZone.run(()=> {
         this.answer = speech;
       });
    })
  }

}

我也安装了插件

ionic cordova 插件添加 cordova-plugin-apiai

当我尝试运行它时,我收到如下所述的错误。

未捕获(在承诺中):ReferenceError:未定义 ApiAIPromises ReferenceError:未定义 ApiAIPromises 在新的 ChatBoxPage (chat-box.ts:19) 在 createClass (core.js:12443) 在 createDirectiveInstance (core.js:12284) 在 createViewNodes (core.js:13742) 在 createRootView (core.js:13631) 在 callWithDebugContext (core.js:15056) 在 Object.debugCreateRootView [as createRootView] (core.js:14339) 在 ComponentFactory_.create (core.js:11236) 在 ComponentFactoryBoundToModule.create (core.js:4023) 在 Tab.NavControllerBase.viewInit (nav-controller-base.js:441) 在新的 ChatBoxPage (chat-box.ts:19) 在 createClass (core.js:12443) 在 createDirectiveInstance (core.js:12284) 在 createViewNodes (core.js:13742) 在 createRootView (core.js:13631) 在 callWithDebugContext (core.js:15056) 在 Object.debugCreateRootView [as createRootView] (core.js:14339) 在 ComponentFactory.create (core.js:11236) 在 ComponentFactoryBoundToModule.create (core.js:4023) 在 Tab.NavControllerBase._viewInit (nav-controller-base.js:441) 在 c (polyfills.js:3) 在 Object.reject (polyfills.js:3) 在 Tab.NavControllerBase._fireError (nav-controller-base.js:223) 在 Tab.NavControllerBase._failed (nav-controller-base.js:216) 在 nav-controller-base.js:263 在 t.invoke (polyfills.js:3) 在 Object.onInvoke (core.js:4749) 在 t.invoke (polyfills.js:3) 在 r.run (polyfills.js:3) 在 polyfills.js:3 defaultErrorLogger @ core.js:1448 ErrorHandler.handleError @ core.js:1509 IonicErrorHandler.handleError @ ionic-error-handler.js:61 下一个 @core.js:5497 schedulerFn @ core.js:4331 SafeSubscriber.__tryOrUnsub @Subscriber.js:239 SafeSubscriber.next @ Subscriber.js:186 Subscriber._next @ Subscriber.js:126 Subscriber.next @ Subscriber.js:90 Subject.next @ Subject.js:55 EventEmitter.emit@core.js:4311(匿名)@ core.js:4771 t.invoke@polyfills.js:3 r.run@polyfills.js:3 NgZone.runOutsideAngular @ core.js:4697 onHandleError @ core.js:4771 t.handleError @ polyfills.js:3 r.runGuarded @ polyfills.js:3 (匿名)@ polyfills.js:3 n.microtaskDrainDone @ polyfills.js:3 o @ polyfills.js:3 e.invokeTask @ polyfills.js:3 p @ polyfills.js:2 v @ polyfills.js:2

【问题讨论】:

  • 当你想使用cordova插件时,在this.platform.ready().then(()=>{})内进行
  • 然后显示错误,因为 ApiAIPromises 未定义
  • 您正在尝试使用设备?
  • 不,我正在离子实验室视图中测试它
  • cordova 不适用于离子实验室...使用设备/模拟器

标签: angular typescript ionic-framework cordova-plugins dialogflow-es


【解决方案1】:

ApiAIPromises 变量在 Cordova 插件中有引用。因此,使用 ionic serve 在浏览器中运行时会出现“未定义”错误。

在设备或模拟器上运行时不会报错。

【讨论】:

  • 请考虑在您的答案中添加更多信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多