在 Angular 的事件前添加“$"美元符号有什么作用? [英] What is the effect of adding '$' dollar sign infront of event in Angular?

查看:61
本文介绍了在 Angular 的事件前添加“$"美元符号有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Angular 的事件前添加 '$' 美元符号有什么作用?

onModelUpdated($event: any) {console.log($event);}

对比

onModelUpdated(event: any) {控制台日志(事件);}

解决方案

$ 前缀在变量名中没有任何效果.JavaScript 中的 $ 美元符号(以及 TypeScript 的扩展名)是用作变量名一部分的有效标识符,它没有语法意义.可用于变量/函数名的开头、中间或结尾.

特别是当谈到 $event 及其在 Angular 中的使用时,它更像是从 AngularJS 时代延续下来的约定.在 AngularJS 中,框架暴露的内部变量以$为前缀一>.

这就是为什么有时您会看到 event 有时会看到 $event.

建议摆脱它,但什么也没发生.>

所以这更多是一个风格和约定的问题,由个人/项目偏好来制定关于是使用 $event 还是 event<的约定/代码>.

What is the effect of adding '$' dollar sign infront of event in Angular?

onModelUpdated($event: any) {
    console.log($event);
  }

vs

onModelUpdated(event: any) {
    console.log(event);
  }

解决方案

$ prefix in variable names has no any kind of an effect. A $ dollar sign in JavaScript (and TypeScript by extension) is a valid identifier to use as part of a variable name, it carries no syntactical meaning. It can be used in the beginning, middle or end of a variable/function name.

Specifically when it comes to $event and its use in Angular, it is more of a convention carried over from the days of AngularJS. In AngularJS, internal variables exposed by the framework were prefixed with a $.

That is why sometimes you see event and other times $event.

There was a proposal to get rid of it, but nothing happened.

So it is more of a question of style and conventions, and it is up to the personal/project preferences to come up with a convention about whether to use $event or event.

这篇关于在 Angular 的事件前添加“$"美元符号有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆