简体   繁体   English

如何在CatberryJS应用程序中使用jQuery?

[英]How can I use jQuery in CatberryJS application?

I want to use jQuery in my web application on CatberryJS framework . 我想在CatberryJS框架的 Web应用程序中使用jQuery。 How can I do it? 我该怎么做?

Using jQuery library registered globally 使用全球注册的jQuery库

You can use jQuery as usual, just include this script tag into your "head" component: 您可以照常使用jQuery,只需将此脚本标签包含在“ head”组件中:

<script src="//code.jquery.com/jquery-2.1.3.min.js"></script>

And then feel free to use jQuery functions in any component's code except its constructor and the " render " method. 然后,随时可以在除组件的构造函数和“ render ”方法之外的任何组件代码中使用jQuery函数。

Using jQuery as a part of built bundle.js 使用jQuery作为内置bundle.js的一部分

In this case you can install jQuery using npm like this: 在这种情况下,您可以使用npm安装jQuery,如下所示:

npm install jquery --save

Then register jQuery as a Catberry Service in ./browser.js 然后在./browser.js jQuery注册为Catberry服务

var catberry = require('catberry'),
    cat = catberry.create({/* config */}),
    jQuery = require('jquery');

cat.locator.registerInstance('jquery', jQuery);

After the jQuery was registered, you can use it in your components resolving it from a Catberry Service Locator : 注册jQuery后,您可以在组件中使用它,而从Catberry Service Locator解析它:

var $ = this.$context.locator.resolve('jquery');
$('.class').html('<div>Hello, World!</div>');

Additionally 另外

If you would like to use jQuery functions in component's constructor or component's " render " method you should check the environment flag like this: 如果要在组件的构造函数或组件的“ render ”方法中使用jQuery函数,则应检查环境标志,如下所示:

if (this.$context.isBrowser) {
  // crazy jQuery stuff
}

Also, keep in mind that to avoid memory leaks your component should unsubscribe all jQuery events and free all resources used by jQuery functions in " unbind " method. 另外,请记住,为避免内存泄漏,您的组件应取消订阅所有jQuery事件,并以“ unbind ”方法释放jQuery函数使用的所有资源

You can use any browser-side library inside component like it's described above for jQuery. 您可以在组件内部使用任何浏览器端库,如上面针对jQuery所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM