vue.js - 使用 vue-js-modal 组件的问题
问题描述
我按照他们文档中有关如何使用该组件的说明进行操作,但是我得到了TypeError: show is not a function
在我的主 JS 文件(app.js)中,我添加了以下内容并使用 npm 添加到我的项目中
import VModal from 'vue-js-modal'
Vue.use(VModal)
文档指出我现在可以从应用程序中的任何位置调用模式,因此我创建了一个特定于页面的 JS 文件并包含以下内容以隐藏/显示name="hello-world"
包含 vue、app.js 和页面特定配置文件的页面上的模式.js 文件。
export default {
methods: {
show() {
this.$modal.show('hello-world');
},
hide() {
this.$modal.hide('hello-world');
}
}
}
当我加载页面时,我看不到模态内容,但是当我单击链接时,<a href="#" @click.prevent="show">Modal</a>
我收到有关 show 方法的错误TypeError: show is not a function
我正在使用 laravel mix 并验证所有内容都按预期编译。下面是我如何在个人资料页面上包含 JS 文件:
<script type='text/javascript' src='/assets/js/manifest.js?ver=5.2.3'></script>
<script type='text/javascript' src='/assets/js/vendor.js?ver=5.2.3'></script>
<script type='text/javascript' src='/assets/js/app.js?ver=1569678574'></script>
<script type='text/javascript' src='/assets/js/profile.js?ver=1569678574'></script>
我正在尝试“升级”我的 Vue 和 JavaScript 体验,以前我只是坚持编写 ES5,而我的 Vue 是在没有组件的情况下编写的,并且绑定到特定于页面的 Vue 实例,因此非常感谢任何帮助!
编辑
应用程序.js
window.Vue = require('vue');
require('./global/header.js');
Vue.component('tabs', require('./components/Tabs.vue'));
Vue.component('tab', require('./components/Tab.vue'));
import VModal from 'vue-js-modal'
Vue.use(VModal)
new Vue({
el: '#app'
});
profile.js
export default {
methods: {
show() {
this.$modal.show('hello-world');
},
hide() {
this.$modal.hide('hello-world');
}
}
}
编译 profile.js 的 webpack.mix.js
mix
.js("resources/js/pages/home.js", "assets/js/home.js")
.js("resources/js/pages/teams.js", "assets/js/teams.js")
.js('resources/js/pages/profile.js', 'assets/js/profile.js')
解决方案
该错误未指定它是 $modal.show() 函数还是未定义的 profile.js show() 函数。我怀疑这是您的 profile.js show() 函数,因为对于 vue-js-modal,看起来一切正常。
您需要将 profile.js 添加为 vue mixin ( https://vuejs.org/v2/guide/mixins.html ),以便将其功能添加到 vue 实例中。所以在你的 app.js 中添加:
import profile from '/assets/js/profile'
Vue.mixin(profile);
推荐阅读
- python - 如何知道其他人的Django项目使用的python包版本并相应地安装它们?
- python - 如何在 sqlalchemy 中使用特定数字启动主键?
- flutter - 当我的 ListView 在展开时出现 RenderBox 错误
- javascript - 如何将图像设置为所有
- tensorflow - 如果 D-Drive 中安装了 anaconda,如何使用 TensorFlow GPU?
- c# - 如何比较具有相同属性的两个视图模型,如果属性的值不同,则将该属性添加到不同的列表中?
- google-bigquery - 我可以从 Can 存储传输到 BigQuery 的最大文件大小是多少
- python - 如何在 Python 中使用正则表达式检测以空格分隔的数字?
- sql-server - 使用 SSIS 将 Excel 文件中的数据以及文件名加载到 SQL Server 的方法
- java - 如何从 putty 远程下载最新的 JDK8