Vue 组件开发demo
2024-10-22 02:42:56
1、代码地址
github:https://github.com/MengFangui/VueComponentDemo-
2、关键代码
(1)main.js
//引入vue
import Vue from 'vue';
import App from './app.vue';
var app = new Vue({
el: '#app',
//虚拟DOM
render: h => {
return h(App)
}
});
(2)app.vue
<template>
<div>
<vTitle title='Vue组件化'></vTitle>
<vButton @click='parentHandleClick'>点击按钮</vButton>
</div>
</template>
<script>
//导入组件
import vTitle from './views/title.vue';
import vButton from './views/button.vue';
export default {
//局部注册组件
components: {
vTitle,
vButton
},
methods: {
parentHandleClick(e) {
alert('触发父组件事件')
}
}
}
</script>
(3)button.vue
<template>
<button @click="childHandleClick" :style="styles">
<!--显示父元素内容-->
<slot></slot>
</button>
</template> <script>
export default{
props:{
color:{
type:String,
default:'#00cc66'
}
},
computed:{
styles(){
return {
background:this.color
}
}
},
methods:{
childHandleClick(e){
//触发父组件的click事件
this.$emit('click',e)
}
}
}
</script> <style scoped="scoped">
button{
border: 0;
outline: none;
color: #fff;
padding: 4px 8px;
}
button:active{
position: relative;
top: 1px;
left: 1px;
}
</style>
使用import导入css方法:
<style scoped="scoped">
@import '../css/button.css';
</style>
(4)title.vue
<template>
<h1>
<a :href="'#'+title">{{title}}</a>
</h1>
</template> <script>
export default {
props:{
title:{
type:String
}
}
}
</script> <style scoped="scoped">
h1 a{
color: red;
font-size: 24px;
}
</style>
3、效果
最新文章
- c#datagridview
- [BI项目记]-文档版本管理笔记
- 第七章 LED 将为我闪烁:控制发光二级管
- sql server远程备份和恢复
- python中的异常处理
- [codevs2170]悠闲的漫步
- SOA一些资料
- 关于combotree的用法总结
- Bear and Friendship Condition-HZUN寒假集训
- python3 购物车
- windows 下使用 sc 添加创建exe服务;
- BZOJ1406 [AHOI2007]密码箱 数论
- django 定义文章url
- 【Logstash系列】使用Logstash作为收集端采集IIS日志
- Docker 入门 之基本命令
- TCP是如何实现三次握手的?
- Servlet笔记2--模拟Servlet本质、第一个Servlet程序、将响应结果输出到浏览器中
- JDK(三)JDK1.8源码分析【排序】mergeSort
- hdu 5065 数学题
- vs开发nodejs api文档生成神器-apidoc
热门文章
- mysql管理和基本操作
- ORM中的N+1问题
- 理解OCI(Open Container Initiative)及docker的OCI实现(转)
- yii2中判断值是否存在二维数组中
- .NET Core Runtime ARM32 builds now available
- 【转载】一键安装maven脚本
- win10下MongoDB安装
- POJ 2236 Wireless&#160;Network&#160;[并查集+几何坐标 ]
- linux程序与进程内存结构
- React中super(props)和super()以及不写super()的区别