vue中组件的3种使用方式详解


Posted in Javascript onMarch 23, 2019

前言

组件是Vue.js最强大的功能之一。组件可以扩展HTML元素,封装可重用的代码。

在vue angular react三大前端框架的大前端时代。许多人选择了vue,在 github 上的star,vue已经超过react的数量了。虽然star并不能代表vue更强,不过在发展速度上看来,vue确实很快。

vue中组件的3种使用方式详解

在模块化的前端时代,万物皆组件,vue学习组件是必不可少的。

vue中组件的3种使用方式详解

可是在大多数人熟悉了纯html、jq之后,在初次接触vue的组件时候,却是满脸蒙蔽。
今天咱们以最简单的方式,带vue小白童鞋们,步入组件的世界~

咱们今天讲三种组件使用方式

  • 基本组件
  • 全局组件
  • 构造组件

1. 基本组件四步骤

  • 写好组件(废话~)
  • 在页面种引用组件
  • 在components中声明组件
  • 在页面上使用

咱们以一个button子组件为例

项目src结构:

vue中组件的3种使用方式详解

组件一般都放在components文件夹下:

1.写好子组件:

<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>

效果:

vue中组件的3种使用方式详解

2. 全局组件五步骤

  • 写好组件(还是废话~)
  • 子组件添加install方法
  • 在 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中组件的3种使用方式详解

2. 构造组件四步骤

  • 写好组件(还**是废话~)
  • vue.extend构建组件
  • 挂载 Vue.prototype
  • 在js中使用

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>

效果:

vue中组件的3种使用方式详解

以上就是三种组件的基本使用啦~~

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对三水点靠木的支持。

Javascript 相关文章推荐
div失去焦点事件实现思路
Apr 22 Javascript
js图片闪动特效可以控制间隔时间如几分钟闪动一下
Aug 12 Javascript
jQuery实现购物车数字加减效果
Mar 14 Javascript
js实现瀑布流的三种方式比较
Jun 28 Javascript
jQuery 监控键盘一段时间没输入
Apr 22 Javascript
JavaScript中获取HTML元素值的三种方法
Jun 20 Javascript
jQuery 如何实现一个滑动按钮开关
Dec 01 Javascript
canvas时钟效果
Feb 16 Javascript
jQuery is not defined 错误原因与解决方法小结
Mar 19 Javascript
iview table render集成switch开关的实例
Mar 14 Javascript
vue根据进入的路由进行原路返回的方法
Sep 26 Javascript
微信小程序使用自定义组件导航实现当前页面高亮
Jan 02 Javascript
ES6入门教程之Array.from()方法
Mar 23 #Javascript
setTimeout与setInterval的区别浅析
Mar 23 #Javascript
如何通过setTimeout理解JS运行机制详解
Mar 23 #Javascript
vue中axios请求的封装实例代码
Mar 23 #Javascript
vueScroll实现移动端下拉刷新、上拉加载
Mar 22 #Javascript
浅谈Angular单元测试总结
Mar 22 #Javascript
JavaScript面试技巧之数组的一些不low操作
Mar 22 #Javascript
You might like
PHP定时自动生成静态HTML的实现代码
2010/06/20 PHP
ThinkPHP整合datatables实现服务端分页的示例代码
2018/02/10 PHP
简单的js分页脚本
2009/05/21 Javascript
javascript 主动派发事件总结
2011/08/09 Javascript
javascript控制swfObject应用介绍
2012/11/29 Javascript
document.getElementBy(&quot;id&quot;)与$(&quot;#id&quot;)有什么区别
2013/09/22 Javascript
HTML+CSS+JS实现完美兼容各大浏览器的TABLE固定列
2015/04/26 Javascript
js简单设置与使用cookie的方法
2016/01/22 Javascript
JavaScript获取当前url根目录(路径)
2016/06/17 Javascript
Three.js基础学习之场景对象
2017/09/27 Javascript
js 毫秒转天时分秒的实例
2017/11/17 Javascript
完美解决axios在ie下的兼容性问题
2018/03/05 Javascript
浅谈Angular6的服务和依赖注入
2018/06/27 Javascript
JS判断字符串是否为整数的方法--简单的正则判断
2018/07/23 Javascript
jQuery滑动效果实现方法分析
2018/09/05 jQuery
vue使用v-if v-show页面闪烁,div闪现的解决方法
2018/10/12 Javascript
vue实现前台列表数据过滤搜索、分页效果
2019/05/28 Javascript
node.JS事件机制与events事件模块的使用方法详解
2020/02/06 Javascript
Javascript ParentNode和ChildNode接口原理解析
2020/03/16 Javascript
用jQuery实现抽奖程序
2020/04/12 jQuery
[03:24]2014DOTA2国际邀请赛 神秘商店生意火爆
2014/07/18 DOTA
pycharm 使用心得(四)显示行号
2014/06/05 Python
Python中的__SLOTS__属性使用示例
2015/02/18 Python
Python中创建字典的几种方法总结(推荐)
2017/04/27 Python
通过Python实现自动填写调查问卷
2017/09/06 Python
浅谈Python的list中的选取范围
2018/11/12 Python
Python对称的二叉树多种思路实现方法
2020/02/28 Python
opencv 查找连通区域 最大面积实例
2020/06/04 Python
设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。
2014/12/30 面试题
个人自我评价和职业目标
2014/01/24 职场文书
舞蹈兴趣小组活动总结
2014/07/07 职场文书
四风批评与自我批评范文
2014/10/14 职场文书
2014年维修工作总结
2014/11/22 职场文书
公司感谢信范文
2015/01/22 职场文书
2016七一建党节慰问信
2015/11/30 职场文书
JS前端使用canvas实现物体的点选示例
2022/08/05 Javascript