javascript – 将第三方js文件导入angular typescript项目
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 将第三方js文件导入angular typescript项目,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8495字,纯文字阅读大概需要13分钟。
内容图文
在我的角度体验中,我被迫使用四种不同的方式来包括第三方库poliglot.js(用于multilang).
所以为了能够在我的Lang类中使用新的Polyglot(…):
export class Lang
{
...
constructor() {
this.polyglot = new Polyglot({ locale: 'en' });
...
}
...
}
我用这四种方法
A.在我相当古老的(2016)angular2(基于framerwork angular2-webpack-starter)项目中(目前这个解决方案由于缺乏新角度项目中的需求指令而无效):
var Polyglot = require('../../../node_modules/node-polyglot/build/polyglot.min.js');
B.在我的下一个项目angular4(基于angular2-webpack-starter):
import Polyglot from '../../../node_modules/node-polyglot/build/polyglot.min.js';
C.在我最近在Laravel项目中嵌入的angular5项目(基于angular-cli)
import * as Polyglot from '../../../node_modules/node-polyglot/build/polyglot.min.js';
D.我还发现了第4个解决方案,它可以用于jQuery的一些旧的角度项目(基于angular2-webpack-starter)(并且互联网上的人们提到了很多这个解决方案)但我使用Polyglot示例写下来:
import '../../../node_modules/node-polyglot/build/polyglot.min.js';
declare var Polyglot: any;
// declare var $:any // this is for jquery (as example)
问题是:这四种解决方案之间的区别是什么?是什么原因导致在某个项目中一个解决方案有效但其他解决方案无效
解决方法:
所以让我们分解一下:
答:在使用它之前,您仍然需要声明要求的任何角度版本.
declare const require: any;
const Polyglot = require('../../../node_modules/node-polyglot/build/polyglot.min.js');
B:A点使用CommonJS模块系统加载依赖关系,其他点使用ES6动态导入系统(可以像commonjs模块系统一样使用,默认情况下为webpack).如果库公开模块,您可以直接导入Polyglot,例如
export class Polyglot {}
C:如果Polyglot有多个你不想使用的成员,你可以通过写作导入Polyglot的所有成员
import * as Polyglot from '../../../node_modules/node-polyglot/build/polyglot.min.js';
D:Polyglot被导入但不绑定到任何变量.但是Polyglot公开了一个你无权访问的全局对象,直到你声明它将在其下可用的变量.
有关更好的解释,请参阅mdn reference
根据您使用的构建系统,没有答案哪个应始终有效,但我的解决方案A应该适用于每个webpack构建以及B和C.友好提醒A和D不是最佳解决方案,只应在没有其他方法可以导入/使用该模块.
编辑:
ES6 standard只描述了模块是什么,它包含什么,模块应该如何是exported和imported等等.
因此,ES6无法处理这些“旧”模块,因为它不是一个库或类似的东西. CommonJS也只是一个标准,由Node.js实现,你知道哪个模块使用require(‘module’).
因此,Webpack可以帮助您处理这两个模块系统,因为他们实现了这两个系统.
如果您创建一个空项目并通过webpack –env development使用webpack构建,您可以看到webpack如何处理不同的模块. Webpack编译您自己处理ESModules或CommonJS模块的代码和广告.根据他们发现的模块,他们将调用不同的方法.我添加了编译代码的示例.
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "./main.js");
/******/ })
/************************************************************************/
/******/ ({
/***/ "./esmodule.js":
/*!*********************!*\
!*** ./esmodule.js ***!
\*********************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.default = void 0;\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar MyClass = function MyClass() {\n _classCallCheck(this, MyClass);\n\n console.log('test');\n};\n\nexports.default = MyClass;\n\n//# sourceURL=webpack:///./esmodule.js?");
/***/ }),
/***/ "./main.js":
/*!*****************!*\
!*** ./main.js ***!
\*****************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
eval("\n\nvar test = _interopRequireWildcard(__webpack_require__(/*! ./esmodule.js */ \"./esmodule.js\"));\n\nfunction _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }\n\n__webpack_require__(/*! ./module */ \"./module.js\");\n\n//# sourceURL=webpack:///./main.js?");
/***/ }),
/***/ "./module.js":
/*!*******************!*\
!*** ./module.js ***!
\*******************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
eval("\n\nmodule.exports = {\n myFunction: function myFunction() {\n console.log('Test');\n }\n};\n\n//# sourceURL=webpack:///./module.js?");
/***/ })
/******/ });
//// main.js
require('./module')
import * as test from './esmodule.js';
//// esmodule.js
export default class MyClass{
constructor(){
console.log('test')
}
}
//// module.js
module.exports = {
myFunction: function () {
console.log('Test')
}
}
您可以看到Webpack创建了一个自执行函数,该函数使用{id(pathToFile):function(module,exports,__ webpack_require__)获取所有创建的模块.在2种不同的模块类型(ESModule,Module – > CommonJS)中,您可以看到Webpack以不同方式处理类型.如果你想要更深入的看,我可以再次编辑我的帖子.
内容总结
以上是互联网集市为您收集整理的javascript – 将第三方js文件导入angular typescript项目全部内容,希望文章能够帮你解决javascript – 将第三方js文件导入angular typescript项目所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。