What is the best way to integrate qrcode-generator into an Angular 2 application?

I've been trying to implement the qrcode-generator in my app without success, even though it works in plunker. In my app, I am using angular-cli and angular 2.rc-1.
Here are the steps to reproduce:

ng new newAppName
cd newAppName
ng serve

It works up to this point.

npm i qrcode-generator // (please note that this is missing the svg support).
ng serve // still working

Then, make changes in 2 files. angular-cli-build.js:

var Angular2App = require('angular-cli/lib/broccoli/angular2-app');
module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'systemjs/dist/system-polyfills.js',
      'systemjs/dist/system.src.js',
      'zone.js/dist/**/*.+(js|js.map)',
      'es6-shim/es6-shim.js',
      'reflect-metadata/**/*.+(js|js.map)',
      'rxjs/**/*.+(js|js.map)',
      'qrcode-generator/**/*.+(js|js.map)',
      '@angular/**/*.+(js|js.map)'
    ]
  });
};

and system-config.ts:

/**********************************************************************************
 * User Configuration.
 *********************************************************************************/
/** Map relative paths to URLs. */
const map: any = {
  'qrcode-generator': 'vendor/qrcode-generator'
};
const packages: any = {
  'vendor/qrcode-generator': {
    main: 'qrcode',
    defaultExtension: 'js'
  }
};
// ... the rest remains the same

Now, edit new-app-name.component.ts and import the qrcode-generator like this

// vscode will underline the qrcode-generator string and complain about not finding it
import * as qrcode from 'qrcode-generator'; 

After that, run ng serve but it errors out with the following message:

/path/to/project/newAppName/tmp/broccoli_type_script_compiler-input_base_path-jscpZEq5.tmp/0/src/app/new-app-name.component.ts (3, 25): Cannot find module 'qrcode-generator'.

I attempted to install typings for it by adding the following to the typings.json file:

"globalDependencies": {
    "qrcode-generator": "registry:dt/qrcode-generator#0.0.0+20160412152159"
  }

and then running:

typings i

The installation was successful, but the error persisted. angular-cli version:

angular-cli: 1.0.0-beta.5
node: 6.2.0
os: linux x64

Am I overlooking something?
Do I need to configure something else?

Answer №1

After seeking guidance from @JavascriptMick on angular-cli's gitter, I was finally successful in importing the necessary code. The process involved setting the format to global:

'vendor/qrcode-generator': {
    format: 'global',
    main: 'qrcode.js'
  }

Subsequently, during the import phase, I had to follow this structure:

import 'qrcode-generator';
declare let qrcode;

Hopefully, this information proves useful to others.

Answer №2

Not knowing your exact requirements, I can offer you an AngularJS (Version 1) application that includes a QR-Code generator:

Within this app, you can:

  • Input the text to encode
  • Choose the error correction level
  • Select the size of the QR code
  • View, copy, or download the generated QR code

I created this page, utilizing the QR generator tool called jsqrencode, developed by [email protected]

You can access the source code for this project on GitHub at the following link: https://github.com/uniqueusername/qr-generator/blob/main/index.html

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS: Advanced Routing for Dynamic Web Applications

Hello, I am currently exploring the possibility of implementing something similar to this code snippet using AngularJS: $routeProvider .when('/root/:controllerName/blah/:blahId/blah/:blah', { templateUrl: '/tmpl/:controllerName ...

The property you are trying to access is not found within the declared type of 'IntrinsicAttributes & { children?: ReactNode; }'

In my React project created using Create-React-App, I have included the following packages (relevant to the issue at hand): "react": "^16.13.1", "react-dom": "^16.13.1", "react-router-dom": "^5.1.2", "react-scripts": "3.4.1", "typescript": "^3.9.2", "@typ ...

Restricting the number of users in Socket.io

Currently, I am in the process of developing an app focused on chat rooms where each room is restricted to only 2 individuals. This application should also keep track of the sequence in which users join a particular room. Building upon a socket-based chat ...

The Angular ui-calendar introduces an innovative approach to event addition, providing users with

I need help with adding events to the angular ui calendar. Currently, I am using $scope.events.push() method to add events, but they get reset when changing the month. If anyone has experience with angular ui-calendar and event addition, please provide ...

The functionality of the Protractor right click feature is smooth, however, there seems to be an issue with selecting

https://i.sstatic.net/KoGto.png Even though I can locate the button within the context menu, I am facing difficulty in clicking it. The code mentioned below is successfully able to click the button, but an error message pops up indicating: Failed: script ...

Utilizing lazy evaluation, multiple functions are triggered by ng-click in succession

Successfully disabled ngClick on an element when the scope variable (notInProgress) is set to true as shown below: <a data-ng-click="notInProgress || $ctrl.setTab('renewal');</a> Now, I want to include additional functions to be execut ...

Uncertain about navigating the Ionic update

I have been actively working with Ionic 3 on multiple projects, but now I find myself in a position where upgrading to Ionic 5 is necessary to stay current. The process of upgrading to Ionic 5 seems daunting as it requires significant rework of my existin ...

Error: The specified property is not found in type 'never' - occurring with the ngFor loop variable

When working with API responses and dynamically sorting my view, I utilize an ngFor loop. Here's the snippet of code in question: <agm-marker *ngFor="let httpResponses of response" [latitude]= "httpResponses.lat" [longitude]=& ...

Issue with Typescript in react: JSX element lacks construct or call signatures

After upgrading TypeScript, I encountered the error mentioned above in one of my components. In that component's render method, I have the following code: render() { const Tag = props.link ? 'a' : 'div'; return ( < ...

angular: the world of guarantees and assistance

I'm struggling to grasp the concept of promises. I understand what they are supposed to do, but when it comes to writing or debugging them, I hit a roadblock. MyController.js (function() { angular.module('WizmoApp').controller('St ...

What is the proper way to define the type of an object when passing it as an argument to a function in React using TypeScript?

I'm struggling to figure out the appropriate type definition for an Object when passing it as an argument to a function in React Typescript. I tried setting the parameter type to "any" in the function, but I want to avoid using "any" whenever passing ...

Having trouble utilizing HTML Canvas in the newest release of Angular and TypeScript

After extensive searching on the internet, I have not been able to find any working examples of using HTML canvas in Angular. It seems that changes in syntax in Typescript and Angular's newer versions have rendered all existing solutions obsolete. I ...

exploring the ins and outs of creating computed properties in TypeScript

How can I store an object with a dynamically assigned property name in an array, but unsure of how to define the array properly? class Driver { public id: string; public name: string; constructor(id , name) { this.id = id; th ...

What is the best way to ensure that a div containing lengthy text wraps to the next line as if it were only text overflow?

Is there a way to make a div or span within another div act as text, causing overflow to shift to the next line? I'm unsure of which CSS properties would achieve this effect. I've attempted using overflow-wrap: break-word; word-break: break-al ...

When a child is added to a parent in Angular UI Tree, it automatically appears in all parent nodes as well

I've been experimenting with the drag and drop feature of Angular UI Tree, and I've encountered a puzzling issue. The JSON data is fetched from my services. Upon receiving it in my controller, I need to format it correctly by adding an empty arra ...

Is there a way to incorporate a <u> tag into an inline angular variable in HTML that has been generated by a ternary expression?

Currently, I have the following scenario. <label> Make {{$ctrl.tcTemplate.isLibrary ? $ctrl.tcTemplate.name : "this item"}} a Library item: <toggle ng-change="changed(); $ctrl.eraseName();" ng-model="$ctrl.tcTemplate.isLibrary;" off="Fals ...

Looking for a specific phrase in the data entered by the user

I am dealing with data in ckeditor that looks like this: <p>test 1</p> <p>test 2</p> <p><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICw ...

JSDoc encounters issues when processing *.js files that are produced from *.ts files

I am currently working on creating documentation for a straightforward typescript class: export class Address { /** * @param street { string } - excluding building number * @param city { string } - abbreviations like "LA" are acceptable ...

Angular does not delay for promises to settle

Issue I am facing is related to Angular not waiting for promises to be resolved. The console inspection reveals that the provider and skills objects are not retrieved before the promises are returned. I have included the key parts of the code below. The s ...

What is the best way to organize the output of a directive?

The Directive below is what I am working with: app.directive('sidebar', function () { return { restrict: 'E', replace: true, template: '<li ng-repeat="(keymenu, valmenu) in menu"><a href="{{val ...