1.APP.vue
<template>
? <div>
? ? <School></School>
? ? <Student></Student>
? </div>
</template>
<script>
? //引入組件
? import School from './School.vue'
? import Student from './Student.vue'
? export default {
? ? name:'App',
? ? components:{
? ? ? School,
? ? ? Student
? ? }
? }
</script>
2.index.html
<!DOCTYPE html>
<html>
? <head>
? ? <meta charset="UTF-8" />
? ? <title>練習(xí)一下單文件組件的語法</title>
? </head>
? <body>
? ? <!-- 準(zhǔn)備一個容器 -->
? ? <div id="root"></div>
? ? <!-- <script type="text/javascript" src="../js/vue.js"></script> -->
? ? <!-- <script type="text/javascript" src="./main.js"></script> -->
? </body>
</html>
3.main.js
import App from './App.vue'
new Vue({
? el:'#root',
? template:`<App></App>`,
? components:{App},
})
4.school.vue
<template>
? <div class="demo">
? ? <h2>學(xué)校名稱:{{name}}</h2>
? ? <h2>學(xué)校地址:{{address}}</h2>
? ? <button @click="showName">點我提示學(xué)校名</button>
? </div>
</template>
<script>
? export default {
? ? name:'School',
? ? data(){
? ? ? return {
? ? ? ? name:'尚硅谷',
? ? ? ? address:'北京昌平'
? ? ? }
? ? },
? ? methods: {
? ? ? showName(){
? ? ? ? alert(this.name)
? ? ? }
? ? },
? }
</script>
<style>
? .demo{
? ? background-color: orange;
? }
</style>
5.Student.vue
<template>
? <div>
? ? <h2>學(xué)生姓名:{{name}}</h2>
? ? <h2>學(xué)生年齡:{{age}}</h2>
? </div>
</template>
<script>
? export default {
? ? name:'Student',
? ? data(){
? ? ? return {
? ? ? ? name:'張三',
? ? ? ? age:18
? ? ? }
? ? }
? }
</script>