tony-xlh / Ionic-Vue-QR-Code-Scanner

Ionic Vue QR Code Scanner
2 stars 1 forks source link

ScannerPage callback to HomePage #7

Open akira32 opened 2 months ago

akira32 commented 2 months ago

I have a HomePage that have two scan buttons. I wish I click one of scan button of HomePage, its will call onScanned function of ScannerPage and then call someone function of HomePage? Does someone know how do I reach to this goal? I need a callback function in HomePage that can do something. Example of getting some data from some web. Two scan buttons have individual functions.

My ScannerPage.vue onScanned code as below:

      const onScanned = (results:TextResult[]) => {
        //console.log("onScanned");//暫時註解
        if (results.length>0 && scanned === false && sharedStates.continuousScan === false) {
          scanned = true;
          document.documentElement.style.setProperty('--ion-background-color', ionBackground);

          let id = route.query.id;
          sharedStates.currentID = id as string;//search read

          sharedStates.barcodeResults = results;

          //router.push({path:'/home', query:{id:id, results: results});

          //emit("onScannedReceive",{id:id, results: results});
          router.back();
        }
        else
        {
          barcodeResults.value = results;
        }
      }
akira32 commented 2 months ago

I had solved this problem by watch.

//store
mutations: {
  setScannedData(state, payload) {
    state.scannedData = payload; 
  }
},

//HomePage.vue
export default {
  setup() {
    const store = useStore();
    const scannedData = computed(() => store.state.scannedData); 

    watch(scannedData, (newValue, oldValue) => {
      if (newValue) {
        onScannedReceive(newValue.id, newValue.barcodeText);
      }
    });