gpt4 book ai didi

jquery - Angular-cli bootstrap v3 和 jquery? Angular 2.0

转载 作者:太空狗 更新时间:2023-10-29 18:14:14 25 4
gpt4 key购买 nike

所以我即将开始一个项目,我认为 Angular 2.0 似乎是一个非常可靠的框架:路由、组件、指令等。但是,已经有很多我想使用的使用 bootstrap v3 和 jQuery 构建的东西。我想知道是否可以使用 Angular-cli 制作的结构来集成 bootstrap v3 和 JQuery?

顺便说一句,我见过其他使用 Gulp 的 Angular 2 种子,但他们总是推荐使用官方的 Angular-cli。

此外,我还不知道如何将现有的 JS 库放入 Typescript 中,以及 jquery 的 $ global 是否会干扰 typescript。

注意:我知道 ng2-bootstrap,但我认为并非所有内容都与此方法兼容,因为它不使用 jquery 作为依赖项。我也知道在 angular 中使用 jquery 不是最佳实践,但我认为在某些情况下您确实需要它作为插件或依赖项。

最佳答案

在他们获得对第 3 方库的更好支持之前,我可以建议您一个解决方法。它对我有用:)

输入后

npm install jquery 

 typings install jquery --ambient --save

在你的 angular-cli-build.json 中,确保它保持这样

module.exports = function(defaults) {
return new Angular2App(defaults, {
vendorNpmFiles: [
...
'jquery/**/*.js'
]
});
};

在你的 system-config.ts 中:

/** Map relative paths to URLs. */
const map: any = {
'jquery': 'vendor/jquery'
};

在你的 src/index.html 添加这一行

<script src="/vendor/jquery/dist/jquery.min.js" type="text/javascript"></script>

现在在你想要使用 jquery 的组件中,这样写

declare var $:any;

@Component({
})
export class YourComponent {
ngOnInit() {
$.("button").click(function(){
// now you can DO, what ever you want
});
console.log();
}
}

在我的项目中,我就是这样做的。希望它可以帮助你 :) 。

关于jquery - Angular-cli bootstrap v3 和 jquery? Angular 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37742151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com