【问题标题】:2 identical file, how to build a template / component in Vue.js 22 相同的文件,如何在 Vue.js 中构建模板/组件 2
【发布时间】:2019-04-24 21:15:27
【问题描述】:

我正在使用 Vue.js 2.0,我在 2 个不同的文件中有完全相同的代码,唯一改变的是 ID_SPECIFIC_TO_THIS_BLOCK 所以我是初学者关于 Vue,我想知道是否有一种方法可以实现一种模板,我可以为我的 2 个文件重用

您可以在下面找到一个文件的完整代码:

<template>
    <div>
        <div class="container">
            <hp-row>
                <hp-column>
                    <component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
                </hp-column>
            </hp-row>
        </div>
    </div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
    name: 'AboutUsView',
    mixins: [ModularView],

    created () {
        this.fetch('blocks/ID_SPECIFIC_TO_THIS_BLOCK')
    },
}
</script>

【问题讨论】:

    标签: templates vue.js vuejs2 vue-component


    【解决方案1】:

    使用道具:

    export default {
        name: 'AboutUsView',
        mixins: [ModularView],
        props: ['ID_SPECIFIC_TO_THIS_BLOCK']
        created () {
            this.fetch(`blocks/${this.ID_SPECIFIC_TO_THIS_BLOCK}`)
        },
    }
    
    <about-us-view ID_SPECIFIC_TO_THIS_BLOCK="123"></about-us-view>
    <about-us-view ID_SPECIFIC_TO_THIS_BLOCK="789"></about-us-view>
    

    【讨论】:

    • 首先感谢您的回答,对不起,我不明白,您为什么在
    • 我没有在脚本中使用 html,而只是在您使用该组件时显示如何传递道具。
    • 对不起,我是初学者,您没有在回答中使用任何进一步的解释或资料,所以您可能做得对,但我不明白
    • @Ced 参考vuejs.org/v2/guide/components.html 并特别注意“使用 Props 将数据传递给子组件”部分。
    • 我可以提供一个工作示例吗?我试过了,但没有用,@Bhojendra Rauniyar 的代码对我来说不清楚
    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 2020-07-10
    • 2016-04-29
    • 2017-08-05
    • 2017-04-15
    • 2018-03-22
    • 2017-08-24
    • 2017-11-23
    相关资源
    最近更新 更多