在 Nativescript Playground 中导入 npm 包

Importing npm packages in Nativescript Playground

我正在尝试学习 nativescript-vue 我正在使用 Nativescript 的 Playground 来试用我的示例代码。我正在尝试使用 nativescript-localstorage 包,以便我可以将一些信息存储到本地存储中:

每当我尝试保存项目时,它都会出现使用编译错误

错误如下:

An error occurred while transpiling nativescript-localstorage/localstorage.js. unknown: We found a path that isn't a NodePath instance. Possiblly due to bad serialisation.

我按照教程添加了包,因为我的代码如下所示:

import Vue from 'nativescript-vue';
import Vuex from 'vuex';
import localStorage from 'nativescript-localstorage';
import userStore from './user-store';

//For local storage of vuex tools
const NSVuexPersistent = store => {
 // Init hook.
 let storageStr = localStorage.getItem('ns-vuex-persistent');
 if (storageStr) {
  store.replaceState(JSON.parse(storageStr))
 }
 store.subscribe((mutation, state) => {
  // Subscribe hook.
  localStorage.setItem('ns-vuex-persistent', JSON.stringify(state));
 })
};

Vue.use(Vuex);

const debug = process.env.NODE_ENV !== 'production';

export default new Vuex.Store({
    modules: {
        userStore
    },
    strict: debug,
    plugins: [NSVuexPersistent]
})

由于项目没有保存,所以我没有分享 link。帮我解决这个问题。谢谢。

nativescript-vue 打包在预览版 APK 中,因此可以导入为

import Vue from 'nativescript-vue'

但是 nativescript-localstorage 是你在项目中安装的东西,所以在 Playground 中你应该使用相对路径来导入模块,比如

import * as localStorage from "../nativescript-localstorage"

// Or

const localStorage = require("../nativescript-localstorage");

只有当包在其导出中将某些内容标记为默认值时,您才能使用 import name from "package",该语法在 ES6 和 Vue 插件中很常用。 nativescript-localstorage 默认情况下没有导出任何内容。