※前提条件:本情報はVue 2.0を基づいて説明してる
在vue中ref可以以属性的形式添加给标签或者组件
ref 写在标签上时:this.$refs.ipt 获取的是添加了ref="ipt"标签对应的dom元素
<template> <input type="text" ref="ipt"/> </template>
methods:{ confirm(){ console.log(this.$refs.ipt.value) //打印出输入框中的value值 } }
下面例子就是取得form的dom元素
this.$refs.form.validate((valid) => { if (valid) { this.dialog = false; } else { return false; } });
ref 写在组件上时:this.$refs['component'] 获取到的是添加了ref="component"属性的这个组件
<template> <comp-detail ref="component"></list-detail> </template>
methods:{ confirm(){ this.$refs['component'].init() //调用组件comp-detail中的init()方法 } }
コメント: