2在angular2+webpack+typescript項目中怎么引入js插件埠通?
在github上搜ng2-admin https://github.com/akveo/ng2-admin 項目充活,可以查看如何引入jqeury蜕乡、ckeditor等插件
這里再介紹一種直接引入的方式:
在index.html中引入相應(yīng)的js文件
<!--文本編輯器-->
<link rel="stylesheet" href="assets/plugin/mathjax_custom.css">
<link rel="stylesheet" href="assets/plugin/ueditor_custom.css">
<script type="text/javascript" src="assets/plugin/ueditor_1.4.3.3/ueditor.config.js"></script>
<script type="text/javascript" src="assets/plugin/ueditor_1.4.3.3/ueditor.all.js"></script>
<script type="text/javascript" src="assets/plugin/ueditor_1.4.3.3/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" src="assets/plugin/ueditor_1.4.3.3/ueditor.parse.min.js"></script>
<script type="text/javascript" src="assets/plugin/ueditor_custom.js"></script>
<!--viewerjs-->
<link rel="stylesheet" href="assets/plugin/viewerjs/viewer.css">
<script type="text/javascript" src="assets/plugin/viewerjs/viewer.js"></script>
在組件中聲明
declare var Viewer: any;
通過Viewer的使用方法和js中一樣
initViewer() {
let _self = this;
let viewBox = document.getElementById('viewer');
let viewerContainer;
let options = {
navbar: 0,
title: 0,
fullscreen: false,
transition: false,
keyboard: false,
ready: function (e) {
viewerContainer = document.getElementsByClassName('viewer-container')[0];
viewerContainer.style.position = 'absolute';
viewerContainer.style.backgroundColor = '#f3f3f3';
viewBox.appendChild(viewerContainer);
},
show: function (e) {
viewBox.style.display = 'block';
},
shown: function (e) {
console.log(e.type);
},
hide: function (e) {
console.log(e.type);
},
hidden: function (e) {
console.log(e.type);
_self.isViewPicture = false;
},
view: function (e) {
console.log(e.type, e.detail.index);
},
viewed: function (e) {
console.log(e.type, e.detail.index);
// this.viewer.zoomTo(1).rotateTo(180);
_self.viewer.zoomTo(0.38, true);
_self.viewer.moveTo(60, 0);
}
};
_self.viewer = new Viewer(document.getElementById('images'), options);
}
類似的ueditor使用方法:
declare var UE: any;
ngOnInit() {
UE.delEditor(this.editorName);
UE.getEditor(this.editorName, {
autoHeight: false,
allowDivTransToP: false
});
this.ue = UE.getEditor(this.editorName);
if (this._content) {
let self = this;
this.ue.ready(() => {
self.ue.setContent(self._content, false);
});
}
}
關(guān)于js插件引入到ts項目中邻吞,還需要對他們進行整理氛悬,會有更好的克锣、更為系統(tǒng)的引入方式甘有,待以后再做整理、總結(jié)管怠。先分享以上內(nèi)容給項目于困擾的同學參考淆衷。
有興趣的同學還可以關(guān)注如何編寫ts聲明文件,在angular2+webpack+starter框架中的custom-typings.d.ts就是這類文件排惨,可以參考學習一下,另外這里有關(guān)于這一塊的ts教程可以學習https://www.tslang.cn/docs/handbook/declaration-files/introduction.html