import

npm install

npm install pubsub-js

Every module that needs to be observed or observed is imported into the js file

An example is as follows:

import '../../../../node_modules/pubsub-js/src/pubsub.js';

Create an Observer - an example here is to tell the observer the change and value of the target

var target='when target change';
//事件的名字
var targetChange='targetChange';
//告诉所有观察者 事件targetChange被触发 传递一个data为target 其他观察者可以拿来用
PubSub.publish(targetChange,target);

Create an observer - an example here is to listen for changes in the target value

// 举例 target改变事件
var targetChange='targetChange';
var target;
PubSub.subscribe(targetChange, function (msg, data) {
    target=data;//将从被观察者那里得到的数据给target 就可以开始使用已经被改变的target的值
});

Individuals draw conclusions not sure of right or wrong

1. import {target} from 'xx/xx'; The target is read-only and cannot be changed. But if it is an object var target{xxx=1}, the properties of the object can be changed, target.xxx=2; but the original referenced target and object will not change.
2. Import is asynchronous. The code written in front of import will not be executed first.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324891973&siteId=291194637