在 angular2 cli 中,我已经将 base.js 导入到组件中.我想要求基地.js成孩子.js



在angular2 cli中,我使用了一个js类并将其导入到组件中。我想将基类从基类导入/要求基类.js到子类.js。在 Child.js 中,我想扩展/继承基类

使用此
  1. 代码创建组件和加载基础.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');
    
    }
    }
    
  2. 创建基.js文件

    function parent(){
    alert("parent");
    }
    
  3. 创建子文件.js文件 我们可以在 perent.js 文件中调用 parent((,在 child(( 中调用 child(( .js 文件

    $(document).ready(function(){
    child();
    })
    function child(){
    parent();
    alert("child");
    }
    

最新更新