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、效果

最新文章

  1. c#datagridview
  2. [BI项目记]-文档版本管理笔记
  3. 第七章 LED 将为我闪烁:控制发光二级管
  4. sql server远程备份和恢复
  5. python中的异常处理
  6. [codevs2170]悠闲的漫步
  7. SOA一些资料
  8. 关于combotree的用法总结
  9. Bear and Friendship Condition-HZUN寒假集训
  10. python3 购物车
  11. windows 下使用 sc 添加创建exe服务;
  12. BZOJ1406 [AHOI2007]密码箱 数论
  13. django 定义文章url
  14. 【Logstash系列】使用Logstash作为收集端采集IIS日志
  15. Docker 入门 之基本命令
  16. TCP是如何实现三次握手的?
  17. Servlet笔记2--模拟Servlet本质、第一个Servlet程序、将响应结果输出到浏览器中
  18. JDK(三)JDK1.8源码分析【排序】mergeSort
  19. hdu 5065 数学题
  20. vs开发nodejs api文档生成神器-apidoc

热门文章

  1. mysql管理和基本操作
  2. ORM中的N+1问题
  3. 理解OCI(Open Container Initiative)及docker的OCI实现(转)
  4. yii2中判断值是否存在二维数组中
  5. .NET Core Runtime ARM32 builds now available
  6. 【转载】一键安装maven脚本
  7. win10下MongoDB安装
  8. POJ 2236 Wireless&#160;Network&#160;[并查集+几何坐标 ]
  9. linux程序与进程内存结构
  10. React中super(props)和super()以及不写super()的区别