JSON.parse()
解析JSON字符串并返回对应的值,可以额外传入一个转换函数,用来将生成的值和其属性, 在返回之前进行某些修改。
JSON.stringify()
返回与指定值对应的JSON字符串,可以通过额外的参数, 控制仅包含某些属性, 或者以自定义方法来替换某些key对应的属性值。
class组合:
定义Object类型:
type: Object,
required: true
url中文转义encodeURI
// 返回默认图片地址
isDefaultImage(url) {
// 1.此处url可能包含中文,需要编码,否则ios无法识别
// 2.local://xxx.png为原生ios端本地图片路径
return url !== undefined ? encodeURI(url) : 'local://logo.png'
}
页面间数据传递:
native -> weex:
可以在native端调用render时传入的option中自定义字段,
例如NSDictary *option = @{@"params": @{}}, 在weex中使用weex.config.params取出数据
weex -> weex: 使用storage
weex -> native: 使用自定义module
/**
从weex页面跳转到原生页面
@param vcName 原生页面的字符串类名,如果这个类有vc的类方法会按此方法生成类,如果没有则直接alloc init生成类,所以这里还是建议大家用类方法vc包装下自己的页面的生成方法,或者手码生成
@param param 传给vcName控制器的字典,运行时遍历字典赋值给控制器相应的属性
*/
- (void)pushNative:(NSString *)vcName param:(NSDictionary *)param {
Class vcClass = NSClassFromString(vcName);
if (vcClass == nil) {
return;
}
UIViewController *vc = [[vcClass alloc] init];
SEL vcSEL = NSSelectorFromString(@"vc");
if ([NSClassFromString(vcName) respondsToSelector:vcSEL]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
vc = [NSClassFromString(vcName) performSelector:vcSEL];
#pragma clang diagnostic pop
}
unsigned int count = 0;
objc_property_t *plist = class_copyPropertyList(vcClass, &count);
for (int i = 0; i
动态宽度
:style="{ width: twoLineTextWidth + 'px' }"
获取网络请求后,值给控件,然后拿控件的size
1、引用控件
{{data.sellerPhone}}
2、导入dom
const dom = weex.requireModule('dom')
3、变更size
/// 生命周期方法,更新成功后,网络请求数据变更新,控件双向绑定,界面刷新成功后会自动走此方法,在这里获取size。
updated:function () {
const result = dom.getComponentRect(this.$refs.phoneText, option => {
var screenWidth = 375*2;
var imageWith = 34;
var imageLeftMargin = 32;
var imageRightMargin = 40;
var phoneLeftRightMargin = 30;
this.twoLineTextWidth = screenWidth - (imageWith + imageLeftMargin + imageRightMargin + phoneLeftRightMargin*2 + option.size.width);
})
}
VSCode里的.vue文件模板