-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
state 中的数据不能有小数点 #968
Comments
测试了其它符号,都可以,只有点不行,在小程序里用原生也没问题,好像是在转换后的问题 <block>
<view>{{anonymousState__temp}}
<view class="item" wx:key="{{index}}" wx:for="{{loopArray0}}"
wx:for-item="key" wx:for-index="index">
<view class="title">{{key.$original}}</view>
<view class="skus">
<view>{{key.$original}}</view>
<view wx:key="{{idx}}" class="{{'sku ' + (key.skuItems[item].active ? 'active' : '') + ' ' + (key.skuItems[item].disabled ? 'disabled' : '')}}"
wx:for="{{key.skuItemKeys}}" wx:for-item="item" wx:for-index="idx">
{{item}}
{{key['skuItems']['45'].active}}
</view>
</view>
</view>
</view>
</block> 小程序原生写法正常 <!--index.wxml-->
<view class="container">
<view wx:for="{{skuKeys}}" wx:for-index="index" wx:for-item="key" class='item'>
{{index}}
<view class='skus'>
<view wx:for="{{key}}" wx:for-index="idx" wx:for-item="item"
class="{{'sku ' + (item.active ? 'active' : '')}}">
{{item.name}} {{item.active}}
</view>
</view>
</view>
</view> //index.js
const app = getApp()
Page({
data: {
skuKeys: {
'颜色': {
'红': { active: false, disabled: false, name: '红' },
'黄': { active: false, disabled: false, name: '黄' }
},
'大小': {
'5.5': { active: false, disabled: false, name: '5.5' },
'45': { active: false, disabled: false, name: '45' }
}
},
skuDefault: ['红', '5.5'],
selectedSkus: {},
skuSpecs: {},
variants: {},
selectedItem: {}
},
setDefault() {
const { skuKeys, skuDefault } = this.data
let data = {}
for (let i in skuKeys) {
data[i] = skuKeys[i]
for (let j in data[i]) {
if (skuDefault.includes(j)) {
data[i][j].active = true
}
}
}
this.setData({
title: 'deng',
skuKeys: data
})
console.log(this.data.skuKeys)
},
onLoad: function() {
this.setDefault()
console.log('ok');
}
}) |
#886 你在循环中不要从 this.state 中拿数据,先给他起个名字不久可以了吗 |
更新了最新版,#912 这里提到的问题解决了,同样#886的问题可以先设置个名字再放进循环了。主要是这个key里有.不正确的问题了。 |
@yuche 请问下这个问题现在有什么办法吗?因为项目里好多这种类型的,已经写了好几个原生组件了,主要是像4.7这类带小数点的object的key传给props时,被分解了。 |
@yuche 我把taro生成的loopArray0和wxml用原生的写了一次,是可以正常的 Page({
data: {
loopArray0: [{ "skuItems": { "红": { "active": true, "disabled": false, "name": "红" }, "黄": { "active": false, "disabled": false, "name": "黄" } }, "skuItemKeys": ["红", "黄"], "$original": "颜色" }, { "skuItems": { "45": { "active": false, "disabled": false, "name": "45" }, "5.5": { "active": true, "disabled": false, "name": "5.5" } }, "skuItemKeys": ["45", "5.5"], "$original": "大小" }]
},
onShow() {
console.log(this.data.loopArray0)
}
}) 下面是直接用的taro生成的wxml <block>
<view>
<view class="item" wx:key="{{index}}" wx:for="{{loopArray0}}" wx:for-item="key"
wx:for-index="index">
<view class="title">{{key.$original}}</view>
<view class="skus">
<view wx:key="{{idx}}" class="{{'sku ' + (key.skuItems[item].active ? 'active' : '') + ' ' + (key.skuItems[item].disabled ? 'disabled' : '')}}"
wx:for="{{key.skuItemKeys}}" wx:for-item="item" wx:for-index="idx">{{key.skuItems[item].active}}{{item}}</view>
</view>
</view>
</view>
</block> |
这个是用tarojs写的生成的js,用 可以看出,taro createComponent在数据生成完成之前,不过具体为什么不带小数点就没问题我也不清楚了,而且如我上面这里#968 (comment) 写的,我从api得到数据,然后prop一个product的object过去,带小数点的key会被分解了。所以应该不是微信本身的问题 "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _index = require("../../npm/@tarojs/taro-weapp/index.js");
var _index2 = _interopRequireDefault(_index);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Demo = function (_BaseComponent) {
_inherits(Demo, _BaseComponent);
function Demo() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Demo);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Demo.__proto__ || Object.getPrototypeOf(Demo)).call.apply(_ref, [this].concat(args))), _this), _this.$usedState = ["loopArray0", "$anonymousCallee__3", "skuKeys", "skuDefault", "selectedSkus", "skuSpecs", "variants", "selectedItem"], _this.state = {
skuKeys: {
'颜色': {
'红': { active: false, disabled: false, name: '红' },
'黄': { active: false, disabled: false, name: '黄' }
},
'大小': {
'5.5': { active: false, disabled: false, name: '5.5' },
'45': { active: false, disabled: false, name: '45' }
}
},
skuDefault: ['红', '5.5'],
selectedSkus: {},
skuSpecs: {},
variants: {},
selectedItem: {}
}, _this.$$refs = [], _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Demo, [{
key: "_constructor",
value: function _constructor(props) {
_get(Demo.prototype.__proto__ || Object.getPrototypeOf(Demo.prototype), "_constructor", this).call(this, props);
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
this.setDefault(this.state.skuKeys, this.state.skuDefault);
}
}, {
key: "setDefault",
value: function setDefault(ks, defaultsku) {
var _state = this.state,
skuKeys = _state.skuKeys,
skuDefault = _state.skuDefault;
var data = {};
for (var i in skuKeys) {
data[i] = skuKeys[i];
for (var j in data[i]) {
if (skuDefault.includes(j)) {
data[i][j].active = true;
}
}
}
this.setState({
skuKeys: data
});
}
}, {
key: "_createData",
value: function _createData() {
this.__state = arguments[0] || this.state || {};
this.__props = arguments[1] || this.props || {};
;
var skuKeys = this.__state.skuKeys;
var $anonymousCallee__3 = Object.keys(skuKeys);
var loopArray0 = Object.keys(skuKeys).map(function (key, index) {
key = {
$original: (0, _index.internal_get_original)(key)
};
var skuItems = skuKeys[key.$original];
var skuItemKeys = Object.keys(skuItems);
return {
skuItems: skuItems,
skuItemKeys: skuItemKeys,
$original: key.$original
};
});
Object.assign(this.__state, {
loopArray0: loopArray0,
$anonymousCallee__3: $anonymousCallee__3
});
console.log('得到的state', this.__state)
return this.__state;
}
}]);
return Demo;
}(_index.Component);
Demo.properties = {};
Demo.$$events = [];
exports.default = Demo;
const com = require('../../npm/@tarojs/taro-weapp/index.js').default.createComponent(Demo, true)
console.log('taro createComponent后生成的', com)
Component(com); |
原因在于 Taro 的 setState 会帮你自动 diff 数据,然后用路径的形式最小化更新。形如 你可以暂时用 |
谢谢,原来如此,我现在有小数点的组件都用原生写了,后面用 this.$scope.setData 试下,这个问题还有希望处理么? |
我们会和微信小程序开发组沟通尝试去解决这个问题 |
好的,谢谢 |
@yuche 你好,请问这个现在可以了? |
问题描述
想要实现打开页面,skuKeys根据skuDefault来设置选中项,如果选中,active 为true,加上class名为active。在skuKeys中,比如大小一项中,如果有如5.5这样带小数点的项,那么className={
sku ${skuItems[item].active ? 'active' : '' } ${skuItems[item].disabled ? 'disabled' : '' }
} 这句就无效了,只要改成没有小数点的名称,就没问题复现步骤
[复现问题的步骤]
[或者可以直接贴源代码,能贴文字就不要截图]
期望行为
进入页面,skuDefault中的值默认选中,className加上active,比如 className='sku active'
报错信息
[这里请贴上你的完整报错截图或文字]

系统信息
补充信息
[可选]
[根据你的调查研究,出现这个问题的原因可能在哪里?]
The text was updated successfully, but these errors were encountered: