在vue
angular
react
三大前端框架的大前端时代。许多人选择了vue
,在 github 上的star
,vue
已经超过react
的数量了。虽然star
并不能表明vue
更强,不过在发展速度上看来,vue
确实很快。html
在模块化的前端时代,万物皆组件,vue
学习组件是必不可少的。前端
html
、
jq
以后,在初次接触
vue
的组件时候,倒是满脸蒙蔽。
今天我们以最简单的方式,带vue
小白童鞋们,步入组件的世界~vue
我们今天讲三种组件使用方式react
我们以一个
button
子组件为例git
项目src结构:github
1.写好子组件:web
<template>
<button class="btn" :style="{color:color}">
<slot/> <!-- 插槽 -->
</button>
</template>
<script>
export default {
// 传入子组件的参数写到props
props: {
color: {
type: String, // 颜色参数类型
default: "#000" // 默认黑色
}
}
}
</script>
<style scoped>
.btn {
width: 110px;
height: 60px;
border-radius: 10px;
border: none;
font-size: 15px;
}
</style>
复制代码
2.3.4.父组件:前端框架
<template>
<div id="app">
<!-- 4. 在页面上使用 -->
<Button color="red">我是插槽的值</Button>
</div>
</template>
<script>
// 2. 在页面种引用组件
import Button from '@/components/Button.vue'
export default {
name: "app",
// 3. 在components中声明组件
components: {
Button
}
};
</script>
复制代码
效果:app
main.js
中引用Vue.use
方法1.子组件仍是那样~~:框架
2. 子组件添加install方法
Button.js :
import ButtonComponent from './Button.vue'
// 添加install方法 (插件方法)
const Button = {
install: function (Vue) {
Vue.component("Button", ButtonComponent);
}
}
// 导出Button
export default Button
复制代码
固然 你能够处理多个全局组件:
import ButtonComponent1 from './Button1.vue'
import ButtonComponent2 from './Button2.vue'
import ButtonComponent3 from './Button3.vue'
const buttonList = [
ButtonComponent1,
ButtonComponent2,
ButtonComponent3
];
// 添加install方法 (插件方法)
const Button = {
install: function (Vue) {
buttonList.forEach(button=>{
// 这里 使用每一个组件的 name 属性做为组件名
Vue.component(button.name, button);
})
}
}
// 导出Button
export default Button
复制代码
3.4. main.js
import Vue from 'vue'
import App from './App.vue'
// 3
import Button from '@/components/Button.js'
// 4
Vue.use(Button);
new Vue({
render: h => h(App),
}).$mount('#app')
复制代码
5. 在页面上使用
app.vue:
<template>
<div id="app">
<!-- 5. 在页面上使用 -->
<Button color="blue">我是全局按钮</Button>
</div>
</template>
复制代码
效果以下:
vue.extend
构建组件Vue.prototype
1.写好子组件:
<template>
<p class="Message">{{value}}</p>
</template>
<script>
export default {
data() {
return {
value: "我是一个弹框"
};
}
};
</script>
<style>
.Message {
position: fixed;
bottom: 30px;
width: 200px;
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
border-radius: 10px;
left: 50%;
transform: translateX(-50%);
line-height: 30px;
text-align: center;
font-size: 15px;
animation: messageFade 3s 1;
}
/* 加个简单动画 */
@keyframes messageFade {
0% {
opacity: 0;
-webkit-transform: translate3d(-50%, 80%, 0);
transform: translate3d(-50%, 80%, 0);
}
16% {
opacity: 1;
-webkit-transform: translate3d(-50%, 0, 0);
transform: translate3d(-50%, 0, 0);
}
84% {
opacity: 1;
-webkit-transform: translate3d(-50%, 0, 0);
transform: translate3d(-50%, 0, 0);
}
100% {
opacity: 0;
-webkit-transform: translate3d(-50%, 80%, 0);
transform: translate3d(-50%, 80%, 0);
}
}
</style>
复制代码
2. vue.extend
构建组件
Message.js :
import Vue from 'vue';
import Message from './Message.vue';
// 构造组件
const MessageConstructor = Vue.extend(Message);
// 设置删除组件
const removeDom = (target) => {
target.parentNode.removeChild(target);
};
// 构造组件添加关闭方法
MessageConstructor.prototype.close = function() {
this.visible = false;
removeDom(this.$el);
};
const MessageDiv = (options) => {
// 实例化组件
const instance = new MessageConstructor({
el: document.createElement('div'),
// 组件参数,运用到组件内的data
data: options,
});
// 在body添加组件
document.body.appendChild(instance.$el);
Vue.nextTick(() => {
instance.timer = setTimeout(() => {
// 定时关闭组件
instance.close();
}, 3000);
});
return instance;
};
export default MessageDiv;
复制代码
3. 挂载 Vue.prototype
main.js :
import Message from '@/components/Message.js'
Vue.prototype.$message = Message;
复制代码
4. 使用:
<template>
<div id="app">
<Button color="blue" @click.native="msg">我是全局按钮</Button>
</div>
</template>
<script>
import Button from "@/components/Button.vue";
export default {
name: "app",
components: {
Button
},
methods: {
msg() {
// 4. 使用构造组件
this.$message({value:'我是构造组件'});
}
}
};
</script>
复制代码
效果:
以上就是三种组件的基本使用啦~~