简体   繁体   English

如何在 VueJS 应用程序上使用导出的函数?

[英]How to use exported functions on VueJS app?

I'm using the Firebase to make users' auth, the Firebase service is works fine.我正在使用 Firebase 进行用户身份验证,Firebase 服务运行良好。 I'm newbie with it and I want delegate the Firebase's services to a class, or file, or module (I don't know exactly the correct way).我是它的新手,我想将 Firebase 的服务委托给一个类、文件或模块(我不知道正确的方法)。 So I created a Settings.vue module to collect data and a UserService.js to make the work, communicate with Firebase and store locally using Vuex, and return success or fail, and when return get the data from state (Vuex) using computed on Settings.vue .所以我创建了一个Settings.vue模块来收集数据和一个UserService.js来完成工作,与 Firebase 通信并使用 Vuex 在本地存储,并返回成功或失败,并在返回时使用计算从状态(Vuex)获取数据Settings.vue At the moment this is my files:目前这是我的文件:

Settings.vue

<template> ... </template>

<script>
import UserService from "@/services/UserService";

export default {
  name: "Settings",
  mounted() {},
  methods: {
    saveDisplayName() {
      UserService.updateDisplayName(this.displayName).then(function(retorno) {
        console.log("Returning: " + retorno);
      });
    }
  },
  data() {
    return {
      displayName: '',
      password: ''
    };
  }
};
</script>

UserService.js

import firebase from 'firebase'

var updateDisplayName = function (displayName) {
    return new Promise(function (resolve, reject) {
        var user = firebase.auth().currentUser;
        console.log("updateDisplayName")
        setTimeout(function () { resolve("resolved! " + displayName) }, 3000);
    })
}

var updatePassword = function (password) {
    return new Promise(function (resolve, reject) {
        var user = firebase.auth().currentUser;
        console.log("updatePassword")
        setTimeout(function () { resolve("resolved! " + password) }, 3000);
    })
}

So, when I run it I receive the follow message:因此,当我运行它时,我收到以下消息:

 WARNING  Compiled with 1 warnings                                                                              11:54:41 PM

 warning  in ./src/views/Settings.vue?vue&type=script&lang=js&

"export 'default' (imported as 'UserService') was not found in '@/services/UserService'

You can export your function in UserService.js您可以在UserService.js导出您的函数

try to add this code in UserService.js尝试在UserService.js添加此代码

export default {
  updateDisplayName,
  updatePassword
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM