Vue.js 尝试导入库时出错

Vue.js error when I'm trying to import library

我很尴尬,因为我不知道为什么我的库不起作用。

注意:我是 Vue.js

的初学者

我已经安装了一个库调用"Bluetooth Serial",我想在组件中使用它,但是尝试了很多还是不行

我在我的入口点main.js

import BluetoothSerial from "@ionic-native/bluetooth-serial/ngx";
Object.defineProperty(Vue.prototype, '$bluetoothSerial', { value: BluetoothSerial });

在我的组件中:

<script>
export default {
  name: "WattComponent",
  props: {},
  methods: {
    scan: function() {
      this.checkBluetoothEnabled();
    },

    checkBluetoothEnabled: function() {
      this.$bluetoothSerial.isEnabled().then(
        success => {
          console.log("enabled");
        },
        error => {
          console.log("erro");
        }
      );
    }
  }
};
</script>

它正在重新调整:

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in v-on handler: "TypeError: Cannot read property 'isEnabled' of undefined"

found in

---> <WattComponent> at src/components/WattComponent.vue
       <Home> at src/views/Home.vue
         <App> at src/App.vue
           <Root>

试试这个:

在你的 main.js(Vue 实例的创建位置):

import Vue from 'vue';
import BluetoothSerial from "@ionic-native/bluetooth-serial/ngx";

Vue.prototype.$bluetoothserial = BluetoothSerial;

在你的 .vue 文件中:

methods: {
   foo(){
       this.$bluetoothserial.isEnabled().then(() => {
          //some code here
       });
   }
}