使用自定义Javascript的Angular应用程序中的本机调用失败
我从一个角度组件调用一些Javascript函数。Javascript函数更像是一个测试函数 位于名为testscript.js的文件中:使用自定义Javascript的Angular应用程序中的本机调用失败,javascript,jquery,angular,Javascript,Jquery,Angular,我从一个角度组件调用一些Javascript函数。Javascript函数更像是一个测试函数 位于名为testscript.js的文件中: function runFunction(aParam) { var res = false; alert('Hello, Custom Javascript!'); return (res); } 请注意,参数aParam是一个Javascript对象,最终将使用 JQuery函数 我已将该文件放在src/assets文件夹中,并将以
function runFunction(aParam) {
var res = false;
alert('Hello, Custom Javascript!');
return (res);
}
请注意,参数aParam是一个Javascript对象,最终将使用
JQuery函数
我已将该文件放在src/assets文件夹中,并将以下行添加到
angular.json文件:
"scripts": [
"src/assets/jquery-3.3.1.js",
"src/assets/testscript.js"
],
我有一个名为jsComponent的组件,用于调用函数:
import { Component, OnInit } from '@angular/core';
declare const runFunction: any;
@Component({
selector: 'app-js',
templateUrl: './js.component.html',
styleUrls: ['./js.component.css']
})
export class JsComponent implements OnInit {
constructor() { }
ngOnInit() {
}
callJS(anObj: InfoObj): boolean {
const res = runFunction(anObj);
return (res);
}
}
function runFunction(aParam) {
var res = false;
var json = JSON.stringify(aParam);
alert('Hello, Custom Javascript! '+json);
return (res);
}
InfoObj是一个将转换为JSON并最终进行处理的对象:
export class InfoObj {
constructor(
public aSize: number,
public someString: string,
public aMesg: string
}
}
运行应用程序时,调用callJS()时会调用Javascript函数runFunction()。
警报会正确显示带有“Hello,Custom Javascript!”的框,不会出现问题
不幸的是,当我进行JSON函数调用时,事情停止正常工作。当我更改runFunction()时
调用本机JSON转换函数的代码:
import { Component, OnInit } from '@angular/core';
declare const runFunction: any;
@Component({
selector: 'app-js',
templateUrl: './js.component.html',
styleUrls: ['./js.component.css']
})
export class JsComponent implements OnInit {
constructor() { }
ngOnInit() {
}
callJS(anObj: InfoObj): boolean {
const res = runFunction(anObj);
return (res);
}
}
function runFunction(aParam) {
var res = false;
var json = JSON.stringify(aParam);
alert('Hello, Custom Javascript! '+json);
return (res);
}
runFunction()函数失败。它甚至不显示警告框
我显然错过了什么。由于某种原因,JSON函数似乎由于未能成功运行而丢失
操作。这就好像没有找到JSON函数一样,悄悄地“挂起”runFunction()调用
有人知道为什么调用这样的函数时该函数会失败吗?我有什么事要做吗
是否在我的testscript.js文件中包含此调用?或者我需要在我的组件中进行声明。我在这里遗漏了什么?开发工具控制台中没有任何内容(如错误消息)?另外,代码中没有jQuery,
JSON
是一个本机api ObjectInterest。我没有意识到JSON是本机的。我将不得不修改我的问题。至于错误:没有,控制台上没有显示任何内容。没有异常,没有错误消息。JSON函数似乎没有被调用,runFunction()挂起。