在angular2 cli中,我已将base.js导入组件中。我想要base.js到child.js

时间:2017-07-12 16:55:04

标签: javascript angular

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

1 个答案:

答案 0 :(得分:0)

  1. 使用此代码创建组件并加载base.js文件和child.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. 创建base.js文件

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

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