在angular4的项目中需要使用bootstrap的tooltip插件。

1. 使用命令安装jQuery和bootstrap

npm install bootstrap jquery --save

2. 安装了bootstrap和jQuery之后,需要在.angular-cli.json中设置对jQuery和bootstrap的引用。

...
"styles": [
"styles/bootstrap.scss",
"styles.scss",
],
"scripts": [
"../node_modules/jquery/dist/jquery.js",
"../node_modules/jqueryui/jquery-ui.js",
"../node_modules/bootstrap/dist/js/bootstrap.js",
]
...

3. 使用directive来定义一个可共用的属性指令。

import { AfterViewInit, Directive, ElementRef, HostBinding, Input, OnDestroy } from '@angular/core';

/**
* @see https://getbootstrap.com/docs/3.3/javascript/#tooltips
*/
@Directive({
selector: '[appTooltip]'
})
export class TooltipDirective implements AfterViewInit, OnDestroy { // @HostBinding('attr.data-toggle')
// readonly dataToggle = 'tooltip'; @Input()
@HostBinding('attr.data-placement')
appTooltipPlacement = 'bottom'; @Input()
@HostBinding('title')
appTooltip: string; constructor(private elementRef: ElementRef) {
} ngAfterViewInit(): void {
// bugfix: 使用 container: 'body' 可以避免在 btn-group 时由于插入了 tooltip 后,
// 最后一个 button 不满足 :not(:last-child) 时导致的圆角消失的 bug
$(this.elementRef.nativeElement).tooltip({
container: 'body'
} as any);
} ngOnDestroy(): void {
$(this.elementRef.nativeElement).tooltip('destroy');
}
}

4. 在app.module.ts中声明这个directive,需要在declarations和exports中引入

...
import {TooltipDirective} from './common/directives/tooltip.directive'; @NgModule({
declarations: [
AppComponent,
...
TooltipDirective
],
imports: [
BrowserModule, FormsModule, ...
],
exports: [TooltipDirective],
entryComponents: [
....
],
providers: [...],
bootstrap: [AppComponent]
})
export class AppModule {
constructor() {
...
}
}

5.html页面里面使用[appTooltip] 来使用这个directive。

<button type="button" class="btn btn-default" (click)="new.emit()" appTooltip="新建">
<i class="i-new"></i>
</button>

5. 不过这里出现一个报错。

Property 'tooltip' does no t exist on type 'JQuery<HTMLElement>

检查了很久,后来找到了问题,没有声明$。

需要在tooltip.directive.ts文件中加上

declare let $: any;

,然后就可以正常使用了。

6.要在项目内全局设置jQuery的引用,在tsconfig.json文件中配置红色显示的部分

{
"compileOnSave": true,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noStrictGenericChecks": false,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"types": [
"jasmine",
"node",
"jquery",
"jqueryui"

],
"lib": [
"es2017",
"dom"
]
}
}

在angular项目中使用bootstrap的tooltip插件时,报错Property 'tooltip' does no t exist on type 'JQuery<HTMLElement>的解决方法和过程的更多相关文章

  1. 解决在Vue项目中时常因为代码缩进导致页面报错的问题

    前言 如果我们初次使用vue-cli来构建单页SPA应用,在撸代码的过程中有可能会遇到这种因为代码缩进导致 页面报错的问题,导致我们烦不胜烦.接下来我们就来看一看如何解决这个小问题... erro原因 ...

  2. 解决 maven 项目中加入了 lombok 库后依然报错的问题

    平时我们采用 maven 引入第三方库,可以方便的管理第三方 jar 包,然加入 lombok 后启动 eclipse 依然报错,这是由于 lombok 是通过反射在运行时自动生成 getter(). ...

  3. 【Kotlin】spring boot项目中,在Idea下启动,报错@Configuration class 'BugsnagClient' may not be final.

    报错如下: Exception encountered during context initialization - cancelling refresh attempt: org.springfr ...

  4. angular项目线上地址跳转或刷新报错的解决

    引用地址:https://blog.csdn.net/qq_35415307/article/details/80707463 本地ng项目没问题,到了线上跳转刷新都会报404错误,相信这个问题每个做 ...

  5. 关于Eclipse中使用Maven进行Install安装时候报错Perhaps you are running on a JRE rather than a JDK?解决办法

    所遇到的问题: 详情报错: 英文描述: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3. ...

  6. Eclipse检出原MyEclipse项目后 javax.servlet.http相关类都报错【我,体现着一类jar包问题的处理方法】

    用Eclipse检出原来为myEclipse搭建的一个项目,检出后,所有关于httpservlet的类都报异常,说有没实现的方法? 但这个项目之前人家用MyEclipse运行都是没有问题的, 按住CT ...

  7. vue-cli+webpack在生成的项目中使用bootstrap方法(二)

    vue-cli+webpack在生成的项目中使用bootstrap方法(一)中,是通过手动下载bootstrap库,然后手动添加到src/assets中,显然是过程太多. 当然是可以更省力些,可以通过 ...

  8. gulp 在 angular 项目中的使用

    gulp 在 angular 项目中的使用 keyword:gulp,angularjs,ng,ngAnnotate,jshint,gulpfile 最后附完整简洁的ng项目gulpfile.js 准 ...

  9. 在 React项目中使用 bootstrap

    在使用create-react-app 创建的项目中使用 bootstrap; 安装react-bootstrap; npm install react-bootstrap --savenpm ins ...

随机推荐

  1. 【SPOJ8222】Substrings (后缀自动机)

    题意: 给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值. 求F(1)..F(Length(S)) Length(S) <= 250000 思路:板子中st[x]定义为r ...

  2. c#蜘蛛

    C#写一个采集器 using System; using System.Collections.Generic; using System.Text; using System.Net; using ...

  3. NOIp 数据结构专题总结 (2):分块、树状数组、线段树

    系列索引: NOIp 数据结构专题总结 (1) NOIp 数据结构专题总结 (2) 分块 阅:<「分块」数列分块入门 1-9 by hzwer> 树状数组 Binary Indexed T ...

  4. 2016亚洲城市GDP50强出炉

    2017年年1月,中国各省GDP排名,台湾排第6:广东,江苏,山东,浙江,河南,台湾,四川,湖北,河北,湖南,我国台湾地区去年的GDP增长率为1.4%,总量折合人民币约为37329.1亿元,加入全国榜 ...

  5. maven 国内加速,修改镜像源

    为什么慢 由于默认情况下执行 mvn 各种命令是去国外的 mvn 官方镜像源获取需要安装的具体软件信息,所以在不使用代理.不翻墙的情况下,从国内访问国外服务器的速度相对比较慢 如何修改镜像源 阿里旗下 ...

  6. Weblgic安装应用报错:Caused by: com.bea.xml.XmlException: failed to load java type corresponding to e=web-a

    文章目录 报错如下 解决: 报错如下 Exception in AppMerge flows' progression 后台日志报错: Caused by: com.bea.xml.XmlExcept ...

  7. mongodb写入策略(WriteConcern)

    写入策略(WriteConcern) mongodb的写入策略有多种方式,写入策略是指当客户端发起写入请求后,数据库什么时候给应答,mongodb有三种处理策略:客户端发出去的时候,服务器收到请求的时 ...

  8. 九条命令检查Linux服务器性能

    一.uptime命令 这个命令可以快速查看机器的负载情况.在Linux系统中,这些数据表示等待CPU资源的进程和阻塞在不可中断IO进程(进程状态为D)的数量.这些数据可以让我们对系统资源使用有一个宏观 ...

  9. cesium加载gltf模型

    cesium加载gltf模型 一.采用vue-cesium:在项目里加载依赖包.命令如下: npm i --save vue-cesium 在main.js中加入如下代码: https://www.n ...

  10. Mybatis简介与原理

    经常面试别人或者被面试,对Mybatis简介与原理这个问题的回答千差万别,为了更好的服务与以后,来个原理介绍. 什么是Mybatis MyBatis 本是apache的一个开源项目iBatis, 20 ...