<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.js"></script>
</head>
<body>
<div id="app"></div>
</body>
<script>
const Counter = {
data() {
return {
count: 0
}
},
template: `<div>{{count}}<button @click="count++">增加1</button></div>`
}
const XieDaJiao = {
template: `<h2>谢大脚</h2>`
}
const app = Vue.createApp({
components: {
jspang: Counter,
'xie-da-jiao': XieDaJiao
},
template: `
<h2>JSPang.com</h2>
<xie-da-jiao />
<jspang />
`
})
const vm = app.mount("#app")
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo04</title>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.js"></script>
</head>
<body>
<div id="app"></div>
</body>
<script>
const app = Vue.createApp({
data() {
return {
name: 123,
pay: () => {
alert('给你500元')
}
}
},
template: `
<h2>JSPang.com</h2>
<Son :name="name" />
`
})
app.component('Son',{
props:['name'],
template:`<div>{{ typeof name}} div </div>`
})
const vm = app.mount("#app")
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo07</title>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.js"></script>
</head>
<body>
<div id="app"></div>
</body>
<script>
const app = Vue.createApp({
data() {
return {
counter: 0
}
},
template: `
<h2>JSPang.com</h2>
<counter :counter="counter"/>
`
})
app.component('Counter', {
props: ['counter'],
template: `
{{counter}}<button @click="this.counter+=1">增加数量</button>
`
})
const vm = app.mount("#app")
</script>
</html>
数据从父级组件传递给子组件,只能单向绑定。子组件内部不能直接修改从父组件传递过来的数据
单向数据流就是父组件可以向子组件传递数据,但是子组件不能修改数据。