简体   繁体   English

将function导入js文件后VueJS报错

[英]VueJS error after importing a function into a js file

I have made a js script that allows me to send data to a database and I would like to import it into my.vue file so that I can use it when I click on a button.我制作了一个 js 脚本,允许我将数据发送到数据库,我想将它导入到 my.vue 文件中,以便在单击按钮时可以使用它。 The problem is that Vue is showing me this error when I have the script imported and nothing is displayed on the page:问题是当我导入脚本并且页面上没有显示任何内容时,Vue 向我显示此错误:

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

"export 'default' (imported as 'mod') was not found in '-!../../node_modules/cache-loader/dist/cjs.js??ref--12-0!../../node_modules/babel-loader/lib/index.js!../../node_modules/cache-loader/dist/cjs.js??ref--0-0!../../node_modules/vue-loader/lib/index.js??vue-loader-options!./Journal.vue?vue&type=script&lang=js&'

my js script:我的 js 脚本:

const mysql = require("mysql");

async function main(userID, text) {
  const connection = mysql.createConnection({
    host: "",
    user: "",
    password: "",
    database: "",
  });
  connection.connect(function (err) {
    if (err) throw err;
    console.log("Connected!");
    var sql = `INSERT INTO 'journal' ('userID', 'journalTXT') VALUES ('${userID}', '${text}')`;
    connection.query(sql, function (err) {
      if (err) throw err;
      console.log("1 record inserted");
      connection.destroy();
    });
  });
}
main();

my vue page:我的页面:

<template>
  <div class="about" v-if="!$auth.isAuthenticated">
    <h1>Bienvenue sur ton journal</h1>
    <h2>Continue d'écrire ce qu'il te passe par la tête</h2>
    <v-container fluid>
      <v-row>
        <v-col cols="7" sm="12">
          <v-form>
            <v-textarea
              id="textarea"
              counter
              placeholder="Commence à écrire quelque chose ici "
              label="Mon journal"
              v-model="journal_txt"
            ></v-textarea>
          </v-form>
        </v-col>
      </v-row>
      <br />
      <v-btn elevation="2" x-large v-on:click="dataExport(journal_txt)"
        >Sauvegarder mon travail</v-btn
      >
    </v-container>
  </div>
</template>
<script>
import main from "@/sendToDB.js";
module.exports = {
  data: function () {
    return {
      journal_txt: "",
    };
  },
  methods: {
    dataExport: function (txt) {
      main.main("1", txt);
    },
  },
};
</script>

If anyone has the solution, I'll gladly take it;)如果有人有解决方案,我会很乐意接受;)

You trying load NodeJS script from the client via VUE, its impossible because vue is rendering in the client only.您尝试通过 VUE 从客户端加载 NodeJS 脚本,这是不可能的,因为 vue 仅在客户端呈现。

you should consider use Nuxt.JS你应该考虑使用 Nuxt.JS

https://nuxtjs.org https://nuxtjs.org

Via nuxt you can create server middleware and use the connection via axios or $http to your api middleware in order to update your database.通过 nuxt,您可以创建服务器中间件并使用通过axios$http连接到您的 api 中间件,以更新您的数据库。

https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-servermiddleware https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-servermiddleware

Hope its helped.希望它有所帮助。

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

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