How can i save a string that i use frequently in my ionic app. I know in android studio you can save them in the string folder and use them frequently. what is the ionic 3 equivalent to that?
Create a constants file like app.constants.ts
export const appConst = {
recordPerPage: 20,
dateFormat: 'dd/mm/yyyy
};
Then import in other pages like below
import { appConst } from './../../../app.constants';
You can use in code like appConst.recordPerPage
Related
I have a tableau dashboard that I am trying to embed into a react website using the tableau-api npm package. Although it looks fine on tableau public, the layout changes when I embed it. How do I ensure that the layout stays the same when I embed it in react?
Edit: here is my code. I used this answer as reference
import React, { Component } from 'react';
import tableau from 'tableau-api';
class Visualization extends Component {
componentDidMount() {
this.initTableau()
}
initTableau() {
const vizUrl = 'url';
const vizContainer = this.vizContainer;
let viz = new window.tableau.Viz(vizContainer, vizUrl)
}
render() {
return (
<div ref={(div) => { this.vizContainer = div }}>
</div>
)
}
}
export default Visualization;
There are a couple of options you should take a look at
Change the size of your dashboard from automatic to fixed (reference here). This will help if you have floating elements in your dashboard
Add your preferred device to the dashboard panel
Change the fit option to entire view
If none of these options work, I would recommend you share a photo of your dashboard and your code to be able to debug
import { Plugins } from '#capacitor/core';
const { Browser } = Plugins;
Browser.open({ url: 'http://www.africau.edu/images/default/sample.pdf', windowName:'_self' });
When the url is specified as a link to a website, it works fine but doesnt when specified a PDF. Could someone please suggest if any changes required ? Do i need to specify the rel ? If yes how ? There is no such key to be passed in open call. Response received is 'NO enabled plugin supports this MIME type'.
The Browser plugin from Capacitor should work fine for this. Try without specifying windowName.
// Capacitor v2
import { Plugins } from '#capacitor/core';
const { Browser } = Plugins;
Browser.open({ url: 'http://www.africau.edu/images/default/sample.pdf'});
// or for Capacitor v3
import { Browser } from '#capacitor/browser';
Browser.open({ url: 'http://www.africau.edu/images/default/sample.pdf'});
InAppBrowser will not pdf files. you will have to use it in some other way,
first you can simply add a href this will open user default browser.. like for android it will open this link in android and for ios it will use safari.
open pdf
Second way is to use a plugin for document viewer .. i have added its link below
constructor(private document: DocumentViewer) { }
const options: DocumentViewerOptions = {
title: 'My PDF'
}
this.document.viewDocument('http://www.africau.edu/images/default/sample.pdf', 'application/pdf', options)
https://ionicframework.com/docs/native/document-viewer
I am working on a javaScript / react playground (something like very simple codesandbox.io) and I'm trying to figure out how to transpile the code. I was thinking of using Babel transform but the app itself is built using create-react-app so I do not have access to Babel. My question is, if I do something like the following and install Babel, will it also override how create-rect-app currently transpiles the code for the app?
// transpile.js
const babelOptions = {
presets: [ "react", ["es2015", { "modules": false }]]
}
export default function preprocess(str) {
const { code } = Babel.transform(str, babelOptions);
return code;
}
EDIT:
I've since learned that I can use Babel standalone for exactly this use case! Now it's just to figure out how to configure it. I would still appreciate help but if I find a solution first I will post for others :)
Ok so I have figured this out but it is not straight forward. I will try to add some details here in case anyone else finds it helpful.
I first needed to load Babel standalone and so I used this answer to create a custom hook to load a script:
import { useEffect } from 'react';
const useScript = url => {
useEffect(() => {
const script = document.createElement('script');
script.src = url;
script.async = true;
document.body.appendChild(script);
console.log(`${url} script loaded`);
return () => {
document.body.removeChild(script);
console.log(`${url} script removed`);
}
}, [url]);
};
export default useScript;
then I used it in my component like this:
import useScript from '../../../hooks/useScript';
useScript("https://unpkg.com/#babel/standalone/babel.min.js");
then I later use the code I wrote in the initial question to transpile my code.
When using the Cameara to take a picture with destinationType: this.camera.DestinationType.FILE_URI, the resulting URL will not work to display the image. For example, when attempting to take a photo like this:
this.camera.getPicture(options).then((url) => {
// Load Image
this.imagePath = url;
}, (err) => {
console.log(err);
});
Attempting to display it as <img [src]="imagePath" > will result in an error (file not found).
The problem here is that the URL is in the file:///storage... path instead of the correct one based on localhost.
In previous versions of Ionic, this would be solved by using normalizeURL. This will not work on Ionic 4 (or at least I could not make it work).
To solve this issue, you will need to use convertFileSrc():
import {WebView} from '#ionic-native/ionic-webview/ngx';
...
this.camera.getPicture(options).then((url) => {
// Load Image
this.imagePath = this.webview.convertFileSrc(url);
}, (err) => {
console.log(err);
});
Now the image URL will be in the appropriate http://localhost:8080/_file_/storage... format and will load correctly.
See WKWebView - Ionic Docs for more information.
In my case, the following code works with me
const downloadFileURL = 'file:///...';
// Convert a `file://` URL to a URL that is compatible with the local web server in the Web View plugin.
const displayedImg = (<any>window).Ionic.WebView.convertFileSrc(downloadFileURL);
In case some gots here looking for the answer on ionic4, check this out
"Not allowed to load local resource" for Local image from Remote page in PhoneGap Build
and look for the answer from #Alok Singh that's how I got it working on ionic4 and even works with livereload
UPDATE december 2021:
You have to install the new Ionic Webview
RUN:
ionic cordova plugin add cordova-plugin-ionic-webview
npm install #awesome-cordova-plugins/ionic-webview
Import it in app.module and your page where you wanna use it:
import { WebView } from '#awesome-cordova-plugins/ionic-webview/ngx';
image = "";
constructor(private webview: WebView){}
Then this will work:
this.camera.getPicture(options).then((imageData) => {
this.image = this.webview.convertFileSrc(imageData)
}, (err) => {
// Handle error
});
And show it in the HTML page:
<img [src]="image" alt="">
I need to generate barcode based on some string data in angular2 nativescript.
Please help me to achieve this.
Font wasn't a good option for my purposes because I can't control it's width/height.
nativescript-zxing is not compatible with nativescript-barcodescanner.
So, I've done next thing
import { DOMImplementation, XMLSerializer } from 'xmldom';
import * as JsBarcode from 'jsbarcode';
const xmlSerializer = new XMLSerializer();
const document = new DOMImplementation().createDocument('http://www.w3.org/1999/xhtml', 'html', null);
const svgNode = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
JsBarcode(svgNode, 'test', { xmlDocument: document });
const svgText = xmlSerializer.serializeToString(svgNode);
const buffer = new Buffer(svgText);
const src = `data:image/svg+xml;base64,${buffer.toString('base64')}`;
And now src can be passed to SVGImage from plugin nativescript-svg
You should probably try this one nativescript-zxing. This plugin allows you to read barcodes, QR-codes, etc. and create them as well based on provided string. Check the documentation for more information. Have a nice day!
If you only need to generate a barcode and not scan it, you can just use a barcode font and set it to your label, following these steps:
Find a barcode font that suits your needs and copy the font file to the /font folder in your app. This font is a good option.
Add create a new class in your app.css that sets the font-family to the barcode font:
.barcode {
font-family: free3of9 /* Replace with the actual name of your font, free3of9 is the name of the font above */
}
Add Label with text set to the value that you want the barcode to represent and class set to your css class. In order to work correctly you need to prefix and suffix the text with *'s. For example, if you want your barcode to read 12316516132, your Label should look like:
<Label text="*12316516132*" class="barcode"></Label>
This is more efficient way than installing a plugin as it has very little footprint on your application resources.
Thanks to #rsboarder I was able to provide a final solution.
Below the steps should be done to make it work
Install (if any) the following plugins:
npm i jsbarcode
npm i xmldom
npm i #sergeymell/nativescript-svg
don't forget to add this to the module yourname.module.ts
import { NativeScriptSvgModule } from '#sergeymell/nativescript-svg/angular'
...
imports [
...
NativeScriptSvgModule
...
]
...
My config it looks like this:
Create this method on your component yourname.component.ts
import * as JsBarcode from 'jsbarcode'
import { DOMImplementation, XMLSerializer } from 'xmldom'
import { Buffer } from 'buffer'
import { ImageSource } from '#nativescript/core'
...
getBarcodeSVGSrc(barcodeNumber, barcodeFormat: string): string {
const xmlSerializer = new XMLSerializer()
const document = new DOMImplementation().createDocument('http://www.w3.org/1999/xhtml', 'html', null)
const svgNode = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
let data: string = barcodeNumber
const result: ImageSource = new ImageSource()
JsBarcode(svgNode, data, {
xmlDocument: document,
format: barcodeFormat // here: https://github.com/lindell/JsBarcode/wiki/ the various barcode formats EAN13, CODE38, etc.
})
const svgText = xmlSerializer.serializeToString(svgNode)
const buffer = Buffer.from(svgText)
return `data:image/svg+xml;base64,${buffer.toString('base64')}`
}
Complete the HTML file yourname.component.html
< StackLayout >
<SVGImage [src]="getBarcodeSVGSrc(item.barcodenr, item.barcodetype)">
< /StackLayout >