添加插件
1 |
$ ionic plugin add phonegap-plugin-barcodescanner |
home.ts代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import { Component } from '@angular/core'; import { NavController } from 'ionic-angular'; import { BarcodeScanner } from 'ionic-native'; @Component({ templateUrl: 'home.html' }) export class HomePage { public text: String; public format: String; constructor(public navCtrl: NavController) { } scan() { BarcodeScanner.scan().then((result) => { if (!result.cancelled) { alert("text:" + result.text); alert("format: " + result.format); } alert("success"); }).catch((err) => { alert(err); }) } } |
home.html代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<ion-header> <ion-navbar> <ion-title>Home</ion-title> </ion-navbar> </ion-header> <ion-content padding> <h2>Welcome to Ionic!</h2> <p> This starter project comes with simple tabs-based layout for apps that are going to primarily use a Tabbed UI. </p> <p> Take a look at the <code>src/pages/</code> directory to add or change tabs, update any existing page or create new pages. </p> <button ion-button (click)="scan()">扫描二维码</button> </ion-content> |
真机调试
1 |
$ ionic run ios --device |