我正在尝试在角度应用程序中包含一些存储在文件中的
js
方法。不幸的是,我无法使浏览器看到js
文件。Sources
) 部分。
Js文件
function toggleModal(id) {
var modal = document.getElementById(id);
$("#" + id).modal('toggle');
}
function setPag(tableId) {
$(tableId).DataTable();
}
我的文件位于:
Root/
assets/
js/
scr.js
组件中的使用
declare function setPag(id:string):any;
declare function toggleModal(id:string):any;
@Component({
selector: 'index',
templateUrl: './index.component.html',
styleUrls: ['./index.component.css']
})
export class IndexComponent implements OnInit {
public toggle(id:string){
toggleModal(id);
}
public setPage(id:string)
{
setPag(id);
}
}
Angular.json
-脚本部分
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/AnubisUI",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/assets"
],
"scripts": [
"./src/assets/js/scr.js",
"node_modules/chart.js/dist/Chart.js",
"node_modules/hammerjs/hammer.min.js"
]
}
试试这个,看看是否适合你
import * as helper from 'assets/js/scr.js'
@Component({
selector: 'index',
templateUrl: './index.component.html',
styleUrls: ['./index.component.css']
})
export class IndexComponent implements OnInit {
public toggle(id:string){
helper.toggleModal(id);
}
public setPage(id:string)
{
helper.setPag(id);
}
}
旁注:每个静态文件都应添加到资产文件夹中,以便您可以导入到您的角度代码
是的,我这样做了,但没有结果。我两天寻找解决方案。 我怎样才能做到这一点?有结果吗?
提前致谢