[Vuejs]-How to attach a event to a variable in vuejs

0đź‘Ť

âś…

session and ua should not set in data() – they should not be tracked by Vue. They are not plain data objects:

The data object for the Vue instance. Vue will recursively convert its
properties into getter/setters to make it “reactive”. The object must
be plain:
native objects such as browser API objects and prototype
properties are ignored. A rule of thumb is that data should just be
data – it is not recommended to observe objects with their own
stateful behavior.

That being said, if you must insist, you could set up a handler right after the session is created as follows:

methods: {
    trackAddedEventHandler() {
        console.log('trackAdded Event!');
    },
    callme() {
        this.session = this.ua.invite('sip:bob@example.com');
        session.on('trackAdded', this.trackAddedEventHandler);
    },
},

But really do test those behaviors, as you may rely on something that Vue may not support when the object is at data().

Leave a comment