TypechoJoeTheme

香草物语

统计
登录
用户名
密码
/
注册
用户名
邮箱
输入密码
确认密码

Vue子组件的索引

Laughing博主
2021-02-21
/
0 评论
/
982 阅读
/
95 个字
/
百度已收录
02/21
本文最后更新于2022年11月20日,已超过669天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!

在Vue中,当子组件较多时,通过this.$children来一一遍历我们需要的一个组件是比较困难的,尤其是组件动态渲染时,他们的序列是不固定的。Vue提供了子组件索引的方法,用特殊的属性ref为子组件指定一个索引的名字。

<html>

<head>
    <title>Vue学习</title>
    <script src="vue.js"></script>
</head>

<body>

    <div id="app">

        <component-a ref="compA"></component-a>

        <button @click="handleMessage">获取组件A的内容</button>

        {{message}}

    </div>

    <script type="text/javascript">

        Vue.component("component-a", {
            data: function () {
                return {
                    message: '这是组件A的message'
                }
            },
            template:'<div></div>'
        })

        var app = new Vue({
            el: '#app',
            data: {
                message: ''
            },
            methods: {
                handleMessage: function () {
                    this.message = this.$refs.compA.message
                }
            }
        })
    </script>
</body>

</html>
朗读
赞(1)
赞赏
感谢您的支持,我会继续努力哒!
版权属于:

香草物语

本文链接:

https://www.xiangcaowuyu.net/web/index-of-vue-subcomponents.html(转载时请注明本文出处及文章链接)

评论 (0)
  1. newlw 闲逛
    Windows 10 · Google Chrome

    看密码

    2018-10-15 回复