最近在学习vue的工程化,记录下如何封装vue组件
废话不多直接上代码
子组件
<template>
<div>
<el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
<span>这是一段信息</span>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="handle()">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
props: ["dialogVisible"],
data() {
return {};
},
methods: {
handle() {
this.$emit("close");
},
},
};
</script>
<style lang="scss" scoped>
</style>
父组件
<template>
<div>
<el-button @click="showvisible()">默认按钮</el-button>
<dia :dialogVisible="dialogVisible" @close="closeVisidle()"></dia>
</div>
</template>
<script>
import Dialong from "../components/dialong.vue";
export default {
components: {
dia: Dialong,
},
data() {
return {
dialogVisible: false,
};
},
methods: {
showvisible() {
this.dialogVisible = true;
},
closeVisidle() {
this.dialogVisible = false;
},
},
};
</script>
<style lang="scss" scoped>
</style>
好了一个超简单的组件封装完成了,注意在父组件中只需要import引入组件并注册即可食用
import Dialong from "../components/dialong.vue";
export default {
components: {
dia: Dialong,
},
}