使用Angular 10生成QR码

angular 生成二维码

使用Angular 10生成QR码 (Generate QR Codes with Angular 10)

Image source Pexels.com
图片来源Pexels.com

In this tutorial, we’ll learn how to generate QR codes in Angular by building a simple example application using the latest Angular 10 version. But first of all, what is a QR code anyway, and what purpose does it serve?

在本教程中,我们将通过使用最新的Angular 10版本构建一个简单的示例应用程序,学习如何在Angular中生成QR代码。 但是首先,什么是QR码,它的目的是什么?

According to Wikipedia “A QR code (abbreviated from Quick Response code) is a type of matrix barcode (or two-dimensional barcode) [that]… often contain[s] data for a locator, identifier, or tracker that points to a website or application. A QR code uses four standardized encoding modes (numeric, alphanumeric, byte/binary, and kanji) to store data efficiently.”

根据Wikipedia的说法,“ QR码(缩写为快速响应代码)是一种矩阵条形码(或二维条形码),它……通常包含指向网站的定位器,标识符或跟踪器的数据。或应用程序。 QR码使用四种标准化的编码模式(数字,字母数字,字节/二进制和汉字)来有效地存储数据。”

Put simply, a QR code is a compact and efficient way of storing data. Now let’s look at how to generate QR codes in your Angular 10 apps by example.

简而言之,QR码是一种紧凑而有效的数据存储方式。 现在,让我们看一下如何在Angular 10应用程序中生成QR码。

先决条件 (Prerequisites)

Before getting started you need a few things:

在开始之前,您需要注意以下几点:

  • Basic knowledge of TypeScript, particularly familiarity with Object-Oriented concepts such as TypeScript classes and decorators.

    TypeScript的基本知识,尤其熟悉面向对象的概念,例如TypeScript类和装饰器。
  • A local development machine with Node 10+, together with NPM 6+ installed. Node is required by the Angular CLI like the most frontend tools nowadays. You can simply go to the download page of the official website and download the binaries for your operating system. You can also refer to your specific system instructions for how to install Node using a package manager. The recommended way though is using NVM — Node Version Manager — a POSIX-compliant bash script to manage multiple active Node.js versions.

    装有Node 10+的本地开发计算机,并安装了NPM 6+ 。 像当今最前端的工具一样,Angular CLI要求使用节点。 您可以直接转到官方网站的下载页面,然后下载适用于您的操作系统的二进制文件。 您也可以参考特定的系统说明,以了解如何使用程序包管理器安装Node。 但是,推荐的方法是使用NVM (节点版本管理器),它是POSIX兼容的bash脚本,用于管理多个活动的Node.js版本。

Note: If you don’t want to install a local environment for Angular development but still want to try the code in this tutorial, you can use Stackblitz, an online IDE for front-end development that you can use to create an Angular project compatible with Angular CLI.

注意 :如果您不想为Angular开发安装本地环境,但仍想尝试本教程中的代码,则可以使用Stackblitz ,这是用于前端开发的在线IDE,可用于创建与Angular项目兼容的使用Angular CLI。

第1步-安装Angular CLI 10 (Step 1 — Installing Angular CLI 10)

In this step, we’ll install the latest Angular CLI 10 version (at the time of writing this tutorial).

在此步骤中,我们将安装最新的Angular CLI 10版本(在编写本教程时)。

Angular CLI is the official tool for initializing and working with Angular projects. To install it, open a new command-line interface and run the following command:

Angular CLI是用于初始化和使用Angular项目的官方工具。 要安装它,请打开一个新的命令行界面并运行以下命令:

$ npm install -g @angular/cli

At the time of writing this tutorial, angular/cli v10 will be installed on your system.

在撰写本教程时,您的系统上将安装angular / cli v10

第2步-创建新的Angular 10应用 (Step 2 — Creating a New Angular 10 App)

Let’s now create our project. Head back to your command-line interface and run the following commands:

现在创建项目。 回到您的命令行界面并运行以下命令:

$ cd ~
$ ng new angular10qrcode

The CLI will ask you a couple of questions — If “Would you like to add Angular routing?” Type “y” for Yes, and for “Which stylesheet format would you like to use?” Choose “CSS”.

CLI会问您几个问题-如果“ 是否要添加Angular路由?” 为“是和“ 您要使用哪种样式表格式?”输入y” 。 选择“ CSS”

Next, navigate to your project’s folder and run the local development server using the following commands:

接下来,导航到项目的文件夹,并使用以下命令运行本地开发服务器:

$ cd angular10qrcode
$ ng serve

Open your web browser and navigate to the http://localhost:4200/ address to see your app running.

打开您的Web浏览器并导航到http://localhost:4200/地址,以查看您的应用程序正在运行。

Next, open a new terminal and make sure to navigate to your project’s folder and run the following command to install the ngx-qrcode library from npm using the following command:

接下来,打开一个新终端,并确保导航到项目的文件夹并运行以下命令,以使用以下命令从npm安装ngx-qrcode

$ npm install @techiediaries/ngx-qrcode

Next, open the src/app/app.module.ts file, and import NgxQRCodeModule from @techiediaries/ngx-qrcode in your module, as follows:

接下来,打开src/app/app.module.ts文件,并从模块中的@techiediaries/ngx-qrcode导入NgxQRCodeModule ,如下所示:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { NgxQRCodeModule } from '@techiediaries/ngx-qrcode';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
NgxQRCodeModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

Once the library has been imported, you can use the ngx-qrcode component in your Angular application.

导入库后,您可以在Angular应用程序中使用ngx-qrcode组件。

Please note that we have also imported FormsModule.

请注意,我们还导入了 FormsModule

Next, open the src/app/app.component.ts file and update it as follows:

接下来,打开src/app/app.component.ts文件并如下更新:

import { Component } from '@angular/core';
import { NgxQrcodeElementTypes, NgxQrcodeErrorCorrectionLevels } from '@techiediaries/ngx-qrcode';@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
elementType = NgxQrcodeElementTypes.URL;
correctionLevel = NgxQrcodeErrorCorrectionLevels.HIGH;
value = 'https://www.techiediaries.com/';
}

Next, open the src/app/app.component.html file and add the following code:

接下来,打开src/app/app.component.html文件并添加以下代码:

<ngx-qrcode
[elementType]="elementType"
[errorCorrectionLevel]="correctionLevel"
[value]="value"
cssClass="bshadow"></ngx-qrcode>

We use various properties for configuring our QR code such as:

我们使用各种属性来配置我们的QR代码,例如:

  • The type

    方式
  • The error correction level

    纠错等级
  • The value

    价值
  • The CSS class

    CSS类

You can find out more information about these properties and the other supported properties from the official ngx-qrcode docs.

您可以从ngx-qrcode官方文档中找到有关这些属性和其他受支持属性的更多信息。

Next, add a text area for entering the value that you want to encode:

接下来,添加文本区域以输入要编码的值:

<textarea [(ngModel)] = "value"></textarea>

Finally, open the src/styles.css file and add the following styles:

最后,打开src/styles.css文件并添加以下样式:

.bshadow {  display: flex;
align-items: center;
justify-content: center;
filter: drop-shadow(5px 5px 5px #222222);
opacity: .5;}textarea {
margin-top: 15px;
display: block;
margin-left: auto;
margin-right: auto;
width: 250px;
opacity: .5;
}

This is a screenshot of our application:

这是我们的应用程序的屏幕截图:

结论 (Conclusion)

That’s it! We have finished our Angular 10 example for demonstrating how to generate QR codes in your Angular apps. You can visit us on Techiediaries for tutorials about Angular and modern web development practices.

而已! 我们已经完成了Angular 10示例,以演示如何在Angular应用中生成QR码。 您可以在Techiediaries上访问我们, Techiediaries获取有关Angular和现代Web开发实践的教程。

You can check out the application we’ve built in this article live on https://stackblitz.com/edit/angular-ngx-qrcode-example.

您可以在https://stackblitz.com/edit/angular-ngx-qrcode-example上实时查看我们在本文中构建的应用程序。

翻译自: https://codeburst.io/generate-qr-codes-with-angular-10-dc698df0713d?source=topic_page---------6------------------1

angular 生成二维码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值