Vue 组件开发demo

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


更多专业前端知识,请上 【猿2048】www.mk2048.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值