vue ref父子組件傳值

一. ref使用在父組件上

父組件html:javascript

  <information ref='information'></information>html

  import information from './information'java

  components:{information,bill,means},dom

在父組件上使用子組件的值,js :this.$refs.information.isAdd;   isAdd是information組件的data的屬性。ide

二.ref使用在元素上

例如本組件html:ui

<span ref="myspan" class="redmy">23232</span>

redmy本組件js使用:this.$refs["myspan"].className //
<span class="redmy">23232</span>this.$refs["myspan"] 指代對象//

三.ref使用在子組件上

子組件上有this

<h5 ref='insideDomRef'>我是子組件</h5>

父組件上能夠引用子組件的值:this.$refs.insideDomRef// <h5 >我是子組件</h5>





實例

這是父組件:
複製代碼
<template><div>
  <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" 
      label-width="100px" class="demo-dynamic">

  <el-form-item prop="email" label="郵箱" :rules="[{required: true,message:'請輸入郵箱地址', trigger:'blur' },{type:'email',message:'請輸入正確的郵箱地址',trigger:'blur,change'}]">
    <el-input v-model="dynamicValidateForm.email" ref="myemail"></el-input>
  </el-form-item>

  <el-form-item v-for="(domain, index) in dynamicValidateForm.domains" :label="'域名' + index"
      :key="domain.key" :prop="'domains.' + index + '.value'" :rules="{required: true, message: '域名不能爲空', trigger: 'blur'}">
      <el-input v-model="domain.value"></el-input>
      <el-button @click.prevent="removeDomain(domain)">刪除</el-button>
  </el-form-item>

  <span ref="myspan" class="redmy">23232</span>

  <el-form-item>
    <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
    <el-button @click="addDomain">新增域名</el-button>
    <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
  </el-form-item>

</el-form>

<childone ref="childonemyyy"></childone>
</div>
</template>
<script>
import childone from './childone'
  export default {
    components:{childone},
    data() {
      return {
        dynamicValidateForm: {
          domains: [{
            value: ''
          }],
          email: '',
          spanval:'',
        }
      };
    },
    methods: {
      submitForm(formName) {

        this.$refs["childonemyyy"].isAdd;//"mychildone"用在父組件上引用子組件值,返回子組件上的data數據
        this.$refs["myspan"].className  //redmy  用在元素上,返回元素節點對象

        this.$refs[formName].validate((valid) => {

          if (valid) {
            alert('submit!'+this.$refs[formName].email);
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      removeDomain(item) {
        var index = this.dynamicValidateForm.domains.indexOf(item)
        if (index !== -1) {
          this.dynamicValidateForm.domains.splice(index, 1)
        }
      },
      addDomain() {
        this.dynamicValidateForm.domains.push({
          value: '',
          key: Date.now()
        });
      }
    }
  }
</script>
複製代碼

這是子組件:spa

複製代碼
<template><div>
    <p class="ppp">我是p段落,我是子組件一</p>
    <el-button @click="submit">子組件</el-button>
    </div>
</template>
<script type="text/javascript">
     export default {
         data(){
             return{
                 isAdd:"mychildone",
             }
         },
         methods:{
             submit(){
                 
} }, created(){ } } </script>
複製代碼
相關文章
相關標籤/搜索