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.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 the 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 textarea 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:

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

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/

如何在Angular 10中生成QR码相关推荐

  1. 在你的计算机上使用qr码登录,如何在Android 10上使用QR码共享您的Wi-fi凭据 | MOS86...

    Android 10随附的新功能之一可能看起来很小,但非常方便. 新的Wi-Fi共享功能使用户只需扫描QR码即可连接到您的网络. 仅使用QR码共享Wi-Fi网络凭据是Android 10中更便捷的新功 ...

  2. 快速生成QR码的方法:教你变成QR Code Master

    目录 简介: 具体实现步骤: 一.可以使用Python中的qrcode和tkinter模块来生成QR码.以下是一个简单的例子,演示如何在Tkinter窗口中获取用户输入并使用qrcode生成QR码. ...

  3. 如何在 Windows 10 中搭建 Node.js 环境?

    2019独角兽企业重金招聘Python工程师标准>>> [编者按]本文作者为 Szabolcs Kurdi,主要通过生动的实例介绍如何在 Windows 10 中搭建 Node.js ...

  4. 组策略禁止自动锁定计算机,如何在Windows 10中禁用自动锁定

    原标题:如何在Windows 10中禁用自动锁定 Windows 10附带了许多功能,这样使工作更加轻松.其实有很多方法可以根据自己的喜好个性化使用.可以通过多种方式来个性化您的体验并控制计算机的行为 ...

  5. Android多个imei如何获取,如何在Android 10中获取IMEI号,这是获取在Android 10及以下Android 10中获取IMEI号的代码...

    如何在android 10中获取imei编号,这是获取在android 10及以下android 10中获取imei编号的代码. if (android.os.Build.VERSION.SDK_IN ...

  6. OpenCV在相机或图像中检测QR码的实例(附完整代码)

    OpenCV在相机或图像中检测QR码的实例 OpenCV在相机或图像中检测QR码的实例 OpenCV在相机或图像中检测QR码的实例 #include "opencv2/objdetect.h ...

  7. 如何在Angular Material中制作自定义主题

    by Charlee Li 通过李李 如何在Angular Material中制作自定义主题 (How to make a custom theme in Angular Material) Angu ...

  8. 如何在 Windows 10 中安装 WSL2 的 Linux 子系统

    本文介绍如何在 Windows 10 中安装 WSL2 的 Linux 子系统 本文内容 第一步:启用虚拟机平台和 Linux 子系统功能 第二步:安装一个 Linux 发行版 第三步:启用 WSL2 ...

  9. win10图标重建缓存_如何在Windows 10中重建损坏的图标缓存

    win10图标重建缓存 The icons that Windows uses for your documents and programs are saved in an icon cache, ...

最新文章

  1. 鸿蒙智慧屏和pro有什么区别,体验揭秘荣耀智慧屏pro评测怎么样?荣耀智慧屏pro和普通版区别有什么不同?...
  2. [转]linux tr命令详解
  3. 有用的mysql语句
  4. ITK:单相Chan和Vese密集域水平集分割
  5. web.xml配置(转)
  6. lightoj 1214
  7. oracle11g linux 日期格式设置
  8. 学了这么久Java都没搞懂位运算符
  9. 学习英语,先记下留着后用
  10. 使用原生javaScript创建ul和li对象以及操作
  11. hdu 2680 Choose the best route【dijstra+反向建图】
  12. React 中setState更新state何时同步何时异步?
  13. 华为路由器设置静态IP
  14. Python计算某年某月某日天数
  15. MSN机器人 博客助手 for I-Favourite
  16. C#爬虫爬取京东自营笔记本
  17. 十分感谢--致谢好友的
  18. which must be escaped when used within the value
  19. pyecharts 画折线图去掉折线上小圆圈
  20. 计算机如何恢复桌面,如何恢复电脑原始桌面

热门文章

  1. 【AI视野·今日CV 计算机视觉论文速览 第232期】Thu, 8 Jul 2021
  2. django-上传图片-后台上传
  3. 数据结构与算法-时间复杂度
  4. 数据结构与算法-算法入门-什么是算法-算法的挑战
  5. 开发者都应该知道的15个API
  6. Java 语言实现的 I/O 模型
  7. TypeScript + React 学习render props
  8. 13商软 《软件工程》课程设计
  9. Winform使用DevExpress的WaitDialogForm画面 z
  10. nm 命令 程序符号信息查看