Search code examples
nativescriptnativescript-vuenativescript-plugin

Nativescript Vue Core Modules


I'm having trouble understanding how to use the Framework modules in nativescript-vue.

For example this module, is what I am trying to understand how to use: https://docs.nativescript.org/ns-framework-modules/platform

Do I need to declare in App.js / Main.js or in the component I'm going to use?

I come from Web Development with Vue.js, I think an implementation example I'm trying to follow would look like this:

It is an example of the web that I am thinking of being the same as the way of implementation in the mobile.

On the web if I needed to use Axios I would import it into App.js / Main.js to stay global in the application or SPF to stay local and be able to call in the desired components.

In mobile if I use pure nativescript the import is clear, but in nativescript-vue I can not understand how to use it, I do not know if it already comes configured by default or not.


Solution

  • Import modules where you need them in your code, both app.js and SPF.

    app.js

    import axios from "axios";
    var platform = require("tns-core-modules/platform");
    
    var instance = axios.create({ baseURL: "https://myendpoint.com" });
    if (platform.isAndroid) { /* ... */ }
    

    File.vue

    import axios from "axios";
    var platform = require("tns-core-modules/platform");
    
    export default {
        methods: {
            androidFunc() {
                if (platform.isIOS) {
                    axios.get("https://website.com/api/ios") // ...
                }
            }
        }
    }
    

    If I need something globally, I usually use Vue instance properties (Vue.prototype.$myProperty) or Vuex store.