我想知道是否可以使用 vue-flash-message在没有安装 NPM 或 Node.js 的 WordPress 插件中。我的想法是尝试包括 script
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<script src="<?php echo plugins_url('/vue-comp/vue-flash-message/vue-flash-message.min.js', __FILE__);?>"></script>
然后做类似的事情:
<script type="text/javascript">
import Vue from 'vue';
import VueFlashMessage from 'vue-flash-message';
Vue.use(VueFlashMessage);
new Vue({
// root node
el: "#vue_contact_form_app",
// the instance state
data: function () {
return {}
}
然后做
this.flash(...);
但这行不通。
最佳答案
您正在使用 import
语句,该语句用于将外部模块加载到当前作用域中。
在最简单的理解层面,这些行:
import Vue from 'vue';
import VueFlashMessage from 'vue-flash-message';
只能在NodeJS环境下有效。更重要的是,它还需要像 Babel 这样的转译器来使这些行代码在 NodeJS 环境中有效,因为 NodeJS 的模块管理系统目前只支持加载外部模块的 require()
方法。
如果你不知道 Babel 是什么,这是对它的简短描述:
Babel is a JavaScript compiler. Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments.
然后,你的问题的答案:
is it possible to use vue-flash-message in a WordPress plugin without NPM or Node.js installed
那是不可能的。
关于vue.js - 是否可以在不安装 Node.js 的情况下使用 vue-flash-message?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52524141/