Vue开发:组件之间的数据传递

2018-10-15  本文已影响51人  何小鹏

问题:兄弟组件之间的通讯(EvenBus的通信方式)
解决方式:

//Bus引用
import Vue from "vue"
export default new Vue();
 //通过emit触发事件,并传值
 bus.$emit("eventTarget","传值");
//emit里面的值可以为字符串、数组、对象
  //通过on监听事件,回调方法获取数据
  bus.$on("eventTarget",v=>{
     console.log(v);
 });

问题:Vue生命周期的理解
解决方式:

1741752219-59c9b774a4ccf_articlex.png 3346068135-580822cd52898_articlex.png

问题: vue中的 ref 和 $refs
解决方案:

ref 被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs 对象上。
父组件

//html部分
<template>
 <div class="analysis-content">
      <EditAttribute :AttributeData="AttributeData" ref="formIndexAttribute"></EditAttribute>
</div> 
</template>
//js部分
<script>
methods: {
    edit() {
      this.$refs.formIndexAttribute.handleSubmit();
    }, 
  }
</script>

子组件

  <Form ref="formValidate" :label-width="100" class="indexForm"> 
              <FormItem v-if="item.fillInType === 0" :label="item.name" :prop="item.name">
                <Input :disabled="true" :value="item.value" @on-change="saveInputData($event,index)"></Input>
              </FormItem>
              <FormItem v-if="item.fillInType === 1" :label="item.name" :prop="item.name" :required="item.required == 1">
                <Input type="textarea" :value="item.value" @on-blur="saveInputData($event,index)"></Input>
              </FormItem> 
 </Form>
<script>
  handleSubmit() {}
</script>

主要解决的情况是:点击父组件里面的按钮获取到子组件form表单里的所有值。

问题:props和data都做同一个变量声明,导致报错

The data property "AttributeData" is already declared as a prop. Use prop default value instead.

解决方案:一般父子组件传值只需要在props中声明即可,data声明的是当前组件使用的变量。

问题:Git用户权限错误
解决方案:修改config文件,在请求地址前面加上用户名:用户密码@请求地址

上一篇 下一篇

猜你喜欢

热点阅读