2017-10-07 366 views
0

在我的vue.js应用程序中,我需要在模态中显示自定义数据。我使用vue-js-modal能够从我的应用程序内的任意位置显示一个模式:https://www.npmjs.com/package/vue-js-modal使用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> 

,我能够成功地把它从我的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> 

然而,模态出现空。

我在ItemModal.vue中错过了什么,能够使用我在showModal函数中传递给它的数据?

+0

建议你阅读了关于插槽。 https://vuejs.org/v2/guide/components.html#Content-Distribution-with-Slots – Bert

回答

0

你应该注册插件,在项目的main.js文件,以这样的方式

import VModal from 'vue-js-modal' 
Vue.use(VModal) 

然后你就可以调用显示功能在任何地方你的项目:

this.$modal.show('the-name-you-asigned-in-your-modal'); 

如果你需要传递PARAMS的模式,你可以很容易得到他们在beforeOpen事件处理程序:

//In the template  
<modal name="hello-world" @before-open="beforeOpen"/> 


methods: { 
    beforeOpen (event) { 
    console.log(event.params.foo); 
    } 
} 

我知道你非常接近它的工作,所以我提供给你一个工作示例,以便您可以将其作为参考:

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 } 
}) 

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> 

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> 
相关问题