当前位置: 首页 > news >正文

jq的插件 vue中引用_vue 组件的三种使用方式教程

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

faad308b125fed5bc1a61bbde4f3f14c.png

c7ea476d121fcd15bf28131756751f23.png

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

a51f8a0cc647e06a2f272914e66970d5.png

可是在大多数人熟悉了纯htmljq之后,在初次接触vue的组件时候,却是满脸蒙蔽。

今天咱们以最简单的方式,带vue小白童鞋们,步入组件的世界~

咱们今天讲三种组件使用方式
  1. 基本组件
  2. 全局组件
  3. 构造组件

1. 基本组件四步骤

  1. 写好组件(废话~)
  2. 在页面种引用组件
  3. 在components中声明组件
  4. 在页面上使用
咱们以一个 button子组件为例

项目src结构:

db3fd5a901025fa6d56654dd54532b3e.png

组件一般都放在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>

效果:

76c4c1142e3d548a207505140d5695fa.png

2. 全局组件五步骤

  1. 写好组件(还是废话~)
  2. 子组件添加install方法
  3. main.js 中引用
  4. 使用 Vue.use 方法
  5. 在页面上使用

1.子组件还是那样~~:

2. 子组件添加install方法

Button.js :

import ButtonComponent from './Button.vue'

// 添加install方法
const Button = {
  install: function (Vue) {
    Vue.component("Button", ButtonComponent);
  }
}

// 导出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>

效果如下:

42784fd8af438aec499c381c1821521c.png

2. 构造组件四步骤

  1. 写好组件(还**是废话~)
  2. vue.extend构建组件
  3. 挂载 Vue.prototype
  4. 在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>

效果:

8446b69c0b8f952b16e31f59857f52fc.gif

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

相关文章:

  • oracle sql 执行计划分析_oracle sql 执行计划分析
  • python读取网络摄像头_python 处理网络摄像头
  • vertx源码_使用Vertx构建微服务-阿里云开发者社区
  • 固有属性指的是用户_用户画像-带你认清的你的用户
  • ai如何对齐两个图形边缘_UI设计中如何更好的运用排版法则
  • 世界地图新西兰_新西兰又被实名羡慕了!在这个榜单排名第1,美国才排第34!...
  • 字体感觉小了 引入的vant_Vant中使用rem
  • eplices开发android_Eclipse 安装(Neon 版本)
  • 加密狗模拟器_汽车驾驶模拟器新软件功能
  • 触摸屏开发_莱宝高科:暂无大规模扩充触摸屏产能计划,正开发OFM结构触摸屏...
  • 模拟黑洞图像_nasa绘制黑洞图像(这次我终于看到了! NASA模拟的黑洞高清令人震惊的画面,令人窒息!)...
  • github覆盖远程分支内容_Github快速上手笔记
  • 显示某年某月日历_一眼就沦陷!一款深藏中国文化古韵的日历应用
  • 深度装机大师一键重装_深度一键重装系统软件
  • idea jsp引用路径_JSP学习4
  • JavaScript 如何正确处理 Unicode 编码问题!
  • [笔记] php常见简单功能及函数
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • Akka系列(七):Actor持久化之Akka persistence
  • If…else
  • IndexedDB
  • Linux CTF 逆向入门
  • Netty源码解析1-Buffer
  • opencv python Meanshift 和 Camshift
  • react-core-image-upload 一款轻量级图片上传裁剪插件
  • Web Storage相关
  • 复杂数据处理
  • 搞机器学习要哪些技能
  • 构建二叉树进行数值数组的去重及优化
  • 关于使用markdown的方法(引自CSDN教程)
  • 机器学习 vs. 深度学习
  • 使用 QuickBI 搭建酷炫可视化分析
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • 正则学习笔记
  • 最近的计划
  • 回归生活:清理微信公众号
  • 交换综合实验一
  • ​linux启动进程的方式
  • #Linux(权限管理)
  • #数学建模# 线性规划问题的Matlab求解
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • (1) caustics\
  • (阿里云万网)-域名注册购买实名流程
  • (草履虫都可以看懂的)PyQt子窗口向主窗口传递参数,主窗口接收子窗口信号、参数。
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (附源码)springboot人体健康检测微信小程序 毕业设计 012142
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (一)UDP基本编程步骤
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .htaccess配置常用技巧
  • .net CHARTING图表控件下载地址
  • .Net MVC + EF搭建学生管理系统
  • .net 发送邮件
  • .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况