【问题标题】:How to pass a dynamic variable to a 'component' and then display it - VueJS如何将动态变量传递给“组件”然后显示它 - VueJS
【发布时间】:2018-02-16 15:58:39
【问题描述】:

我无法在我的component 中显示变量(这是图像的链接)我迷路了...

(为了帮助你,我在一张桌子上点击打开 pdf 的图标)

谢谢!

Vue.component('modal', {
    template: '#modal-template-pdf',
    props: {
        lien: String
    }
});

let appCompta = new Vue({
        data: {
            showModalActive: false,
            currentModalPdfLink: '',
        }
}).$mount('#'+id+' #app-compta')
    
    
    
<img slot="apercu" slot-scope="props" class="iconeged main" src="images/ico_pdf.png"
             v-bind:currentModalPdfLink='props.row.imglien'
             @click="showModalActive = true"
        >

<modal v-if="showModalActive" @close="showModalActive = false" v-bind:lien="currentModalPdfLink">
        <h3 slot="header">Aperçu PDF</h3>
    </modal>

    <script type="text/x-template" id="modal-template-pdf">
        <transition name="modal">
            <div class="modal-mask">
                <div class="modal-wrapper">
                    <div class="modal-container">

                        <div class="modal-header">
                            <slot name="header">
                                default header
                            </slot>
                        </div>

                        <div class="modal-body">
                            <slot name="body">
                                {{currentModalPdfLink}}
                                <iframe height='100%' width='100%' frameborder='0' :src="currentModalPdfLink"/>
                            </slot>
                        </div>

                        <div class="modal-footer">
                            <slot name="footer">
                                <button class="modal-default-button" @click="$emit('close')">
                                    FERMER
                                </button>
                            </slot>
                        </div>
                    </div>
                </div>
            </div>
        </transition>
    </script>

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-component


    【解决方案1】:

    我认为currentModalPdfLink 是您的 pdf 链接? 您传递的道具的名称是src,因此您需要在组件中使用相同的名称引用它:

    Vue.component('modal', {
        template: '#modal-template-pdf',
        props: ['src']
    });
    

    【讨论】:

    • 我有以下错误:“属性或方法“currentModalPdfLink”未在实例上定义,但在渲染期间被引用。确保此属性是反应性的,无论是在数据选项,或基于类的组件,通过初始化属性。” 我将您在组件中告诉我的内容和模态的第一行替换为:` v-bind:src="currentModalPdfLink"`
    • 问题是,我看不到你的 Vue-App-structure。您将 vue-instance 绑定到哪个元素?你使用 Vue-devtools (chrome.google.com/webstore/detail/vuejs-devtools/…) 吗?它将帮助您查看所有 vue 数据和道具。
    • 您好,感谢您的回答,对我的延误表示歉意。我提供给 VueJS 的数据不使用 Node,而是使用 php 和 axios。 (所以我也没有扩展名)。我真的不知道该给你更多什么......我正在尝试使用“props”来传递一个变量,但我只找到具有多个变量或表的案例。
    猜你喜欢
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 2021-04-03
    • 1970-01-01
    相关资源
    最近更新 更多