In Ubuntu 18.04.02 I'm trying to figure out how to make Peer.js working with Vue.js .
This is the vue page:
<template>
<div>
<p>My Peer Id : {{ myPeerId }}</p>
<hr>
<p>Remote Peer Id
<input v-model="remotePeerId">
</p>
<hr>
<p button @click="connect()">Connect to my friend!</p>
</div>
</template>
<script>
import Peer from 'peerjs';
export default {
data: () => ({
connection: null,
connecting: true,
message: '',
messages: [],
myPeer: null,
myPeerId: '',
remotePeerId: ''
}),
created() {
this.myPeerId = (Math.random() * 6 + Math.random()).toString(36).replace('.', '');
this.myPeer = new Peer({ id: 'this.myPeerId', key: 'lwjd5qra8257b9' })
this.myPeer.on('open', (id) => {
console.log('Connected at PeerJS server with success')
console.log('My peer ID is: ' + id);
this.connecting = false
})
},
computed: {
},
methods: {
connect() {
if (this.remotePeerId.trim().length) {
this.connection = this.myPeer.connect(this.remotePeerId)
// Receive messages:
this.connection.on('data', (data) => {
this.connection.on('data', data => this.messages.push(data))
})
}
},
sendMessage() {
const newMessage = {
id: this.myPeerId,
message: this.message
}
if (this.connection) {
this.connection.send('Hello')
}
this.messages.push(newMessage)
this.message = ''
}
},
filters: {
}
}
</script>


Why when clicking "Connect to my friend!" it says "TypeError: cannot read property 'on' of undefined" ?
How to solve the problem?
Marco