问题描述
我正在使用Angular2.我想实现可折叠的小组面板.为此,有许多使用JQuery的示例.但是,我想使用原始javascript来实现它.要使用JQuery,我也必须导入JQuery库,这可能会导致一些额外的加载.我认为除了小组讨论之外,未来的开发中可能还会有一些DOM操作.我想知道使用JavaScript或JQuery哪个更好.我知道他们之间需要权衡.在性能方面,建议采用哪种方式.推荐同时使用Angular2和JQuery吗?请给我一些建议.
I am using Angular2. I would like to implement group panel collapsable. Toward it, there are a lot of examples using JQuery. However, I would like to implement it using original javascript. To use JQuery, I have to import the JQuery library too which might cause some additional loading. I think there may be some DOM manipulation in my future development also besides group panel. I would like to know which is better using javascript or JQuery. I know there is some trade-off between them. In the respect of performance, which way would be recommended. Using Angular2 and JQuery together is recommended approach? Please give me some advice.
推荐答案
Angular希望控制DOM,其关于页面和数据交互的原理与jQuery的混合效果很差(在jQuery中,您倾向于创建一个许多直接的DOM修改,而在Angular中,DOM几乎是作用域数据的副作用表示;在Angular的预期路径之外进行的DOM修改会在下一个摘要中被吹走,或者会覆盖或破坏Angular依赖的绑定上.)
Angular expects to be in control of the DOM, and its philosophy of how the page and the data interact mixes poorly with jQuery (in jQuery you tend to make a lot of direct DOM modifications, while in Angular the DOM is almost a side-effect representation of the scope data; modifications to the DOM made outside Angular's expected path can get blown away on the next digest, or can overwrite or destroy bindings that Angular depends on.)
通过jQuery单独操作DOM是可能,只要您格外小心不要干扰Angular生命周期-但是除非您对Angular的DOM有非常深入的了解,否则它很容易出错.生命周期以及更改的安全性和安全性.几乎没有必要;使用Angular自己的工具几乎总是更好.
Manipulating the DOM separately through jQuery is possible, so long as you use great care not to interfere with the Angular lifecycle -- but it's highly error prone unless you have a very strong understanding of Angular's DOM lifecycle and of what is and isn't safe to change. It's rarely necessary; it's almost always better to use Angular's own tools.
(还要注意Angular包括jqLite;在很多时候,您正在寻找的jQuery函数已经可用,而无需安装完整的库.)
(Also note that Angular includes jqLite; much of the time the jQuery function you're looking for is already available without needing to install the full library.)
(如果您必须使用jQuery,请将其保留在特定Components的afterViewInit
函数中,并且不要尝试从另一个组件中修改一个组件的结果.)
(If you must use jQuery, keep it within specific Components' afterViewInit
function and never try to e.g. modify the results of one component from within another.)
这篇关于一起使用Angular2 + JQuery怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!