vue2.0

公共子组件(父传子)

2019-11-25  本文已影响0人  zlf_j

一、代码示例

父组件(弹框为例)

 <feedbackDailies ref="feedbackDailies" :nowData="dailies_history[dailiesPage]"></feedbackDailies>
import feedbackDailies from '../../../Common/detail.vue' // 文件路径自己定
methods: {
   dailiesPicture() {
        this.$refs.feedbackDailies.open()
   },
}
components: {
      feedbackDailies
    },

子组件

<div v-show="dialogFormVisible">
  {{currentData}}
</div>
 data () {
   return: {
      dialogFormVisible: false, // 控制弹框显隐
      currentData: {}
   }
 },
 methods: {
   open() {
       this.dialogFormVisible = true
   },
 },
 watch: {
       nowData: {
            handler (val, oldVal) {
              if (val) {
                this.currentData = val
              } else {
              }
            },
            deep: true,
            immediate: true
          },
 }

二、了解

// 获取值
console.log(this.$refs.addRef.ruleForm)
// 调用方法
this.$ref.addRef.submitRuleform()

https://www.cnblogs.com/luguankun/p/11700132.html

子组件调用父组件方法(this.$parent.event)
methods: {
      fatherMethod() {
        console.log('测试');
      }
}
this.$parent.fatherMethod();

https://www.cnblogs.com/jin-zhe/p/9523782.html

三、v-model传参

父组件

<tinymce :height="600" v-model="item.html" />

子组件

* 值(默认值是value)
 props: {
  value: {
    type: String,
    default: ""
   }
 }
* 传参(默认方法是input)
this.$emit("input", 内容);
* 值(默认值是modelValue)
 props: {
  modelValue: {
     type: String,
     default: ""
  }
}
* 传参(默认方法是update)
 this.$emit("update:modelValue", 内容);

https://www.jianshu.com/p/854fc40cf0b4

上一篇 下一篇

猜你喜欢

热点阅读