首页 > 解决方案 > Nativescript Vue 核心模块

问题描述

我无法理解如何在 nativescript-vue 中使用框架模块。

例如这个模块,是我试图了解如何使用的: https ://docs.nativescript.org/ns-framework-modules/platform

我需要在 App.js / Main.js 或我要使用的组件中声明吗?

我来自 Vue.js 的 Web 开发,我认为我尝试遵循的实现示例如下所示:

这是我认为与移动设备中的实现方式相同的网络示例。

在网络上,如果我需要使用 Axios,我会将其导入 App.js / Main.js 以在应用程序中保持全局,或者将 SPF 导入以保持本地并能够调用所需的组件。

在移动设备中,如果我使用纯 nativescript 导入是明确的,但在 nativescript-vue 中我无法理解如何使用它,我不知道它是否已经默认配置。

标签: nativescriptnativescript-vuenativescript-plugin

解决方案


在代码中需要它们的地方导入模块,包括 app.js 和 SPF。

应用程序.js

import axios from "axios";
var platform = require("tns-core-modules/platform");

var instance = axios.create({ baseURL: "https://myendpoint.com" });
if (platform.isAndroid) { /* ... */ }

文件.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") // ...
            }
        }
    }
}

如果我需要全局的东西,我通常使用 Vue 实例属性 (Vue.prototype.$myProperty) 或 Vuex 存储。


推荐阅读