【问题标题】:Function is not define功能未定义
【发布时间】:2018-10-19 18:09:33
【问题描述】:

这是我的代码:

// And my javascript file verbatims.js :
class Verbatims {
    
      list() {
        return ["c'est super", "j'aime pas", "ça marche bien"];
      }
    }
    
    module.exports = Verbatims;
<!DOCTYPE html>
<html lang="fr">

	<head>
        <meta charset="utf-8">
        <meta content="X-Content-Type-Options: nosniff">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">        
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>
        <script src="/src/verbatims.js" type="application/javascript"></script>
        
		<title>My Project</title>
	</head>

	<body>
        <div id="PRISME_data">
            Le nombre de requête est de  : {{ nb_request }}<br>
            <button v-on:click="change">Change value</button>
            <button v-on:click="stop">Arrêter</button>
        </div>

        <script>
            let app = new Vue({
                el:'#PRISME_data',
                data: {
                    nb_request: "toto",
                    ite: 0
                },
                methods: {
                    change: function() {
                        changeNbRequest()
                    },
                    stop: function() {
                        clearInterval()
                    }
                }
            });

            changeNbRequest = function() {
                var timer = setInterval(function() {
                    let verbatim = new Verbatims().list()[ite];
                }, 5000);
            }
        </script>
	</body>
</html>

当我尝试用我的节点服务器显示我的页面时,我遇到了这个错误:

Uncaught ReferenceError: Verbatims is not defined at index:45

在这条线上:let verbatim = new Verbatims().list()[ite];

我不明白为什么,我尝试了很多东西但没有任何效果!你有什么想法吗?

谢谢你:)

【问题讨论】:

  • module.exports = Verbatims;这是第45行的内容吗?
  • 不抱歉,我忘了准确!错误在这一行:let verbatim = new Verbatims().list()[ite];
  • 您是否确认(使用开发人员工具的“网络”选项卡)verbatims.js 正在成功加载?在您引用的错误之前,控制台上是否有任何先前的错误?
  • verbatims.js 加载成功,在此之前控制台上没有错误。

标签: javascript html node.js vue.js


【解决方案1】:

这可能是由于以下原因:

  • verbatims.js 文件没有加载到浏览器中,因为 错误的路径。
  • 到达浏览器的verbatims.js 文件确实 不包含您的课程。
  • 您没有在引用脚本上导入 Verbatims 类。

修复第三个选项应该是这样的:

 <script type="module">
  import {Verbatims} from './src/vebatim.js';

   ....ommitted for brevity

  changeNbRequest = function() {
            var timer = setInterval(function() {
                let verbatim = new Verbatims().list()[ite];
            }, 5000);
  }
 </script>

附加提示

要改进您的代码,请尝试将 index.html 中的所有 JS 代码放到一个外部文件中,以便您可以利用 ES6 导入/导出功能。

【讨论】:

  • 所以我的文件正在浏览器中加载,它包含我的类。所以你的第三个选择,但现在我的浏览器没有加载我的 vue ...
  • 我已经尝试将我的 js 代码放在一个外部文件中,但是它无法识别我的类 Vue
猜你喜欢
  • 2016-10-29
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-14
  • 2015-09-30
相关资源
最近更新 更多