1

こんにちは、「script.js」という名前のスクリプトがあります。問題は、そこに多くの関数があり、角度の最初でこの関数を実行する必要があることです。

var appMaster = {

    preLoader: function(){

    }, 
    smoothScroll: function() {

    }
}

しかし、この変数を appMaster と呼ぶ必要があります

import '../../assets/js/script.js';
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class HomeComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    appMaster.preLoader();
    appMaster.smoothScroll();
  }
}

エラーは appMaster no undeclared variable です。その他のスクリプト内で関数を実行するにはどうすればよいですか?

4

4 に答える 4

0
You can call varibale from anthoer script in Angular application.

Step 1. Create demo.js file in assests/javascript folder.

export function test1(){
    console.log('Calling test 1 function');
}

Step 2. Create demo.d.ts file in assests/javascript folder.

export declare function test1();

Step 3. Use it in your component
//User defined file path
import { test1 } from '../assets/javascript/demo'; 
 @Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor() {
    console.log(test1());
  }
}


Note: js and .d.ts file name shoule be same
于 2017-11-17T10:44:11.010 に答える