angular - 在Angular中的不同组件上有组件保存按钮触发动作吗?
问题描述
我有一个使用 NGPrime 数据表的角度组件....当用户选择一行时,他们会得到一个弹出组件,让他们编辑数据....在他们编辑数据后,他们可以单击“保存”按钮。如何将保存按钮单击操作发送到带有数据表的组件,以便触发数据刷新?
解决方案
你有几种方法。
如果它们是不相关的组件,那么
通用服务
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable()
export class ShareService {
constructor() { }
private paramSource = new BehaviorSubject("");
sharedData = this.paramSource.asObservable();
setParam(param:string) { this.paramSource.next(param)}
}
在 appmodule 中放入提供者
providers:[ShareService ]
在组件中放入构造函数
constructor(private shareService: ShareService ){}
从第一个组件发送喜欢
this.shareService.setParam('Sending param');
从第二个组件收到类似
this.shareService.sharedData.subscribe(data=> { console.log(data); })
路由方法
您可以将参数放入路由器并发送。您可以将路由器设置为参数,就像您的 routing.ts
{ path: 'component/:param', component: RelatedComponent },
然后在组件中你想让它像ActiveRoute
构造函数中的导入一样调用
constructor(private route: ActivatedRoute){}
并得到喜欢
this.route.snapshot.paramMap.get('param')
或者如果您不想在路线中显示参数,您可以像下面的代码一样发送状态
this.router.navigate(['your_component_url'], {state: { param: 'data you send' } });
在构造函数中读取状态this.router.getCurrentNavigation().extras.state.param;
储存方法
您可以设置为 localStorage 或 sessionStorage 然后从另一个组件的存储中获取。
localStorage.setItem("param","parameter")
localStorage.getItem("param")
如果它们是父子组件,那么你也可以在上面做,而且也很容易使用
@输入法
<your_child_component param="{{ parameter}}"></your_child_component>
在子组件中getlike
@Input param;
推荐阅读
- java - 与 Homebrew 一起安装时,OpenJDK 16 在 MacOS Apple Silicon 上显示为 17
- linux - 如何将 Azure Blob 复制到本地 Linux 服务器?
- asp.net - “docker pull microsoft/aspnet”错误“没有匹配的清单......”
- flutter - 接受所有可用付款方式的订阅付款
- javascript - 使用可能的未定义变量评估多个条件语句
- stl - 为什么 vector.push_back(System::Byte) 在 VC++ 14.29 (C++/CLI) 中不再编译
- javascript - AWS Lambda 函数响应时间超过 1200 毫秒
- kotlin - 在 IntelliJ 中构建 Kotlin 程序后,如何直接从终端执行它?
- node.js - Heroku 服务器 api 调用(本地主机)不起作用
- javascript - React 应用程序的编译错误:模块构建失败(来自 ./node_modules/babel-loader/lib/index.js)