<template>
<view class="container">
<text>{{title}}</text>
<button type="default" @click="changeTitle1">改变标题内容按钮1</button>
<button type="default" @click="changeTitle2">改变标题内容按钮2</button>
</view>
</template>
<script>
export default{
data(){
return{
title : "这个是标题",
}
},
methods:{
changeTitle1(){
this.title = "改变标题1";
},
//可以发现下面这个执行了success方法,但是调用this赋值却无法改变内容
changeTitle2(){
uni.setStorage({
key: ‘storage_key‘,
data: ‘hello‘,
success: function () {
this.title = "改变标题2";
console.log(‘changeTitle2------success‘);
}
});
},
}
}
</script>
<style>
.container{
display: flex;
flex-flow: column;
}
</style>
//可以发现这样操作就可以解决作用域问题
changeTitle3(){
//赋值
var me = this;
uni.setStorage({
key: ‘storage_key‘,
data: ‘hello‘,
success: function () {
me.title = "改变标题3";
console.log(‘changeTitle2------success‘);
}
});
},
changeTitle4(){
uni.setStorage({
key: ‘storage_key‘,
data: ‘hello‘,
success:() => {
this.title = "改变标题4";
console.log(‘changeTitle2------success‘);
}
});
},
原文:https://www.cnblogs.com/yc211/p/12912372.html