angular - 角度中的类或函数实用程序
问题描述
您如何以及何时在 angular2 中创建实用程序/帮助程序类或函数?
我创建了一个文件夹helpers
,用于存放将要创建的所有通用函数和类。
但现在我有这段代码将用于 8 个组件。
/**
* this is initialize after the elements have been loaded. called inside ngAfterViewInit methods
* this sets up the joyrideService
*/
joyRideInit(): void {
const fileNameNotes = this.designNotes.map((matchImgUrl) =>
extractKeyFromFilename(matchImgUrl)
);
const seenNotes = {
viewed_notes: {
match: fileNameNotes,
...this.viewedNotes
}
};
this.joyrideService
.startTour({
steps: [
'matchFirst',
...this.designNotes,
'guidedTourBtn',
'saveNextBtn'
]
})
.subscribe(null, null, () => {
this.updateUserNotes(seenNotes);
});
}
/**
* sends a resquest to update the notes
* @param userUpdateNotes
*/
updateUserNotes(userUpdateNotes): void {
this.userService
.updateUser(userUpdateNotes, this.user.id)
.pipe(
first(),
map((result) => {
// * result no used.
console.log('res', result);
}),
catchError((error) => {
console.error('display-notes#nextNoteOrClose: error in ', error);
throw error;
})
)
.subscribe();
}
我应该把它作为一个类或函数吗?这将有 4 个参数来自将使用它的组件。
解决方案
创建共享/公共服务,所有像这样的方法你都可以把它放在那里进行默认注入。
推荐阅读
- java - 不可变对哈希码
- php - DatabasePHP:创建的用户A访问创建的用户B的数据库,反之亦然
- c++ - 通过 const&、&& 或仅通过值,哪个更好地获得返回值?
- permissions - launchctl / launchd - 守护程序/代理 - 权限错误 - OSX 11.0.1
- javascript - 由于“cloudinary.uploader 未定义”错误,从 cloudinary 中删除媒体文件失败
- java - java.lang.ArrayIndexOutOfBoundsException: Index -2 out of bounds for length 46410 在使用 Apache POI 打开 XLS 文件时是什么意思?
- vaadin-flow - 瓦丁 18 | 需要使用 Lit-Template 和 HTML 文本将消息从客户端传递到服务器
- github - VS Code github 一直显示 Uploading files forever
- r - ggplotly 是否提供了一种将选择中的数据(例如,散点图中来自 aes() 的 x 和 y 值)复制到剪贴板的方法?
- r - R:read.csv 在一列名称中引入了不可读的字符