refs里面调用即可,减少了dom节点获取的消耗
<div id="app">
<input type="text" ref="input1"/>
<button @click="add">添加</button>
</div>
new Vue({
el: "#app",
methods:{
add(){
this.$refs.input1.value ="996";
}
}
})
2、父组件可以调用子组件中得方法 子组件:
<template>
<div></div>
</template>
<script>
export default {
methods:{
childMethod(flag) {
console.log(flag)
}
},
created(){
}
}
</script>
父组件:在子组件加上ref即可通过this.$refs.ref.methods调用方法
<template>
<div @click="parentMethod">
<children ref="child1"></children>
</div>
</template>
<script>
import children from 'components/children/children.vue'
export default {
data(){
return {
flag: true
}
},
components: {
'children': children
},
methods:{
parentMethod() {
console.log(this.$refs.child1) //返回的是一个vue对象,所以可以直接调用其方法
this.$refs.child1.childMethod(this.flag);
}
}
}
</script>