We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
如果子组件的data不是方法的话,每个组件返回相同的一份数据,那么多个子组件的数据就会冲突。所以子组件的data方法必须是一个function,每个子组件返回独立的数据。
<h1>子组件data方法测试</h1> <div id="counter-event-example"> <button-counter></button-counter> <button-counter></button-counter> </div> <script> Xiao.component('button-counter', { template: '<button @click="incrementCounter">{{ counter }}</button>', data: function () { return { counter: 0 } }, methods: { incrementCounter: function () { console.log('incrementCounter') this.counter += 1 } }, }) new Xiao({ el: '#counter-event-example', }) </script>
判断data是不是方法,如果是方法,把调用后的值赋值为data即可。调用的时候会绑定this为当前实例。
/** * 监听data * @param {*} vm */ function initData(vm: Xiao) { let data = vm.$options.data if (!data) { //observe(vm._data = {}, true /* asRootData */) } data = vm._data = typeof data === 'function' ? getData(data, vm) : data || {} // proxy data on instance const keys = Object.keys(data) const props = vm.$options.props const methods = vm.$options.methods let i = keys.length while (i--) { const key = keys[i] if (!isReserved(key)) { // 这里才是真正的代理数据 proxy(vm, `_data`, key) } } // observe data observe(data, true /* asRootData */) } export function getData(data: Function, vm: Xiao): any { try { return data.call(vm, vm) } catch (e) { warn("get data error:", e); return {} } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原因
如果子组件的data不是方法的话,每个组件返回相同的一份数据,那么多个子组件的数据就会冲突。所以子组件的data方法必须是一个function,每个子组件返回独立的数据。
测试代码
实现
判断data是不是方法,如果是方法,把调用后的值赋值为data即可。调用的时候会绑定this为当前实例。
The text was updated successfully, but these errors were encountered: