Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【vue】为什么在初始化阶段才去进行合并处理 #57

Open
yayxs opened this issue Apr 7, 2021 · 0 comments
Open

【vue】为什么在初始化阶段才去进行合并处理 #57

yayxs opened this issue Apr 7, 2021 · 0 comments

Comments

@yayxs
Copy link
Owner

yayxs commented Apr 7, 2021

const Child = {
  template: '<span></span>',
  data () {
    return {
      childData: this.parentData
    }
  },
  props: ['parentData'],
  created () {
    // 这里将输出 parent
    console.log(this.childData)
  }
}

var vm = new Vue({
    el: '#app',
    // 通过 props 向子组件传递数据
    template: '<child parent-data="parent" />',
    components: {
      Child
    }
})

因为 inject 和 props 这两个选项的初始化是先于 data 选项的,这就保证了我们能够使用 props 初始化 data 中的数据

这样就保证了我们在 data 中使用props 中的值

由于 props 的初始化先于 data 选项的初始化
2、data 选项是在初始化的时候才求值的,你也可以理解为在初始化的时候才使用 mergeData 进行数据合并

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant