当前位置: 首页 > 知识库问答 >
问题:

如何通过vue实例在单独的JS文件中调用函数[duplicate]

邵骏喆
2023-03-14

我有一个javascript函数在一个单独的js文件toaster-message.js,该文件在ASP. net应用程序的www root/js中,它调用引导烤面包机。

烤面包机留言。js。

showCompleteToast: (message) => {

    const toastElm = document.getElementById('#complete-toast');
    const toast = new bootstrap.Toast(toastElm)

    toast.show();
}

我想从我的vue实例调用show CompleteToast()。我创建vue实例与直接脚本包含。

我不想在Vue实例之外向函数添加任何Vue依赖项。那么,调用Vue实例之外的外部js文件中的函数的正确方法是什么呢?

@section scripts {

    <script>

        const app = new Vue({
            el: "#app",
            data() {
                return {

                }
            },
            methods: {
                showToast: function(){
                   //I want to call the show toast from here
                },
                submit: async function () {
                    try {

                        this.showToast();

                    } catch (error) {
                      
                        console.log(error)
                    }
                }
            }

        });
    </script>
}

当我尝试使用以下方式导入时:

import { showCompleteToast } from "~/lib/js/toater-message.js"

使用时:

export default {
   showCompleteToast: (message) => {

       const toastElm = document.getElementById('#complete-toast');
       const toast = new bootstrap.Toast(toastElm)

       toast.show();
   },
   // ... other methods here
};

我得到的错误是:

“Uncaught SyntaxError: Cannot use import statement outside a module”

我尝试使用以下方式导入:

<script type="module">
    import { showCompleteToast } from "../../wwwroot/js/toaster-message.js"
    alert(showCompleteToast)
</script>

这给出了错误为:

GET https://localhost:44307/wwwroot/js/toaster-message.js net::ERR_ABORTED 404

共有1个答案

凌和颂
2023-03-14

我不太熟悉php,但通常可以导入JavaScript文件,然后处理其内容。唯一的要求是导入的文件需要定义导出。

// toaster-message.js

export default {
   showCompleteToast: (message) => {

       const toastElm = document.getElementById('#complete-toast');
       const toast = new bootstrap.Toast(toastElm)

       toast.show();
   },
   // ... other methods here
};

然后像这样把它拉到你的Vue文件中:

@section scripts {

    <script>
        import { showCompleteToast } from "..path/to/toaster-message.js"
        const app = new Vue({
            el: "#app",
            data() {
                return {

                }
            },
            methods: {
                showToast: function(){
                   //I want to call the show toast from here
                   showCompleteToast();
                },
                submit: async function () {
                    try {

                        this.showToast();

                    } catch (error) {
                      
                        console.log(error)
                    }
                }
            }

        });
    </script>
}
 类似资料:
  • 本文向大家介绍vue项目中在外部js文件中直接调用vue实例的方法比如说this,包括了vue项目中在外部js文件中直接调用vue实例的方法比如说this的使用技巧和注意事项,需要的朋友参考一下 一般我们都是在main.js中引入vue,然后在vue文件中直接使用this(this指向的是vue实例),但是在实际开发中,我们往往会引入外部的js文件使用this,这个this就会指向window,并

  • function参数是一个数组。现在我将如何在一个特性文件中调用函数。当我尝试下面的代码 我收到以下错误: 中读取属性“length” 我已经打印了并且它正确地出现在print语句中。

  • 问题内容: 我正在尝试从外部PHP文件将JavaScript函数调用为JavaScript脚本。我的代码又大又不同,所以我在这里编写示例代码。 这是我的PHP代码: 这是我的JavaScript代码: 这就是我想要做的。 我 原来的 PHP文件不包含这些数学函数,但是想法是相同的。 如果一些它怎么没有一个妥善的解决办法,那么可能你 请 提出一种替代,但它应该从外部PHP调用值。 问题答案: 是的,

  • 我在@Click上调用了3个函数,我是这样做的: 首先调用getBillPrice(),然后调用updateBill(),然后调用postData(),这一点很重要。但是这些函数的运行顺序是错误的,首先是updateBillPrice,然后是postData(),然后是getBillPrice() 我该如何修复它?以下是我的一些功能。这是文本可以发布这么多代码,您可以直接跳过此文本:Lorem i

  • 基本上我想这样做: 使用其他gradle文件中的Gradle函数 也就是说,在一个gradle脚本中从另一个调用一个函数。问题是我的build.gradle是静态编程语言(build.gradle.kts),我的函数所在的脚本仍然是groovy。 我遵循了groovy到groovy的上述链接,但我无法使用Kotlin DSL实现这一点。 在我的groovy文件中,函数。gradle,我有: 和 然

  • 目前正在学习Linux,在《Linux命令行与shell脚本编程大全(第3版)》的17.7.2小节中,作者写到:“只要是在shell脚本中,都可以用source命令(或者它的别名点操作符)将库文件中的函数 添加到你的.bashrc脚本中......更好的是,shell还会将定义好的函数传给子shell进程,这样一来,这些函数就自动能够用于该shell会话中的任何shell脚本了。”,原文如下图所示