我试图让我的Node.js对象投射事件.现在,如果我创建"静态"对象并实例化它们,这不是问题,但是当我的对象没有静态爷爷时,如何使用对象文字符号创建对象时如何执行此操作?
我习惯编写ExtJS语法,所以我更喜欢对象文字中的所有内容.
// var EventEmitter = require('events').EventEmitter; // How where when?
myObject = {
myFunction: function() {
console.log('foo');
},
urFunction: function() {
console.log('bar');
}
};
Run Code Online (Sandbox Code Playgroud)
这是一个对象.它没有构造函数,因为不需要更多实例.
现在我如何允许myObject发出事件?
我已经尝试过并尝试调整代码,但是如果不将我的对象重写为带有类似构造函数的表单,我就无法工作:
var EventEmitter = require('events').EventEmitter;
var util = require('util');
function myClass() {
EventEmitter.call(this);
myFunction: function() {
this.emit('foo');
},
urFunction: function() {
this.emit('bar');
}
};
myClass.prototype = Object.create(EventEmitter.prototype);
// or // util.inherits(myClass, EventEmitter);
var myObject = new myClass; // Shouldn't …Run Code Online (Sandbox Code Playgroud) 使用Node的EventEmitter时,您订阅了一个事件.您的回调仅在该特定事件被触发时执行:
eventBus.on('some-event', function(data){
// data is specific to 'some-event'
});
Run Code Online (Sandbox Code Playgroud)
在Flux中,您使用调度程序注册商店,然后在调度每个事件时调用您的商店.商店的工作是过滤它获得的每个事件,并确定事件对于商店是否重要:
eventBus.register(function(data){
switch(data.type){
case 'some-event':
// now data is specific to 'some-event'
break;
}
});
Run Code Online (Sandbox Code Playgroud)
在此视频中,演示者说:
"商店订阅行动.实际上,所有商店都接收所有行动,这就是保持其可扩展性的因素."
为什么以及如何将每个操作发送到每个商店[可能]比仅向特定商店发送操作更具可扩展性?
Angular2中有没有办法以某种方式阻止使用EventEmitter的事件的默认值?
我有以下场景:
import {Component, Output, EventEmitter} from 'angular2/core'
@Component({
selector: 'my-component',
template: `<button (click)="clickemitter.emit($event); onClick($event)" [style.color]="color"><ng-content></ng-content></button>`
})
export class MyComponent {
@Output clickemitter: EventEmitter<MouseEvent> = new EventEmitter();
private color: string = "black";
constructor() {
}
private onClick(event: MouseEvent): void {
if(!event.defaultPrevented) //gets called before the observers of clickemitter
this.color = "red";
else this.color = "blue";
}
}
@Component({
selector: 'my-app',
providers: [],
template: `
<my-component (clickemitter)="$event.preventDefault()">Hello Angular</my-component>
`,
directives: [MyComponent]
})
export class App {
constructor() {
}
}
Run Code Online (Sandbox Code Playgroud)
我做了一个Plunker这一点,太: …
我想要做这样的工作:
var Events=require('events'),
test=new Events.EventEmitter,
scope={
prop:true
};
test.on('event',function() {
console.log(this.prop===true);//would log true
});
test.emit.call(scope,'event');
Run Code Online (Sandbox Code Playgroud)
但是,不幸的是,听众甚至没有被召唤.有没有办法做这个W/EventEmitter?我可以Function.bind听听,但是,我真的希望EventEmitter有一些特殊的(或明显的)方法来做到这一点......
谢谢您的帮助!
我在我的智慧,神经,一切都结束了,在过去的4个小时里,我正在尝试调试和阅读互联网上的问题.
所以,我正在努力模拟移动应用程序,我的逻辑是这样的:
在主要的Home视图中,我正在评估上述语句,首先我向他展示了如果他有本地存储数据,然后我在线检查并在必要时重定向到登录.
每当我有两个$ state或$ locations它就会循环.就像浏览器几乎冻结一样,幸运的是Chrome.我得到了Cannot call method 'insertBefore' of null.从我读到的,一个已知的angular.ui路由器问题,如果使用不好或其他一些神秘的问题.
一些代码:
这些事件发射器和接收器,我在StateManagement Services返回时调用
//Notification Helpers, also exposed, to keep them in the same scope tree.
function emitNotification (event, data) {
console.log('Emitting event: ' + event);
$rootScope.$emit(event, data);
}
function emitListen (event, fn) {
console.log('Listening for: ' + event);
$rootScope.$on(event, function (e, data) {
fn(data);
});
}
Run Code Online (Sandbox Code Playgroud)
...逻辑
//Update the userData in localStorage and in .value
$auth.storage.set('userData', data);
userData = $auth.storage.get('userData'); //TODO: Redundant, …Run Code Online (Sandbox Code Playgroud) javascript eventemitter angularjs angularjs-scope angular-ui-router
我们可以有一个发射器的多个侦听器,每个都在不同数量的参数上工作吗?
例如,让事件发射器像这样:
evetE.emit('pre', global, file, self);
corresponding event listeners:
//Listener 1
m.eventE.on('pre', function() {
//TODO
})
//Listener 2
eventE.on('pre', function(context, file, m){
console.log(context.ans);
});
//Listener 3
eventE.on('pre', function(context){
console.log(context.ans);
});
//Listener 4
this.eventE.on('pre',function (context) {})
Run Code Online (Sandbox Code Playgroud)
如果以上是真的,那么哪个参数转到哪个监听器?
我编写了一个使用EventEmitter类的基本角度应用程序,但是我无法获取侦听组件来捕获事件.
这是我的代码(在Angular2/TypeScript 1.5上使用alpha.27编译为ES5)为详细示例道歉.
关于我错误做什么的任何建议将不胜感激.
import {Component, View, EventEmitter} from 'angular2/angular2';
@Component({
selector: 'login',
events : ['loggedIn']
})
@View({
template: '<button type="button" (click)="submitForm()">Click Me</button>'
})
export class Login {
loggedIn = new EventEmitter();
constructor() {
}
submitForm() {
console.log("event fired");
this.loggedIn.next({});
}
}
@Component({
selector: 'app'
})
@View({
template: "<div>This is the application</div>"
})
export class App {
constructor() {
}
}
@Component({
selector: 'root'
})
@View({
template: '<app [hidden]=!showApp></app><login (loggedIn)="loggedIn()" [hidden]=showApp></login>',
directives: [ App, Login ]
})
export class Root …Run Code Online (Sandbox Code Playgroud) 我正在尝试从子组件向父组件发出事件.
家长:
parent.ts
onChangeUpload(event){
console.log('event');
console.log(event);
}Run Code Online (Sandbox Code Playgroud)
<app-upload (uploadEmit)="onChangeUpload($event)"></app-upload>Run Code Online (Sandbox Code Playgroud)
儿童:
@Output() uploadEmit: EventEmitter = new EventEmitter();
this.uploadEmit.emit('upload successful');Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
core.js:1448 ERROR Error: Uncaught (in promise): TypeError: instance[output.propName].subscribe is not a function
Run Code Online (Sandbox Code Playgroud)
@angular/cli: 1.7.3
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.2
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.6.2
webpack: 3.11.0Run Code Online (Sandbox Code Playgroud)
是否可以等待 Angular EventEmitter 发出,就像可以等待 ajax 调用(Angular 5 同步 HTTP 调用)一样?
我想做的是:
有一个子组件
@Component({
template: '<button (click)="emit1()">emit</button>'
})
export class ChildComponent {
@Output() dataChanged: EventEmitter<number> = new EventEmitter<number>(true); // synchronous
emit1(){
this.dataChanged.emit(1)
}
}
Run Code Online (Sandbox Code Playgroud)
以及动态创建子组件的父组件
@Component()
export class ParentComponent {
childComponentRef: ComponentRef<ChildComponent>
constructor(private resolver: ComponentFactoryResolver, private vcRef: ViewContainerRef) {}
openChild() {
const factory = this.resolver.resolveComponentFactory(ChildComponent);
this.childComponentRef = this.vcRef.createComponent(factory);
await this.childComponentRef.instance.dataChanged.toPromise()
alert('emited!')
}
}
Run Code Online (Sandbox Code Playgroud)
我想 (a) 等待,直到从子组件发出值。
我正在尝试设计一个场景,在特定事件被触发时,我想让一些听众执行一些任务.试图管理代码的SRP,我希望将侦听器放在不同的源文件中.我想知道使用事件Emitter是否可行.事件发射器只能在单个源文件上工作吗?
var events = require('events');
var em = new events.EventEmitter();
exports.saveScheme = function (req, res) {
var dal = dalFactory.createDAL(constants.SCHEME);
return new Promise.resolve(dal.PromiseSave(req.body))
.then(function(data){
var schemeId = data._id;
em.addListener('FirstEvent', function (data) {
console.log('First subscriber: ' + data);
});
em.emit('FirstEvent', 'Test event emitter');
}).catch(function(error){
console.log(error);
});
};
Run Code Online (Sandbox Code Playgroud)
我的其他源文件是
var emitter = require('events').EventEmitter;
var em = new emitter();
//Subscribe FirstEvent
em.on('FirstEvent', function (data) {
console.log('First subscriber: ' + data);
});
Run Code Online (Sandbox Code Playgroud) eventemitter ×10
node.js ×5
angular ×4
javascript ×4
typescript ×2
angularjs ×1
async-await ×1
emit ×1
events ×1
flux ×1
mocha.js ×1
protractor ×1
reactjs ×1
scope ×1