Skip to content

Commit 81d9ec2

Browse files
committedOct 14, 2021
vue3中的toRaw和markRaw
1 parent 58878bc commit 81d9ec2

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
 

‎16_src_toRaw和markRaw/App.vue

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<template>
2+
<!--vue3的组件模版结构可以没有根标签-->
3+
<button @click="isShowDemo = !isShowDemo">{{ isShowDemo ? '隐藏' : '显示'}}</button>
4+
<Demo v-if="isShowDemo"/>
5+
</template>
6+
7+
<script>
8+
import Demo from "./components/Demo";
9+
import { ref } from "vue";
10+
export default {
11+
name: 'App',
12+
components: {Demo},
13+
setup(){
14+
let isShowDemo = ref(true);
15+
return {
16+
isShowDemo
17+
}
18+
}
19+
}
20+
</script>
21+
22+
<style>
23+
#app {
24+
font-family: Avenir, Helvetica, Arial, sans-serif;
25+
-webkit-font-smoothing: antialiased;
26+
-moz-osx-font-smoothing: grayscale;
27+
text-align: center;
28+
color: #2c3e50;
29+
margin-top: 60px;
30+
}
31+
</style>
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<template>
2+
<h2>当前求和为:{{ sum }}</h2>
3+
<button @click="sum++">sum+1</button>
4+
<hr/>
5+
<h2>姓名:{{ name }}</h2>
6+
<h2>年龄:{{ age }}</h2>
7+
<h2>薪资:{{ job.j1.salary }}K</h2>
8+
<h3 v-show="person.car">座驾信息:{{ person.car }}</h3>
9+
<button @click="name = name + '~'">修改姓名</button>
10+
<button @click="age++">增长年龄</button>
11+
<button @click="job.j1.salary++">增长薪资</button>
12+
<button @click="showRawPerson">输出最原始的person</button>
13+
<button @click="addCar">给人添加一台车</button>
14+
<button @click="person.car && (person.car.name +='!') ">换车名</button>
15+
<button @click="changePrice">换价格</button>
16+
</template>
17+
18+
<script>
19+
import {markRaw, reactive, ref, toRaw, toRefs} from 'vue';
20+
21+
export default {
22+
name: 'Demo',
23+
setup(){
24+
25+
let sum = ref(0);
26+
27+
let person = reactive({
28+
name: '张三',
29+
age: 18,
30+
job:{
31+
j1:{
32+
salary: 20
33+
}
34+
}
35+
});
36+
37+
//ref reactive(响应式)
38+
39+
const showRawPerson = () => {
40+
const p = toRaw(person);
41+
// console.log(person); //proxy代理对象 Proxy {....}
42+
p.age++; //注意此时页面不会再发生变化了,普普通通的对象不是响应式
43+
console.log(p); //原始对象数据 {....}
44+
45+
// const sum = toRaw(sum);
46+
// console.log(sum); //undefined //这条路走不通,toRaw只处理reactive对象
47+
}
48+
49+
const addCar = () => {
50+
person.car = markRaw({
51+
name: 'benz',
52+
price: 40
53+
}); //在响应式的对象身上添加任何属性都是响应式的,经过markRaw一包装就变成最原始的数据就不会再做响应
54+
}
55+
56+
57+
const changePrice = () => {
58+
person.car?.price && person.car.price++;
59+
console.log(person?.car?.price);
60+
}
61+
62+
return {
63+
sum,
64+
person,
65+
...toRefs(person),
66+
showRawPerson,
67+
addCar,
68+
changePrice
69+
};
70+
71+
}
72+
}
73+
</script>
74+
75+
<style>
76+
</style>
77+

‎16_src_toRaw和markRaw/main.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//引入不再是Vue构造函数了,引入的是一个名为createApp的工厂函数
2+
//注意在这里无法像vue2那样去书写了,(这里并不兼容)
3+
import { createApp } from 'vue'
4+
import App from './App.vue'
5+
6+
//创建应用实例对象---app类似于vue2中vm,但app比vm更轻
7+
const app = createApp(App);
8+
// console.log(app);
9+
//挂载
10+
app.mount('#app');
11+
12+
// setTimeout(() => {
13+
// app.unmount("#app");
14+
// },2000)
15+
16+
// //vue2写法
17+
// const vm = new Vue({
18+
// render: h=> h(App)
19+
// });
20+
// vm.$mount('#app')

0 commit comments

Comments
 (0)
Please sign in to comment.