认识组件化开发
![](https://img2020.cnblogs.com/blog/1877004/202105/1877004-20210523155645866-854928542.png)
组件化开发
![](https://img2020.cnblogs.com/blog/1877004/202105/1877004-20210523155700654-1077253749.png)
Vue的组件化
![](https://img2020.cnblogs.com/blog/1877004/202105/1877004-20210523160119854-558869121.png)
注册组件的方式
![](https://img2020.cnblogs.com/blog/1877004/202105/1877004-20210523160146222-934967010.png)
注册全局组件
![](https://img2020.cnblogs.com/blog/1877004/202105/1877004-20210523160214973-275378184.png)
全局组件的逻辑
![](https://img2020.cnblogs.com/blog/1877004/202105/1877004-20210523160257010-88624701.png)
组件的名称
![](https://img2020.cnblogs.com/blog/1877004/202105/1877004-20210523160335921-247872730.png)
注册局部组件
![](https://img2020.cnblogs.com/blog/1877004/202105/1877004-20210523160424593-231301970.png)
布局组件注册代码
![](https://img2020.cnblogs.com/blog/1877004/202105/1877004-20210523160445152-625555131.png)
02_注册全局组件.html
<!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>
</head>
<body>
<div id="app"></div>
<!-- 子组件要挂载到这里,而不是挂载到<div id="app"></div> -->
<template id="my-app">
<component-a></component-a>
<component-b></component-b>
<!-- 推荐使用横线,不推荐使用驼峰 -->
<!-- <ComponentName></ComponentName> -->
<component-name></component-name>
</template>
<template id="component-a">
<h2>{{title}}</h2>
<p>{{desc}}</p>
<button @click="btnClick">按钮点击</button>
</template>
<template id="component-b">
<div>
<input type="text" v-model="message"/>
<h2>{{message}}</h2>
</div>
</template>
<template id="component-c">
<h2>ComponentC</h2>
</template>
<script src="../js/vue.js"></script>
<script>
const App = {
template: "#my-app",
};
const app = Vue.createApp(App);
// 使用app注册一个全局组件app.component()
// 全局组件: 意味着注册的这个组件可以在任何的组件模板中使用
app.component("component-a", {
template: "#component-a",
data() {
return {
title: "我是标题",
desc: "我是内容, 哈哈哈哈哈",
};
},
methods: {
btnClick() {
console.log("按钮的点击");
},
},
});
app.component("component-b", {
template: "#component-b",
data() {
return {
message: "Hello World",
};
},
});
app.component('ComponentName', {
template: "#component-c"
})
app.mount("#app");
</script>
</body>
</html>
03_注册局部组件.html
<!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>
</head>
<body>
<div id="app"></div>
<template id="my-app">
<h2>{{message}}</h2>
<component-a></component-a>
</template>
<template id="component-a">
<h2>我是组件A</h2>
<p>我是内容, 哈哈哈哈</p>
</template>
<script src="../js/vue.js"></script>
<script>
const ComponentA = {
template: "#component-a"
}
const App = {
template: '#my-app',
components: {
// key: 组件名称
// value: 组件对象
ComponentA: ComponentA
},
data() {
return {
message: "Hello World"
}
}
}
const app = Vue.createApp(App);
// app.component("ComponentA", ComponentA);
app.mount('#app');
</script>
</body>
</html>