Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vue] 怎么访问到子组件的实例或者子元素? #451

Open
haizhilin2013 opened this issue Jun 22, 2019 · 5 comments
Open

[vue] 怎么访问到子组件的实例或者子元素? #451

haizhilin2013 opened this issue Jun 22, 2019 · 5 comments
Labels
vue vue

Comments

@haizhilin2013
Copy link
Collaborator

[vue] 怎么访问到子组件的实例或者子元素?

@haizhilin2013 haizhilin2013 added the vue vue label Jun 22, 2019
@maomao1996
Copy link

通过

this.$refs

@Panamer
Copy link

Panamer commented Jul 25, 2019

vm.$children

@xiaoliuer
Copy link

this.$children/this.$refs.xxx

@yangyangjizi
Copy link

在子组件标签上加 ref属性如ref="baseAlert",在父组件通过this.$refs.baseAlert.子组件方法名。

@pluscai
Copy link

pluscai commented Jul 1, 2020

写了完整的demo,你可看下

<div id="app">
    <base-input ref="usernameInput"></base-input>
</div>

<script>
    Vue.component("base-input", {
        template: "<input type='input' ref='input'>",
        methods: {
            popUp() {
                alert(11)
            },
            focus: function () {
                this.$refs.input.focus()
            }
        }
    });

    new Vue({
        el: "#app",
        data: {},
        mounted: function () {
            this.$refs.usernameInput.popUp();
            this.$refs.usernameInput.focus();
        }
    });
</script>

完整demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vue vue
Projects
None yet
Development

No branches or pull requests

6 participants