在angular项目中使用bootstrap的tooltip插件时,报错Property 'tooltip' does no t exist on type 'JQuery<HTMLElement>的解决方法和过程
在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>的解决方法和过程的更多相关文章
- 解决在Vue项目中时常因为代码缩进导致页面报错的问题
前言 如果我们初次使用vue-cli来构建单页SPA应用,在撸代码的过程中有可能会遇到这种因为代码缩进导致 页面报错的问题,导致我们烦不胜烦.接下来我们就来看一看如何解决这个小问题... erro原因 ...
- 解决 maven 项目中加入了 lombok 库后依然报错的问题
平时我们采用 maven 引入第三方库,可以方便的管理第三方 jar 包,然加入 lombok 后启动 eclipse 依然报错,这是由于 lombok 是通过反射在运行时自动生成 getter(). ...
- 【Kotlin】spring boot项目中,在Idea下启动,报错@Configuration class 'BugsnagClient' may not be final.
报错如下: Exception encountered during context initialization - cancelling refresh attempt: org.springfr ...
- angular项目线上地址跳转或刷新报错的解决
引用地址:https://blog.csdn.net/qq_35415307/article/details/80707463 本地ng项目没问题,到了线上跳转刷新都会报404错误,相信这个问题每个做 ...
- 关于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. ...
- Eclipse检出原MyEclipse项目后 javax.servlet.http相关类都报错【我,体现着一类jar包问题的处理方法】
用Eclipse检出原来为myEclipse搭建的一个项目,检出后,所有关于httpservlet的类都报异常,说有没实现的方法? 但这个项目之前人家用MyEclipse运行都是没有问题的, 按住CT ...
- vue-cli+webpack在生成的项目中使用bootstrap方法(二)
vue-cli+webpack在生成的项目中使用bootstrap方法(一)中,是通过手动下载bootstrap库,然后手动添加到src/assets中,显然是过程太多. 当然是可以更省力些,可以通过 ...
- gulp 在 angular 项目中的使用
gulp 在 angular 项目中的使用 keyword:gulp,angularjs,ng,ngAnnotate,jshint,gulpfile 最后附完整简洁的ng项目gulpfile.js 准 ...
- 在 React项目中使用 bootstrap
在使用create-react-app 创建的项目中使用 bootstrap; 安装react-bootstrap; npm install react-bootstrap --savenpm ins ...
随机推荐
- 【项目管理和构建】——Maven简介(一)
在现实的企业中,以低成本.高效率.高质量的完成项目,不仅仅需要技术大牛,企业更加需要管理大牛,管理者只懂技术是远远不够的.当然,管理可以说有很多的方面,例如:对人员的管理,也有对项目的管理等等.如果你 ...
- ACM中java的使用 (转)
ACM中java的使用 这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入: 格式为:Scanner ...
- vux使用方法
# 使用vux及vuex-i18n需要做的工作 ### 1.首先需要安装vux ### 2.需要安装vux-loader ### 3.需要安装vuex ### 4.需要安装vuex-i18n ### ...
- iview+vue 使用中遇到的问题(分页)
1.分页默认页数 当页面只有一个功能需要分页组件时,引用iview分页组件当然没问题.当一个页面中有多个需要分页组件的时候,便容易出现问题.例如:在项目中有多个不同的表格需要分页功能,几个表格共用一个 ...
- php优化及高效提速问题小结
一. 在函数中,传递数组时使用 return 比使用 global 要高效,比如: function userloginfo($usertemp){ $detail=explode("&qu ...
- CET-6 分频周计划生词筛选(Week 2)
点我阅读 Week 2 2016.09.04/05 p58 ongoing / forward p59 prosperity p60 rear p61 rival + segregation + se ...
- shell zip和unzip压缩和解压,压缩效率
1.把/home目录下面的mydata目录压缩为mydata.zip zip -r mydata.zip mydata #压缩mydata目录zip -r mydata.zip ./*txt #压缩当 ...
- python 装饰器 第三步:使用语法糖
# 第三步:使用语法糖(就是语法) # 用于扩展基本函数的函数 def kuozhan(func): # 扩展功能1 print('饭前要洗手') # 调用基本函数 func() # 扩展功能2 pr ...
- Linux下实现客户端和服务器端的通信
首先,可以将代码复制下来放到U盘里,然后挂载到Linux上 挂载步骤 找到设备->USB->你U盘的名字 挂载成功 访问U盘把代码拷贝到home文件夹下,就可以直接进行编译. client ...
- 金额格式化,例子:fmoney("12345.675910", 3),返回12,345.676
/** * 金额格式化 * 例子:fmoney("12345.675910", 3),返回12,345.676 * @data 备注lhh 2016-09-18 */ functi ...