1

I am building a NativeScript-Angular app which displays a link than needs to open in a new browser on both the web and mobile.

For Angular, I can use window.open("http://example.com", "_blank"); - which does not work for NativeScript.

For NativeScript I can use utilityModule.openUrl(http://example.com); where var utilityModule = require("utils/utils"); - but this does not work with Angular (and it can't resolve and tns modules).

Is there any way I can use a single method in the my component to launch the default browser on a mobile and also open a new tab on a web browser?

Roland
  • 85
  • 3
  • 11

1 Answers1

1

Define a Service and have two implementations of it in your code you import "service"

  • service.ts: this will have the WEB version on how to open the link.
  • service.tns.ts: this will have the nativescript version on how to open the link.
  • Thanks Jose. Haven't given it a go yet but sounds like exactly what's needed. – Roland Mar 18 '20 at 21:44
  • Hi Jose, I haven't yet marked this as the answer as it seems like the .tns version of my service is not being picked up. Should this happen automatically? Or do I need to ad something else to a configuration file. The tsconfig.tns.ts already has entry "paths": { "@src/*": [ "src/*.tns.ts", "src/*.ts" ] } – Roland Mar 25 '20 at 17:02
  • The error I am getting is System.err: Error: Unexpected value '[object Object]' imported by the module 'AppModule' – Roland Mar 25 '20 at 17:45
  • Issue was obviously user error :-) I had to remove all references to utils/utils and will start that over – Roland Mar 25 '20 at 19:16