如何在Ionic 3中使用外部Javascript文件

问题描述 投票:1回答:1

我试图从这个链接http://openexchangerates.github.io/money.js/转换不同格式的货币

我在我的assets / js /文件中复制了http://openexchangerates.github.io/money.js/money.js文件,名称为“converter.js”

我还在index.html文件中声明了这个文件:

<script src="assets/js/converter.js"></script>

现在我想将fx.convert(1, {from: "USD", to: "INR"});函数调用到我的home.ts文件中

我喜欢哪个

declare var fx;

alert(fx.convert(1, {from: "USD", to: "INR"}));

返回错误:

ERROR Error: fx error
at viewWrappedDebugError (VM263 vendor.js:10180)
at callWithDebugContext (VM263 vendor.js:15482)
at Object.debugCreateRootView [as createRootView] (VM263 vendor.js:14755)
at ComponentFactory_.create (VM263 vendor.js:11652)
at ComponentFactoryBoundToModule.create (VM263 vendor.js:4404)
at ViewContainerRef_.createComponent (VM263 vendor.js:11849)
at IonicApp.ngOnInit (VM263 vendor.js:54207)
at checkAndUpdateDirectiveInline (VM263 vendor.js:12785)
at checkAndUpdateNodeInline (VM263 vendor.js:14309)
at checkAndUpdateNode (VM263 vendor.js:14252)

如何完美地实现此功能。

先感谢您。

javascript ionic-framework
1个回答
0
投票

在build / polyfills.js和build / main.js(它们在body标签中)之前,将导入js放在src / index.html标头标记中;

我创建了一个带有var测试的文件src / assets / test.js,在src / index.html中导入,然后在src / app / app.component.ts中声明了declare var test;。

test.js

var test = "Hello";

的index.html

...

 <link rel="icon" type="image/x-icon" href="assets/icon/favicon.ico">
  <link rel="manifest" href="manifest.json">
  <meta name="theme-color" content="#4e8ef7">

  <!-- cordova.js required for cordova apps -->
  <script src="cordova.js"></script>
  <script src="assets/js/test.js"></script>

... app.componet.ts

declare var test;

@Component({
   templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;
  constructor(private statusbar : StatusBar,  splashScreen: SplashScreen) {
   alert(test);
...
© www.soinside.com 2019 - 2024. All rights reserved.