Parent getting data in Child

by Irene Tang

HTML

<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@1.4.1/lib/index.js"></script>
<script id="child" type="x-template">
  <el-dialog v-model="visible"
    title="子组件">
    <div>Content</div>
    <div slot="footer">
      <el-button @click="close">取消</el-button>
    </div>
  </el-dialog>
</script>

<div id="parent">
  <el-button @click="openDialog">打开弹框</el-button>
  <child ref="child"></child>
</div>

CSS

@import url("//unpkg.com/element-ui@1.4.1/lib/theme-default/index.css");

JavaScript

var Child = {
  template: '#child',
  data() {
    return {
      visible: false
    }
  },
  methods: {
    open () {
      this.visible = true
    },
    close () {
      this.visible = false
    }
  }
}
var Parent = {
  components: {
    Child
  },
  methods: {
    openDialog () {
      this.$refs.child.visible = true
    }
  }
}
var Ctor = Vue.extend(Parent)
new Ctor().$mount('#parent')