【问题标题】:Typescript Twitter Bootstrap not injectable?Typescript Twitter Bootstrap 不可注入?
【发布时间】:2015-11-18 10:16:17
【问题描述】:

我试过了

tsd install jquery --save
tsd install bootstrap --save

而 tsd.json 会是

 {
   "version": "v4",
   "repo": "borisyankov/DefinitelyTyped",
   "ref": "master",
   "path": "typings",
   "bundle": "typings/tsd.d.ts",
   "installed": {
     "jquery/jquery.d.ts": {
       "commit": "c5a2f44bab06cf9f2bcc14530171daac1cebff6c"
     },
     "bootstrap/bootstrap.d.ts": {
       "commit": "c5a2f44bab06cf9f2bcc14530171daac1cebff6c"
     }
   }
 }

而 tsd.d.ts 是

/// <reference path="jquery/jquery.d.ts" />
/// <reference path="bootstrap/bootstrap.d.ts" />

所以,我认为我可以做到

/// <reference path="../../typings/tsd.d.ts" />

import $ = require('jquery');
$('#modal').modal();

但是我收到了这个警告。

Uncaught TypeError: t(...).modal is not a function

有什么想法吗?

【问题讨论】:

  • 这应该像你写的那样工作,实际的错误是什么?你写的 t(...).modal 不是一个函数。但我没有看到 t(...)
  • 编译后,浏览器控制台显示如下消息Uncaught TypeError: t(...).modal is not a function。我想这意味着引导程序没有注入 jQuery :(

标签: twitter-bootstrap-3 typescript typescript1.5


【解决方案1】:

提供的代码示例:

import $ = require('jquery');
$('#modal').modal();

工作正常as shown in the test file

【讨论】:

    【解决方案2】:

    TypeScript 仅针对开发时间。如果您遇到运行时问题,可能是因为您没有包含一些脚本。

    【讨论】:

    • 你知道我应该如何在其中包含未包含的脚本吗? :s
    • 和你在项目中添加jquery的方法一样(可能在你的index.html中),添加缺少的文件(可能是bootstrap文件)
    【解决方案3】:

    您提供的说明:

    tsd install jquery --save

    tsd install bootstrap --save

    为我工作。我的主要组件 (app.component.ts) 中有以下内容:

    declare var $:JQueryStatic

    我有一个模态组件,我使用以下内容没有任何抱怨:

    @ViewChild('modal') modal: ElementRef

    ngAfterViewInit() { $(this.modal).modal() }

    我希望它也对你有用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      相关资源
      最近更新 更多