javascript - Angular - 如何将 Javascript 导入 Angular 组件
问题描述
在我的 Angular-11 中,我有这个 Javascript 文件:
“node_modules/admin-lte/plugins/bs-stepper/js/bs-stepper.min.js”,
如上所示,我将其添加到 angular.json 中。
import Stepper from '...';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
name = 'Angular';
private stepper: Stepper;
next() {
this.stepper.next();
}
onSubmit() {
return false;
}
ngOnInit() {
this.stepper = new Stepper(document.querySelector('#stepper1'), {
linear: false,
animation: true
})
}
}
我如何将它导入到这个组件中:profile.component.ts 这种方式,
从'...'导入步进器;
从 Javascript 路径
谢谢
解决方案
您必须首先在 typing.d.ts 中声明它并包含 angular.json 脚本。
在 angular.json
{
"build" : {
"scripts" : [
"node_modules/admin-lte/plugins/bs-stepper/js/bs-stepper.min.js",
....
]
在打字.d.ts
declare module 'admin-lte/plugins/bs-stepper/js/bs-stepper.min';
注意:如果这是一个 JQuery 包,那么您需要创建一个接口。
declare module 'jquery';
interface JQuery {
Stepper(DOM : any, options?: any): any;
}
最后你现在可以在组件中调用它了。
在组件中
import Stepper from 'admin-lte/plugins/bs-stepper/js/bs-stepper.min';
编辑:typing.d.ts
在 src 文件夹中创建一个名为的文件。然后加
/// <reference path = "typings.d.ts" />
到src/main.ts
文件顶部
推荐阅读
- r - R中无需替换的惰性随机数生成
- c# - DTO 中是否有用于数据转换的注释?
- php - 在 Google App Engine 柔性环境 PHP (Laravel) 上启用 CORS
- ruby - Ruby 编译时出现 ocra 错误 - 找不到 libssp-0.dll
- javascript - Pressmove in javascript ,有没有办法检测鼠标或手指触摸移动的元素?
- swift - 应用内购买沙盒环境不工作
- vue.js - vuetify中如何支持大屏自定义tile?
- ngxs - 带有注入的选择器
- java - 最长路径算法 排序路径查找
- python - 为什么Python 3.6默认不安装wheel包?