如何在Angular 10中生成QR码
In this tutorial, we'll learn how to generate QR codes in Angular 10 by building a simple example application.
在本教程中,我们将通过构建一个简单的示例应用程序来学习如何在Angular 10中生成QR码。
But first of all, what's a QR code and what does it do?
但是首先,什么是QR码,它的作用是什么?
According to Wikipedia:
根据维基百科 :
A QR code (abbreviated from Quick Response code) is a type of matrix barcode (or two-dimensional barcode) first designed in 1994 for the automotive industry in Japan.
QR码(快速响应码的缩写)是一种矩阵条形码(或二维条形码),于1994年首次为日本的汽车行业设计。
A QR code (abbreviated from Quick Response code) is a type of matrix barcode (or two-dimensional barcode) first designed in 1994 for the automotive industry in Japan.
QR码(快速响应码的缩写)是一种矩阵条形码(或二维条形码),于1994年首次为日本的汽车行业设计。
A barcode is a machine-readable optical label that contains information about the item to which it is attached.
条形码是一种机器可读的光学标签,其中包含有关其所附着物品的信息。
A barcode is a machine-readable optical label that contains information about the item to which it is attached.
条形码是一种机器可读的光学标签,其中包含有关其所附着物品的信息。
In practice, QR codes often contain data for a locator, identifier, or tracker that points to a website or application.
实际上,QR码通常包含指向网站或应用程序的定位器,标识符或跟踪器的数据。
In practice, QR codes often contain data for a locator, identifier, or tracker that points to a website or application.
实际上,QR码通常包含指向网站或应用程序的定位器,标识符或跟踪器的数据。
So it's simply a compact and efficient way of storing data.
因此,这只是一种紧凑而有效的数据存储方式。
Now let's see how to generate QR codes in your Angular 10 apps by creating an example.
现在,让我们看看如何通过创建示例在Angular 10应用程序中生成QR码。
先决条件 (Prerequisites)
Before getting started you need a few prerequisites:
在开始之前,您需要满足一些先决条件:
- Basic knowledge of TypeScript. Particularly the 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 10+的本地开发计算机,并安装了NPM 6+ 。
Node is required by the Angular CLI like the most front end tools nowadays. You can simply go to the downloads page of the official website and download the binaries for your operating system.
像当今最前端的工具一样,Angular CLI需要节点。 您可以直接转到官方网站的下载页面,然后下载适用于您的操作系统的二进制文件。
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。 但是,推荐的方法是使用NVM (节点版本管理器),它是POSIX兼容的bash脚本,用于管理多个活动的Node.js版本。
Note: 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 frontend development that lets you create an Angular project compatible with the Angular CLI.
注意 :是否不想为Angular开发安装本地环境,但仍想尝试本教程中的代码? 您可以使用Stackblitz (用于前端开发的在线IDE)来创建与Angular CLI兼容的Angular项目。
步骤1 —安装Angular CLI 10 (Step 1 — Installing Angular CLI 10)
In this step, we'll install the latest Angular CLI 10 (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, 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:
CLI会问您几个问题:
Would you like to add Angular routing? Type y for Yes, and
您想添加角度路由吗? 键入y表示是,然后
Which stylesheet format would you like to use? Choose CSS.
您想使用哪种样式表格式? 选择CSS 。
Next, navigate to you 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.tsfile, and import
NgxQRCodeModulefrom
@techiediaries/ngx-qrcodein 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-qrcodecomponent in your Angular application.
导入库后,您可以在Angular应用程序中使用
ngx-qrcode组件。
Please note that we have also imported the
FormsModule.请注意,我们还导入了
FormsModule。
Next, open the
src/app/app.component.tsfile 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.htmlfile 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 textarea for entering the value that you want to encode:
接下来,添加一个文本区域以输入要编码的值:
<textarea [(ngModel)] = "value"></textarea>
Finally open the
src/styles.cssfile 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:
这是我们的应用程序的屏幕截图:
That's it we have finished our Angular 10 example project that demonstrates how to generate QR codes.
就是这样,我们已经完成了Angular 10示例项目,演示了如何生成QR码。
You can visit us on Techiediaries
for tutorials about Angular and modern web development practices.
您可以在Techiediaries
上访问我们, Techiediaries
获取有关Angular和现代Web开发实践的教程。
You can check out the application we've built in this article live on Stackblitz:
您可以在Stackblitz上现场查看我们在本文中构建的应用程序:
翻译自: https://www.freecodecamp.org/news/generate-qr-codes-in-angular-10/
- 如何生成1-10之间不重复的随机数
- 如何删除 Windows 10 系统生成的 WindowsApps 文件夹
- Arcgis ArcMap 10 如何生成msd地图文档定义【 arcgis mxd怎么转换成msd】
- 【转】有rand7(可以随机生成1到7的数据的随机函数),如何产生rand10(随机产生1-10的数)
- 如何使用Java生成具有安全哈希的QR码
- Arcgis ArcMap 10 如何生成msd地图文档定义【 arcgis mxd怎么转换成msd】
- yii 自动生成的内容,分页信息(Displaying 1-10 of 15 results.)如何修改或是yii cgridview template summary
- 如何使用zend studio10生成wsdl文件
- 如何使用Visual Studio 2010在数据库中生成随机测试数据
- 如何使用Visual Studio 2010在数据库中生成随机测试数据
- vs2008如何生成安装文件
- 如何自动生成论文目录
- 七步从Angular.JS菜鸟到专家(1):如何开始
- 如何利用ASP.NET技术动态生成HTML页面
- AngularJS开发指南3:Angular主要组成部分以及如何协同工作
- 如何将android源码生成eclipse可用的jar,以marvell的fmmanager为例
- ADS1.2如何生成BIN文件
- 深度学习100问-10:如何部署一个轻量级的深度学习项目?
- 如何生成每秒百万级别的 HTTP 请求?
- 【转】如何在高并发分布式系统中生成全局唯一Id