在 vue 3 中读取 ic 卡需要通过以下步骤:安装 nfc 插件 vuex-nfc。创建 vuex 模块 nfc,添加 nfc api 插件。使用 this.$nfc.read() 读取 ic 卡,并将其数据存储到 vuex 模块中。从 vue 组件中通过 this.$store.state.nfc.carddata 获取读取的数据。
Vue 3 中读取 IC 卡
在 Vue 3 中读取 IC 卡需要使用 NFC 模块。NFC(近场通信)是一种短距离无线通信技术,允许设备在近距离(通常为 10 厘米以内)交换数据。
步骤:
安装依赖项:
立即学习“前端免费学习笔记(深入)”;
创建 Vuex 模块:
集成 NFC API:
import VuexNfc from 'vuex-nfc';
export default new Vuex.Module({
state: {
cardData: null
},
mutations: {
setCardData(state, data) {
state.cardData = data;
}
},
plugins: [VuexNfc]
})
读取 IC 卡:
this.$nfc.read()
.then(cardData => {
this.$store.commit('nfc/setCardData', cardData);
})
.catch(error => {
console.error(error);
});
获取读取的数据:
const cardData = this.$store.state.nfc.cardData;
注意: