扫描产品,然后使用nativescript-vue导航到另一个组件

时间:2018-08-18 13:40:32

标签: vuejs2 fetch nativescript nativescript-vue

我正在使用nativescript-vue制作一个简单的应用程序,在主页上,我有一个按钮可以启动扫描仪,以便可以扫描产品。这是我的主页:

<template>
  <Page class="page">

   <StackLayout class="hello-world">
     <Button @tap="scan" text="Scan a product"/>
   </StackLayout>

  </Page>
</template>

<script>
const BarcodeScanner = require("nativescript- 
barcodescanner").BarcodeScanner;
import Display from "./Display";
export default {
data() {
  return {
    text: ""
  };
},
methods: {
scan: function() {
  var barcodescanner = new BarcodeScanner();
  barcodescanner
    .scan({
      // formats: "QR_CODE,PDF_417", // Pass in of you want to restrict scanning to certain types
      cancelLabel: "EXIT. Also, try the volume buttons!", // iOS only, default 'Close'
      cancelLabelBackgroundColor: "#333333", // iOS only, default '#000000' (black)
      message: "Use the volume buttons for extra light", // Android only, default is 'Place a barcode inside the viewfinder rectangle to scan it.'
      showFlipCameraButton: true, // default false
      preferFrontCamera: false, // default false
      showTorchButton: true, // default false
      beepOnScan: true, // Play or Suppress beep on scan (default true)
      torchOn: false, // launch with the flashlight on (default false)
      closeCallback: function() {
        console.log("Scanner closed");
      }, // invoked when the scanner was closed (success or abort)
      resultDisplayDuration: 500, // Android only, default 1500 (ms), set to 0 to disable echoing the scanned text
      // orientation: "landscape", // Android only, optionally lock the orientation to either "portrait" or "landscape"
      openSettingsIfPermissionWasPreviouslyDenied: true // On iOS you can send the user to the settings app if access was previously denied
    })
    .then(
      function(result) {
        console.log("Scan format : " + result.format);
        console.log("Scan text :   " + result.text);
        this.text = result.text.trim();
         this.$navigateTo(Display, {context: {propsData: {"code":  this.text }}});

      },
      function(error) {
        console.log("No scan: " + error);
      }
    );
  }
}
};
</script>

一旦扫描完成,我想导航到我的其他组件“ Display”,在这里我想使用之前扫描过的文本向我的api进行提取请求并获得有关它的一些产品信息,这是我的显示组件:

<template>
  <Page class="page">
    <ActionBar class="action-bar" title="Results"/>

   <StackLayout>
     <Label v-if="posts.product_name" :text="posts.product_name" />
     <Image :src="posts.image_thumb_url" stretch="none" />
     <Label :text="posts.ingredients_text_fr" class="list-group-item-text" textWrap="true"></Label>
   </StackLayout>

  </Page>
</template>

<script>
export default {
  props: {
    code: {
     type: String,
     required: true,
   }
  },
data() {
  return {
    posts: [],
  };
},
methods: {},
beforeCreate() {
  fetch(
  `my-api.com/product/${this.code}.json`
)
  .then(response => response.json())
  .then(data => {
   this.posts = data.product;
      });
  }
};
 </script>

但是在这里我有一个问题,我不知道如何调用我的api,何时调用(挂载,beforeMounted...。)。因为我的“显示”组件似乎已经与我的“主页”组件同时安装了。

2 个答案:

答案 0 :(得分:1)

您可以将数据值设置为true,然后让Display组件使用v-if指令。这也将安装它。

data:{
   showDisplay: false
}

<Display v-if ='showDisplay' />

someAsyncMethod().then(result => {
   // do init here
   this.showDisplay = true
}

答案 1 :(得分:1)

请勿使用function() {},而是使用箭头功能(=>)。如果使用this,则带有箭头功能的this上下文不会更改,另一方面,function() {}的上下文将根据调用上下文而更改。