|
@@ -1,50 +1,26 @@
|
|
|
<template>
|
|
|
- <h2> toRaw 与 markRaw</h2>
|
|
|
- <h3>state: {{ state }}</h3>
|
|
|
- <hr>
|
|
|
- <button @click="testToRaw">测试toRaw</button>
|
|
|
- <button @click="testMarkRow">测试markRaw</button>
|
|
|
+ <h2>响应式数据的判断</h2>
|
|
|
</template>
|
|
|
|
|
|
<script lang="ts">
|
|
|
-import { defineComponent, markRaw, reactive, toRaw } from 'vue'
|
|
|
-interface userInfo{
|
|
|
- name: string,
|
|
|
- age: number,
|
|
|
- likes?: string[]
|
|
|
-}
|
|
|
+import { defineComponent ,ref,isRef, isReactive, reactive, isReadonly, readonly, isProxy} from 'vue'
|
|
|
+
|
|
|
export default defineComponent({
|
|
|
+ //isRef
|
|
|
+ //isReactive
|
|
|
+ //isReadonly
|
|
|
+ //isProxy
|
|
|
setup () {
|
|
|
- const state = reactive<userInfo>({
|
|
|
- name:'zs',
|
|
|
- age: 18
|
|
|
- })
|
|
|
- const testToRaw = ()=>{
|
|
|
- //把代理对象编程普通对象 数据变化 页面不变化
|
|
|
- const user = toRaw(state)
|
|
|
- user.name += '=='
|
|
|
- console.log(111)
|
|
|
- }
|
|
|
- const testMarkRow = ()=>{
|
|
|
- // state.likes = ['吃','喝']
|
|
|
- // state.likes[0] += '='
|
|
|
- // console.log(state)
|
|
|
- const likes = ['吃','喝']
|
|
|
- state.likes = markRaw(likes)
|
|
|
- //markRaw标记独享的数据,从此以后都不能成为代理对象了
|
|
|
- setInterval(()=>{
|
|
|
- if(state.likes){
|
|
|
- state.likes[0] += '='
|
|
|
- console.log('定时器启动')
|
|
|
- }
|
|
|
- },1000)
|
|
|
- }
|
|
|
-
|
|
|
- return {
|
|
|
- state,
|
|
|
- testToRaw,
|
|
|
- testMarkRow
|
|
|
- }
|
|
|
+ //isRef: 检查一个值是否为ref对象
|
|
|
+ console.log(isRef(ref({})))
|
|
|
+ //isReactive检查一个对象是否是reactive创建的响应式代理
|
|
|
+ console.log(isReactive(reactive({})))
|
|
|
+ //isReadonly 检查一个对象是否是由readonly创建的只读代理
|
|
|
+ console.log(isReadonly(readonly({})))
|
|
|
+ //isProxy 检查一个对象是否是由reactive或者readonly方法创建的代理
|
|
|
+ console.log(isProxy(readonly({})))
|
|
|
+ console.log(isProxy(reactive({})))
|
|
|
+ return {}
|
|
|
}
|
|
|
})
|
|
|
</script>
|