响应性
https://vue3js.cn/docs/zh/guide/reactivity-fundamentals.html#%E5%A3%B0%E6%98%8E%E5%93%8D%E5%BA%94%E5%BC%8F%E7%8A%B6%E6%80%81
响应性
组合式API
ref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 | <template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<h1>{{ count }}</h1>
<!-- <button @click="increase">👍+1</button> -->
</div>
</template>
<script lang="ts">
import { ref } from 'vue';
export default ({
name: 'App',
setup() {
const count = ref(0)
return {
count
}
}
});
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 | <template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<h1>{{ count }}</h1>
<button @click="increase">👍+1</button>
</div>
</template>
<script lang="ts">
import { ref } from 'vue';
export default ({
name: 'App',
setup() {
const count = ref(0)
const increase = () => {
count.value++
}
return {
count,
increase
}
}
});
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 | <template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<h1>{{ count }}</h1>
<h1>{{ double }}</h1>
<button @click="increase">👍+1</button>
</div>
</template>
<script lang="ts">
import { ref, computed } from 'vue';
export default ({
name: 'App',
setup() {
const count = ref(0)
const double = computed(() => {
return count.value * 2
})
const increase = () => {
count.value++
}
return {
count,
increase,
double
}
}
});
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
|
reactive
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 | <template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<h1>{{ data.count }}</h1>
<h1>{{ data.double }}</h1>
<button @click="data.increase">👍+1</button>
</div>
</template>
<script lang="ts">
import { computed, reactive } from 'vue';
interface DataProps {
count: number;
double: number;
increase: () => void;
}
export default ({
name: 'App',
setup() {
const data: DataProps = reactive({
count: 0,
increase: () => { data.count++ },
double: computed(() => data.count * 2)
})
return {
data
}
}
});
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 | <template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<h1>{{ count }}</h1>
<h1>{{ double }}</h1>
<button @click="increase">👍+1</button>
</div>
</template>
<script lang="ts">
import { computed, reactive, toRefs } from 'vue';
interface DataProps {
count: number;
double: number;
increase: () => void;
}
export default ({
name: 'App',
setup() {
const data: DataProps = reactive({
count: 0,
increase: () => { data.count++ },
double: computed(() => data.count * 2)
})
const refData = toRefs(data)
return {
...refData
}
}
});
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
|
响应式对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 | <template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<h1>{{ count }}</h1>
<h1>{{ double }}</h1>
<ul>
<li v-for="number in numbers" :key="number">
<h1>{{ number }}</h1>
</li>
<h1>{{ person.name }}</h1>
</ul>
<button @click="increase">👍+1</button>
</div>
</template>
<script lang="ts">
import { computed, reactive, toRefs } from 'vue';
interface DataProps {
count: number;
double: number;
increase: () => void;
numbers: number[];
person: { name?: string };
}
export default ({
name: 'App',
setup() {
const data: DataProps = reactive({
count: 0,
increase: () => { data.count++ },
double: computed(() => data.count * 2),
numbers: [0, 1, 2],
person: {}
})
data.numbers[0] = 5
data.person.name = 'caiji'
const refData = toRefs(data)
return {
...refData
}
}
});
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
|