Vue use js const and Vue Reactivity All In One
Vue use js const and Vue Reactivity All In One
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<section>
<div v-if="creativeLimit > 0">vue use js const ok ✅</div>
<div v-else>vue use js const bug ❌❌❌</div>
</section>
<!-- <div v-if="isOverLimit">vue use js const bug ❌❌</div> -->
<!-- <div>creativeLimit = {{ creativeLimit }}❌</div> -->
<div>getCreativeLimit = {{ getCreativeLimit }}✅</div>
<section>
<div v-if="isOverLimit">
vue use js const with computed property ok ✅
</div>
<div v-else>vue use js const bug ❌</div>
</section>
<section>
<div>constValue = {{ constValue }}</div>
<div>obj.constValueReactive = {{ obj.constValueReactive }}</div>
</section>
</div>
</template>
<script>
const creativeLimit = 15;
export default {
name: "HelloWorld",
props: {
msg: String,
},
data() {
return {
creativeMaxNums: 15,
obj: {},
arr: [],
};
},
computed: {
// 不能直接使用 const, 定义的变量,必须放到 computed 或 data 或 methods 里面间接使用;??? 因为不是 data 初始化里面的,就不是响应式的
// 那 TM 还不如直接使用 computed 属性
getCreativeLimit() {
return creativeLimit;
},
},
mounted() {
// const
console.log("creativeLimit =", creativeLimit);
// data const ??? 性能问题 (vue 会使用 defineProperty 进行监听)
console.log("❌ this.creativeMaxNums =", this.creativeMaxNums);
// computed const
console.log("✅ this.getCreativeLimit =", this.getCreativeLimit);
// data const ??? (非响应式)
this.constValue = 2021;
console.log("✅ this.constValue =", this.constValue);
// data const ??? (响应式)
// this.$set(this.$data, "constValueReactive", 2021);
// this.$set(this.constValueReactive, "constValueReactive", 2021);
// console.log("✅ 响应式 this.constValueReactive =", this.constValueReactive);
this.$set(this.obj, "constValueReactive", 2021);
console.log(
"✅ 响应式 this.obj.constValueReactive =",
this.obj.constValueReactive
);
console.log("\n old this.$data =", this.$data);
setTimeout(() => {
// console.log("\n this =", this);
console.log("\n new this.$data =", this.$data);
// const
console.log("creativeLimit =", creativeLimit);
// data const ??? 性能问题 (vue 会使用 defineProperty 进行监听)
console.log("❌ this.creativeMaxNums =", this.creativeMaxNums);
// computed const
console.log("✅ this.getCreativeLimit =", this.getCreativeLimit);
// data const ??? (非响应式)
console.log("✅ this.constValue =", this.constValue);
// data const ??? (响应式)
// console.log(
// "✅ 响应式 this.constValueReactive =",
// this.constValueReactive
// );
console.log(
"✅ 响应式 this.obj.constValueReactive =",
this.obj.constValueReactive
);
}, 3000);
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
margin: 40px 0 0;
}
</style>
utils
// vue-use-js-const-and-vue-reactivity-all-in-one => Vue use js const and Vue Reactivity All In One
// 驼峰命名,自动转换
refs
https://codesandbox.io/s/vue-use-js-const-and-vue-reactivity-all-in-one-1k98b
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/14857379.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2020-06-07 ShadowsocksX-NG-R8 & 设置教程
2020-06-07 新三板 & 挂牌费用
2019-06-07 ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await
2018-06-07 JavaScript 设计模式: 发布者-订阅者模式
2018-06-07 how to write a front-end framework by using the vanilla JavaScript?
2018-06-07 front end & backend
2016-06-07 sphinx 文档生成器 (基于 python )