0

i'm learning vue and i have small problem.

I've created code, which receive some informations from webserwer (via socket) and this code works fine.

But i would like to do very simple thing - display info as variable in HTML and i have problem with it.

My code is:

    export default {
  components: {
    BCard,
    BButton,
  },
  data() {
    return {
      connection: null,
      json: {
        cmd: 'start',
        width: 1024,
        height: 800,
        url: 'https://www.google.com',
        token: '',
        service_id: 1,
        thumbnail_width: 100,
        thumbnail_height: 100,
      },
    }
  },
  created() {
    console.log('Starting Connection to WebSocket')
    this.connection = new WebSocket('ws://127.0.0.1:8080/')
    // this.connection = new WebSocket('ws://echo.websocket.org')
    this.connection.onopen = function (event) {
      console.log(event)
      console.log('Success')
    }
    this.connection.onmessage = webSocketOnMSG
  },
  methods: {
    sendMessage(message) {
      console.log(this.connection)
      console.log(message)
      console.log(JSON.stringify(message))
      const dat = this.connection.send(JSON.stringify(message))
      console.log('TT', dat)
    },
    drawItem() {
      const img = document.createElement('img')
      const canvas = document.getElementById('canvasId')
      img.src = 'http://image...'
      img.onload = function (a) {
        const h = a.target.height
        const w = a.target.width
        const c = canvas.getContext('2d')
        canvas.width = w
        canvas.height = h
        c.drawImage(img, 0, 0)
        document.getElementById('draw-image-test').appendChild(canvas)
      }
    },
    webSocketOnMSG(msg) {
      console.log(msg)
    },
  },
}

and i would like to add code like this:

data: {
xposition: 'xpos',
yposition: 'ypos'
}

but when i'm adding it to created earlier data() i have error, so this doesn't work:

data() {
xposition: 'xpos',
yposition: 'ypos',
        return {...}
}

where should i add code to replace variables {{xposition}} and {{yposition}} in HMTL?

1
  • In Vue, you declare an object from which you have access in the template and in all methods and lifecycle hooks. So, return an object { a: 123, b: { x: 321 } } in data and you can access e.g. a in a method with this.a. You can also access it in the template with {{ a }} or {{ b.x }}. So, just overwrite the relevant property of that object in the method and it will automatically reflect to the template. Commented Apr 26, 2021 at 10:17

1 Answer 1

1

You must put your new variables inside your returned object in the data function, alongside your 'json' variable. You need to declare them first as empty values, and then add the proper values in your API call callback

data() {
  return {
    xposition: '',
    yposition: '',
    ...
  }
}

    webSocketOnMSG(msg) {
      // this will change your component's xposition property
      this.xposition = msg.propertyYouWantToAccess
    },

Sign up to request clarification or add additional context in comments.

2 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
@Regolith You are right, I didn't properly understand the author's question. I edited my response to give a proper answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.