[英]Angular 5 + Popper.JS
I'm trying to launch training project that requires Popper.Js in its features. 我正在尝试启动需要Popper.Js功能的培训项目。 However, it doesn't seem to let me just start to use Popper.Js.
但是,它似乎并没有让我开始使用Popper.Js。
According to Bootstrap's docs I start Popper.Js by 根据Bootstrap的文档,我开始使用Popper.Js
$(function () {
$('[data-toggle="tooltip"]').tooltip();
});
I use it in main component in ngOnInit
. 我在
ngOnInit
主要组件中使用它。 However, it doesn't work. 但是,它不起作用。 The error occurs " Property 'tooltip' does not exist on type 'JQuery' ".
发生错误“ 属性'工具提示'在类型'JQuery'上不存在 ”。 I imported jquery and bootstrap bundle file.
我导入了jquery和bootstrap包文件。 Types for all the libraries are installed.
安装所有库的类型。
I also tried the pure "popper.js" instead of bootstrap.bundle.js. 我也试过纯粹的“popper.js”而不是bootstrap.bundle.js。 But the same error occurs.
但是会发生同样的错误。 Bootstrap.bundle.js(includes bootstrap and popper.js in right order) and jquery are imported in angular-cli.json.
Bootstrap.bundle.js(包括正确顺序的bootstrap和popper.js)和jquery在angular-cli.json中导入。
Run the following command to install bootstrap jquery & popper.js: 运行以下命令以安装bootstrap jquery和popper.js:
npm install bootstrap@4.0.0-beta.2 popper.js jquery --save
In angular-cli.json add these lines: 在angular-cli.json中添加以下行:
"styles": [
"styles.css",
"../node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/popper.js/dist/popper.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
],
In your page.ts Add: 在你的page.ts中添加:
declare var $;
Edit: 编辑:
Not all JavaScript libraries/frameworks have TypeScript declaration files. 并非所有JavaScript库/框架都具有TypeScript声明文件。 On the other hand, we might want to use libraries/frameworks in our TypeScript files without getting compilation errors.
另一方面,我们可能希望在TypeScript文件中使用库/框架而不会出现编译错误。 One solution is to use the declare keyword.
一种解决方案是使用declare关键字。 The declare keyword is used for ambient declarations where you want to define a variable that may not have originated from a TypeScript file.
declare关键字用于环境声明,您可以在其中定义可能不是源自TypeScript文件的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.