angular - How to pass function as data to another module component in angular?
问题描述
I have a main module with some content and Nav
I have sub modules (lazy loaded) within the main module which should be giving me the nav Items.
I have made a service to pass data:
const navItems = [
{ text: 'Add', icon: faUser,, function: 'AddDLevels()'},
{ text: 'Create D1', icon: faUser, function: 'toggleCreateD1()'},
{ text: 'Create D2', icon: faUser, function: 'toggleCreateD2()'},
{ text: 'Push DS78', icon: faUser, function: 'pushDS()'}
];
this.consoleNav.setNavItems(navItems);
How do I make the functions work ? I want to click the nav button and it should reflect in child modules component.
解决方案
Rather than passing the functions as strings, you should pass a reference to a function, Eg:
const navItems = [
{ text: 'Add', icon: faUser, function: () => AddDLevels()},
];
Then you just call that function in your on click handler or whatnot
item.function()
推荐阅读
- java - Java、Firebase、请求通知权限
- android - Firebase 数据库免受 apk 反编译和使用的影响
- reactjs - 只显示点击卡片的内容
- reactjs - ReactJS - 具有多个输入的自动保存 - 最佳实践
- javascript - 使用“new Function”语法导入库
- javascript - Svg 使用 greensock 仅使用 css 转换它
- php - codeigniter 中的动态相关下拉列表
- java - 从 POM.xml 运行 Testng.xml 没有按顺序运行测试
- python - Glyphicons 无法在 bootstrap 4.5.0 中应用该网站
- ffmpeg - FFMPEG:如何只保留图像的上半部分?