NathanaelA / nativescript-zxing

ZXing for NativeScript
Other
15 stars 22 forks source link

How to use in Nativescript-Vue #23

Closed Patrick777777 closed 5 years ago

Patrick777777 commented 5 years ago

Hi, I've tried this plugin with NativeScript-Vue. I followed the instructions in the documentation, but I can't get it to work, I keep getting this error: "Cannot read property 'BarcodeFormat' of undefined". Is there any way to use this plugin with NativeScript-Vue?

acidbiscuit commented 5 years ago

Implementation is pretty straightforward:

<script>
import { fromNativeSource } from 'tns-core-modules/image-source'
import ZXing from 'nativescript-zxing'

export default {
  name: 'MyComponent',
  data () {
    return {
      imageSource: null
    }
  },
  beforeMount () {
    const zx = new ZXing()
    const barcode = zx.createBarcode({ encode: 'some-text', height: 200, width: 200, format: ZXing.QR_CODE })
    this.imageSource = fromNativeSource(barcode)
  }
}
</script>

Now imageSource can be used as an imageSource attribute for Image element inside templates.

<template>
  <Image
    :imageSource="imageSource"
  />
</template>
Patrick777777 commented 5 years ago

thanks for the help, i got it working 👍

acidbiscuit commented 5 years ago

@Patrick777777 Awesome! Please, don't forget to close the issue.

oliverpool commented 5 years ago

It could be great to add this code snippet to the demo folder https://github.com/NathanaelA/nativescript-zxing/tree/master/demo :-)

NathanaelA commented 5 years ago

@oliverpool - It would be awesome if someone wanted to create a NS-Vue demo. I love PR's. ;-)