From bcc2b1fe3bbf681c4fb79c2c37b77497daf9b3fb Mon Sep 17 00:00:00 2001 From: guy Date: Mon, 12 Oct 2020 18:06:00 +0800 Subject: [PATCH] =?UTF-8?q?context=E9=87=8D=E6=9E=84,=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?inject=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/fix/fix.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/dist/fix/fix.js b/dist/fix/fix.js index 539a39d14..486b935d5 100644 --- a/dist/fix/fix.js +++ b/dist/fix/fix.js @@ -1332,6 +1332,25 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons vm.$$context = createViewModel$1({}, props); } + function getInjectValue(vm, key) { + var p = vm._parent; + while (p) { + if (p.$$context && key in p.$$context) { + return p.$$context[key]; + } + p = p._parent; + } + } + + function getInjectValues(vm) { + var inject = vm.inject || []; + var result = {}; + _.each(inject, function (key) { + result[key] = getInjectValue(vm, key); + }); + return result; + } + var Model = function () { function Model() { _classCallCheck(this, Model); @@ -1350,17 +1369,18 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons var state = _.isFunction(this.state) ? this.state() : this.state; var computed = this.computed; var context = this.context; + var inject = this.inject; var childContext = this.childContext; var watch$$1 = this.watch; var actions = this.actions; - var keys = _.keys(this.$$model).concat(_.keys(state)).concat(_.keys(computed)).concat(context || []); + var keys = _.keys(this.$$model).concat(_.keys(state)).concat(_.keys(computed)).concat(inject || []).concat(context || []); var mixins = this.mixins; defineProps(this, keys); childContext && defineContext(this, childContext); this.$$model && (this.model.__ob__ = this.$$model.__ob__); initMixins(this, mixins); this.init(); - initState(this, state); + initState(this, _.extend(getInjectValues(this), state)); initComputed(this, computed); initWatch(this, watch$$1); initMethods(this, actions);