Tia*_*ago 3 javascript modal-dialog vue.js
在我的 vue.js 应用程序中,我需要在模态中显示自定义数据。我正在使用vue-js-modal能够从我的应用程序内的任何位置显示模态:https : //www.npmjs.com/package/vue-js-modal
我创建了一个非常简单的自定义模态模板来试用它,名为ItemModal.vue:
<template>
<modal name="item-modal">
<b>{{item.name}}</b>
</modal>
</template>
<script>
export default {
name: 'item-modal',
props: [ 'item' ]
}
</script>
Run Code Online (Sandbox Code Playgroud)
我能够从我的ItemList.vue组件内部成功调用它:
<template>
<div id="content">
<item-modal></item-modal>
<li v-for="item in items">
<a v-on:click="showModal( item )">
<span>{{item.name}}</span>
</a>
</li>
</div>
</template>
<script>
import ItemModal from './ItemModal.vue';
import Items from '@/api/items';
export default {
name: 'items',
asyncComputed: {
items: {
async get() {
const items = await Items.getAll();
return items.data;
},
default: []
}
},
methods: {
showModal( item ) {
this.$modal.show('item-modal', { item: item } );
}
},
components: {
ItemModal
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
但是,模态是空的。
我错过了什么ItemModal.vue才能使用我在showModal函数中传递给它的数据?
您应该在项目的 main.js 文件中注册插件,如下所示:
import VModal from 'vue-js-modal'
Vue.use(VModal)
Run Code Online (Sandbox Code Playgroud)
然后你就可以在项目的任何地方调用 show 函数:
this.$modal.show('the-name-you-asigned-in-your-modal');
Run Code Online (Sandbox Code Playgroud)
如果您需要将参数传递给模态,您可以在 beforeOpen 事件处理程序中轻松接收它们:
//In the template
<modal name="hello-world" @before-open="beforeOpen"/>
methods: {
beforeOpen (event) {
console.log(event.params.foo);
}
}
Run Code Online (Sandbox Code Playgroud)
我知道你很接近让它工作,所以我为你提供了一个工作示例,以便你可以将其作为参考:
1- 在 main.js 文件中包含插件。
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import VModal from 'vue-js-modal'
Vue.config.productionTip = false
Vue.use(VModal)
/* eslint-disable no-new */
new Vue({
el: '#app',
template: '<App/>',
components: { App }
})
Run Code Online (Sandbox Code Playgroud)
2- 创建一个包含模态的组件(我们称之为Modal.vue)
<template>
<modal name="hello-world" @before-open="beforeOpen">
<div class="wrapper">
<p>{{ itemToShow }}</p>
</div>
</modal>
</template>
<script>
export default {
name: 'HelloWorld',
data: function () {
return {
item: ''
}
},
computed: {
itemToShow: function () {
return this.item
}
},
methods: {
beforeOpen (event) {
this.item = event.params.item;
}
}
}
</script>
<style scoped>
.wrapper {
height: 100%;
width: 100%;
background-color: black;
}
</style>
Run Code Online (Sandbox Code Playgroud)
3- 显示模态组件
<template>
<div id="app">
<img src="./assets/logo.png">
<Modal />
<button @click="onClick">
CLICK HERE!
</button>
</div>
</template>
<script>
import Modal from './components/Modal'
export default {
name: 'app',
components: {
Modal: Modal
},
methods: {
onClick() {
this.$modal.show('hello-world', {item: 'Hello world'});
}
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10063 次 |
| 最近记录: |