在angular2 cli中,我使用了一个js类并将其导入到组件中。我想将基类从基类导入/要求基类.js到子类.js。在 Child.js 中,我想扩展/继承基类
使用此
代码创建组件和加载基础.js文件和子.js文件。
import { Component, OnInit, ElementRef } from '@angular/core'; @Component({ selector: 'app-jquerydata', templateUrl: './jquerydata.component.html', styleUrls: ['./jquerydata.component.css'] }) export class JquerydataComponent implements OnInit { public loadScript(url) { console.log('preparing to load...') let node = document.createElement('script'); node.src = url; node.type = 'text/javascript'; document.getElementsByTagName('head')[0].appendChild(node); } constructor(private elmref:ElementRef) { } ngOnInit():any { this.loadScript('assets/base.js'); this.loadScript('assets/child.js'); } }
创建基.js文件
function parent(){ alert("parent"); }
创建子文件.js文件 我们可以在 perent.js 文件中调用 parent((,在 child(( 中调用 child(( .js 文件
$(document).ready(function(){ child(); }) function child(){ parent(); alert("child"); }