怎么访问到子组件的实例或者子元素?
发布于 2021-01-11 17:02:51
关注者
0
被浏览
274
3 个回答
-
通过
this.$refs
-
写了完整的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