简体   繁体   English

如何在控制器/组件中使用导入的第三方js(不是addon / npm包)?

[英]How to use imported third-party js (not addon/npm package) in controller/component?

I have put my js files eva.min.js/feather.min.js and so on in vendor dir, then I imported them in ember-cli-build.js app.import('vendor/eva.min.js') . 我在供应商目录中放了我的js文件eva.min.js/feather.min.js等,然后我将它们导入到ember-cli-build.js app.import('vendor/eva.min.js') But how to use it? 但是怎么用呢?

I tried something like import eva from 'eva'/'eva.min'/'eva.min.js' or import Eva from 'eva'; 我尝试import eva from 'eva'/'eva.min'/'eva.min.js' import Eva from 'eva';import Eva from 'eva'; and so on, but it doesn't work. 等等,但它不起作用。

  app.import('vendor/eva.min.js');
  app.import('vendor/bootstrap.min.js');
  app.import('vendor/feather.min.js');
  app.import('vendor/popper.min.js');
  app.import('vendor/jquery-slim.min.js');
  app.import('vendor/swipe.js');
import Swipe from 'swipe';

Console usually gives me the could not find the module error. 控制台通常会让我找不到模块错误。 And I don't have a deep background in programming, so I would highly appreciate if you explained the problem as simple as possible. 我没有深入的编程背景,所以如果你能尽可能简单地解释问题,我将非常感激。

UPD: I found all js code as npm package (it happens that the js files weren't third-party) UPD:我发现所有js代码都是npm包(碰巧js文件不是第三方)

https://www.npmjs.com/package/feather
https://www.npmjs.com/package/popper.js
https://www.npmjs.com/package/jquery-slim
https://www.npmjs.com/package/swipe
https://www.npmjs.com/package/bootstrap
https://www.npmjs.com/package/eva-icons

But all your responses were helpful. 但你所有的回答都很有帮助。 Anyway in the near future I expect to use third-party libraries. 无论如何,我希望在不久的将来使用第三方库。

A quick way is to use scriptjs and it allows you to load any javascript into your component in the following way: (I am using Yammer as an example) 一种快速的方法是使用scriptjs ,它允许您以下列方式将任何javascript加载到组件中:(我使用Yammer作为示例)

    import $scriptjs from 'scriptjs';
    componentDidUpdate() {
       //script loader
       setTimeout(function(){
         $scriptjs('https://c64.assets-yammer.com/assets/platform_embed.js', 
         () => {
            window.yam.connect.embedFeed(YammerHelper.loadComments());
         });
       }, 1000);
    }

You should get the idea how to consume it. 你应该知道如何消费它。 Check their docs with lots of examples. 通过大量示例检查他们的文档。

This is not the best solution. 这不是最好的解决方案。 But one way of using the third party js is, 但使用第三方js的一种方法是,

1) say you have a function in your js file vendor/third-party.js 1)假设你的js文件vendor/third-party.js有一个函数

someFunction = function (element) {
  ...
  console.log("works")
};

2) Then import it in your ember-cli-build.js 2)然后在你的ember-cli-build.js导入它

...
app.import('vendor/third-party.js');
...

3) After importing restart your server. 3)导入后restart服务器。

Use the function directly in your controller/component as 直接在控制器/组件中使用该功能

window["someFunction"]

Unless the JavaScript library being used explicitly supports the import X from 'y' syntax then when you import in the build using the app.import syntax you just use it in your app just as the plugin documentation describes. 除非明确使用的JavaScript库支持import X from 'y'语法import X from 'y'否则当您使用app.import语法导入构建时,您只需在应用程序中使用它,就像插件文档所描述的那样。

So for Swipe you would do the following. 因此,对于Swipe您将执行以下操作。 Based on this documentation: https://github.com/thebird/Swipe 基于此文档: https//github.com/thebird/Swipe

// ember-cli-build.js
app.import('myswipe.js`);
// component.js
/* global Swipe */ // This silences the linter from throwing errors...
classNames: ['swipe'],
didInsertElement() {
   this._swipe = Swipe(this.element, {
       option1: option1
   });
}
// component.hbs
<div class='swipe-wrap'>
    {{yield}}
</div>

This codes creates a component to control your swipe plugin. 此代码创建一个组件来控制滑动插件。 This code would create a swipe object and isolate it to the component. 此代码将创建一个滑动对象并将其与组件隔离。

Again when you use the app.import you are just loading the library on boot. 再次使用app.import您只是在启动时加载库。 The library does whatever it says it will do in the docs. 该库可以完成它在文档中所做的任何操作。 Sometimes they register a global object, sometimes they dont. 有时他们会注册一个全局对象,有时他们不会。

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

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