<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
<script>
const HelloWorld = {
props: ['modelValue'],
template: `<div>
{{modelValue}}
<button @click="handleClick">计算</button>
</div>`,
methods: {
handleClick() {
this.$emit('update:modelValue', this.modelValue + 5)
}
},
}
const app = Vue.createApp({
components: { HelloWorld },
template: `<div>
<hello-world v-model="count"/>
</div>`,
data(){
return {
count: 0,
}
}
})
app.mount('#root')
</script>
<div id="root1"></div>
<script>
const test = {
props: ['num', 'n'],
template: `<div>
{{num}} - {{n}}
<button @click="handleClick2">计算</button>
</div>`,
methods: {
handleClick2() {
this.$emit('update:num', this.num + 5)
this.$emit('update:n', this.n + 2)
}
},
}
const App = Vue.createApp({
components: { test },
template: `<div>
<test v-model:num="count" v-model:n="n"/>
</div>`,
data(){
return {
count: 0,
n: 0
}
}
})
App.mount('#root1')
</script>
</body>
</html>
|