Angular之引用第三方库的方法

Posted by Shen Chaoran on December 4, 2017

引用js

放在angular.cli.jsonscripts中,如

"scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/jquery-slimscroll/jquery.slimscroll.js",
    "../node_modules/tether/dist/js/tether.js",
    "../node_modules/chroma-js/chroma.js",
    "../node_modules/lodash/",
    "../node_modules/postal/lib/postal.min.js",

    "../node_modules/@webcomponents/custom-elements/custom-elements.min.js",
    "../node_modules/clarity-icons/clarity-icons.min.js",
    "../node_modules/jqwidgets-framework/jqwidgets/jqxcore.js",
    "../node_modules/openlayers/dist/ol.js",
    "../node_modules/ol3-ext/dist/ol3-ext.min.js"
]

引用css

放在angular.cli.json的styles里,如

"styles": [
    "styles.scss"
]

另外在styles.scss中添加如下:

@import "../node_modules/roboto-fontface/css/roboto/roboto-fontface.css";
@import "../node_modules/normalize.css/normalize.css";
@import '../node_modules/font-awesome/css/font-awesome.css';
@import "../node_modules/ionicons/css/ionicons.min.css";
@import "../node_modules/ng2-slim-loading-bar/style.css";
@import "../node_modules/perfect-scrollbar/dist/css/perfect-scrollbar.min.css";
@import "../node_modules/openlayers/dist/ol.css";
@import "app/common/shared/theme/theme.scss";

相对路径的问题

??

声明

只引用工具库是不行的,通不过typescript的编译器检查,还需要对引入的对象进行声明,有两种方式进行声明:

  • 声明文件typings.d.ts,如下
      declare var module: NodeModule;
      interface NodeModule {
          id: string;
      }
    
    
      declare var assert: any;
      declare var _: any;
      declare var $: any;
    
      interface JQuery {
      easyPieChart;
      }
    
      declare var GoogleMapsLoader: any;
      declare var L: any;
      declare var AmCharts: any;
      declare var Chart: any;
      declare var Chartist: any;
      declare const chroma: any;
      declare var jQuery: any;
      declare var _: any;
      declare var postal: any;
    
  • 在用到的文件中声明:

      // map.service.ts
      declare var ol: any;