Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

javascript - 如何将node.js项目中的JavaScript变量导入到html文件中?(How do I import a JavaScript variable that is in a node.js project to a html file?)

I want to know how do I import variables from a node project into a normal javascript file that doesn't use node.

(我想知道如何将变量从节点项目导入到不使用节点的普通javascript文件中。)

I only need the value of the variables

(我只需要变量的值)

my index.js file:

(我的index.js文件:)

const sequelize = require('sequelize')

const bd =  new sequelize('usuarios', 'root', 'sintese>marketing', {
    host: 'localhost',
    dialect: 'mysql'
})

const Usuario = bd.define('usuario', {
    telefone: {
        type: sequelize.STRING,
        allowNull: false
    },
    ddd: {
        type: sequelize.INTEGER,
        allowNull: false
    },
    data_de_entrada: {
        type: sequelize.DATEONLY,
        allowNull: false
    }
})

async function contar_entrantes(dia, mes, ano) {
    dados = await Usuario.findAndCountAll({
        where: {
            data_de_entrada: ano + '-' + mes + '-' + dia
        }
    })
    return(dados.count)
}
let hoje = new Date();
let entrantes_dia_1 = contar_entrantes(hoje.getDate() - 4, hoje.getMonth() + 1, hoje.getFullYear())
let entrantes_dia_2 = contar_entrantes(hoje.getDate() - 3, hoje.getMonth() + 1, hoje.getFullYear())
let entrantes_dia_3 = contar_entrantes(hoje.getDate() - 2, hoje.getMonth() + 1, hoje.getFullYear())
let entrantes_dia_4 = contar_entrantes(hoje.getDate() - 1, hoje.getMonth() + 1, hoje.getFullYear())
let entrantes_dia_5 = contar_entrantes(hoje.getDate() - 4, hoje.getMonth() + 1, hoje.getFullYear())

let dia_1 = (hoje.getDate() - 4) + '/' + (hoje.getMonth()+1) + '/' + hoje.getFullYear()
let dia_2 = (hoje.getDate() - 3) + '/' + (hoje.getMonth()+1) + '/' + hoje.getFullYear()
let dia_3 = (hoje.getDate() - 2) + '/' + (hoje.getMonth()+1) + '/' + hoje.getFullYear()
let dia_4 = (hoje.getDate() - 1) + '/' + (hoje.getMonth()+1) + '/' + hoje.getFullYear()
let dia_5 = hoje.getDate() + '/' + (hoje.getMonth()+1) + '/' + hoje.getFullYear()

module.exports = {entrantes_dia_1, entrantes_dia_2, entrantes_dia_3, entrantes_dia_4, entrantes_dia_5, dia_1, dia_2, dia_3, dia_4, dia_5}

index.html file:

(index.html文件:)

<!DOCTYPE html>
<html>
    <head>
        <canvas id="myCanvas"></canvas>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
        <script type="text/javascript" src="index.js"></script>

    </head>
    <body>
            <script>
                let ctx = document.getElementById('myCanvas').getContext('2d');

                let chart = new Chart(ctx, {
                    type: 'bar',
                    data: {
                        labels: [dia_1, dia_2, dia_3, dia_4, dia_5],
                        datasets: [{
                            label: 'Entrantes',
                            backgroundColor: 'rgb(255, 99, 132)',
                            borderColor: 'rgb(255, 99, 132)',
                            data: [entrantes_dia_1,
                                entrantes_dia_2,
                                entrantes_dia_3,
                                entrantes_dia_4,
                                entrantes_dia_5]
                        }]
                    }
                })
            </script>
    </body>
</html>

I want to use the variables in module export in my html file.

(我想在HTML文件中的模块导出中使用变量。)

I only need these variables values.

(我只需要这些变量值。)

It says these variables aren't defined when I try to use them in html script.

(它说这些变量在我尝试在html脚本中使用时未定义。)

How do I do this?

(我该怎么做呢?)

  ask by Hockpond translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...