Project by TS, global variable

nativescriptcore

#1

Hello !

In project by JS I defined and used on next lines:

global.config = require("config");      // app/tns_modules   config.js
global.oClientData = {};
oClientData.user_info={};
oClientData.user_info.first_name=config.rhost;

config.js:

var rhost="https://test.tophr.kz/";

module.exports = {
    rhost: rhost,
    loginURL: rhost + "login",
    timeout: 90000,
    currentUrl : rhost + "supplierProfile/employees/current",
    auth: false
};

How they are will in ts project ?


#2

in appState.ts:

export namespace AppState {
    export var config = {
        myValue: 4;
    }
}

Then where ever you want to use AppState:

import { AppState } from "path/to/appState"; //that is, path to appState.ts

@Component({
    selector: "Some",
    moduleId: module.id,
    templateUrl: "./some.component.html",

})
export class SomeComponent implements OnInit {
    config = AppState.config;

    constructor() {
        // some init code
        this.config.myValue = 1337;
    }
  

    ngOnInit() {
        // something
        console.log(this.config.myValue);
    }

}

#3

My project was created via next command:

tns create tophr --template https://github.com/NativeScript/template-drawer-navigation-ts --appid kz.tophr

By your example I did next:

  1. created file config.ts in folder /app/
  2. added lines in config.ts:
export namespace AppState {
    export var config = {
        myValue: 4
    }
}
  1. in file /app/app.ts inserted:
import { AppState } from "./config"; //that is, path to config.ts

@Component({
    selector: "Some",
    moduleId: module.id,
    templateUrl: "./some.component.html",

})
export class SomeComponent implements OnInit {
    config = AppState.config;

    constructor() {
        // some init code
        this.config.myValue = 1337;
    }
    ngOnInit() {
        // something
        console.log(this.config.myValue);
    }
}

Now, IDE show errors:

[ts] Cannot find name 'Component’

[ts] Cannot find name 'OnInit’


#4

Sorry, I undestood.

in file /app/app.ts inserted:

import { AppState } from "./config"; //that is, path to config.ts

let config=AppState.config;

console.log("config="+config);
console.log("config.txt="+JSON.stringify(config));

console:


#5

How can I declare global the variable config ?

let config=AppState.config;

In my example, I can’t used it in other module.
/app-root/app-root.ts:

export function onLoaded(args: EventData): void {
    const drawerComponent = <RadSideDrawer>args.object;

    console.log("app.js, config="+config);
    console.log("app.js, config.txt="+JSON.stringify(config));
    
    drawerComponent.bindingContext = new AppRootViewModel();
}

Error:
[ts] Cannot find name ‘config’.


#6

add:
import { Component, OnInit } from "@angular/core";
on top of app.ts


#7

[ts] Cannot find module ‘@angular/core’


#8

Open a TypeScript file.
Click the TypeScript version to the bottom right in the Status Bar.
Choose Use Workspace Version from the message box.


#9

Didn’t solved problem.
Any versions ?


#10

app/app-root/app-root.ts(12,40): error TS2304: Cannot find name ‘config’.
app/app-root/app-root.ts(13,59): error TS2304: Cannot find name ‘config’.
app/app.ts(1,35): error TS2307: Cannot find module ‘@angular/core’.


#11

Is is normal ?

PS F:\NScript\tophr> npm install angular-module-core --save
npm WARN extract-text-webpack-plugin@3.0.2 requires a peer of webpack@^3.1.0 but none is installed. You must install peer dependencies yourself.
npm WARN schema-utils@0.4.3 requires a peer of webpack@^2.0.0 || ^3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ angular-module-core@1.2.19
added 67 packages and updated 1 package in 34.111s
PS F:\NScript\tophr>


#12

Result:

PS F:\NScript\tophr> tns run android
Searching for devices...
Executing before-watchPatterns hook from F:\NScript\tophr\hooks\before-watchPatterns\nativescript-dev-sass.js
Executing before-watchPatterns hook from F:\NScript\tophr\hooks\before-watchPatterns\nativescript-dev-typescript.js
Executing before-watchPatterns hook from F:\NScript\tophr\hooks\before-watchPatterns\nativescript-dev-webpack.js
Executing before-watch hook from F:\NScript\tophr\hooks\before-watch\nativescript-dev-sass.js
Found peer node-sass
Executing before-watch hook from F:\NScript\tophr\hooks\before-watch\nativescript-dev-typescript.js
Found peer TypeScript 2.7.2
17:04:26 - Starting compilation in watch mode...



app/app-root/app-root.ts(12,40): error TS2304: Cannot find name 'config'.

app/app-root/app-root.ts(13,59): error TS2304: Cannot find name 'config'.
app/app.ts(1,35): error TS2307: Cannot find module '@angular/core'.

17:04:34 - Compilation complete. Watching for file changes.



Executing before-watch hook from F:\NScript\tophr\hooks\before-watch\nativescript-dev-webpack.js
Executing before-shouldPrepare hook from F:\NScript\tophr\hooks\before-shouldPrepare\nativescript-dev-webpack.js
Skipping prepare.
Installing...
Successfully installed on device with identifier '192.168.173.101:5555'.
Successfully transferred all files.