vue组件基础、定义、注册及使用是怎样的
Admin 2022-09-14 群英技术资讯 788 次浏览
很多朋友都对“vue组件基础、定义、注册及使用是怎样的”的内容比较感兴趣,对此小编整理了相关的知识分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获,那么感兴趣的朋友就继续往下看吧!Vue的两大核心
1. 数据驱动 - 数据驱动界面显示
2. 模块化 - 复用公共模块,组件实现模块化提供基础
组件基础
组件渲染过程
template ---> ast(抽象语法树) ---> render ---> VDom(虚拟DOM) ---> 真实的Dom ---> 页面
Vue组件需要编译,编译过程可能发生在
(1)打包过程 (使用vue文件编写)
(2)运行时(将字符串赋值template字段,挂载到一个元素上并以其 DOM 内部的 HTML 作为模板)
对应的两种方式 runtime-only vs runtime-compiler
runtime-only(默认)
(1)打包时只包含运行时,因此体积更少
(2)将template在打包的时候,就已经编译为render函数,因此性能更好
runtime-compiler
(1)打包时需要包含(运行时 + 编译器),因此体积更大,大概多10Kb
(2)在运行的时候才把template编译为render函数,因此性能更差
启用runtime-compiler
vue.config.js(若没有手动创建一个)
module.exports = {
runtimeCompiler: true //默认false
}
组件定义
1. 字符串形式定义(不推荐)
例子
const CustomButton = {
template: "<button>自定义按钮</button>"
};
这种形式在运行时才把template编译成render函数,因此需要启用运行时编译(runtime-compiler)
2. 单文件组件(推荐)
创建.vue后缀的文件,定义如下
<template> <div> <button>自定义按钮</button> </div> </template>
<template> 里只能有一个根节点,即第一层只能有一个节点,不能多个节点平级
这种形式在打包的时就编译成render函数,因此跟推荐这种方式定义组件
组件注册
1. 全局注册
全局注册是通过Vue.component()注册
import CustomButton from './components/ComponentDemo.vue'
Vue.component('CustomButton', CustomButton)
优点
其他地方可以直接使用
不再需要components指定组件
缺点
全局注册的组件会全部一起打包,增加app.js体积
适合
基础组件全局注册
2. 局部注册
在需要的地方导入
<template>
<div id="app">
<customButton></customButton>
</div>
</template>
<script>
import CustomButton from "./components/ComponentDemo.vue";
export default {
name: "App",
components: { CustomButton }
};
</script>
优点
按需加载
缺点
每次使用必须导入,然后components指定
适合
非基础组件
组件使用
组件复用
<template> <div id="app"> <img alt="Vue logo" src="./assets/logo.png" /> <customButton></customButton> <customButton></customButton> <customButton></customButton> </div> </template>
customButton 组件
<template>
<div id="app">
<button @click="increment">click me {{times}} times</button>
</div>
</template>
<script>
export default {
data() {
return { times: 0 };
},
methods: {
increment() {
return this.times++;
}
}
};
</script>
每个组件都会创建一个新实例,组件的data必须是function,因为每个实例维护自己的data数据
组件传参
1. 通过props属性
定义一个button,按钮文本通过props传入
<template>
<button> {{buttonText}} </button>
</template>
<script>
export default {
props: {
buttonText: String
}
};
</script>
调用者通过attribute传入
<customButton buttonText="Button 1"></customButton> <customButton buttonText="Button 2"></customButton> <customButton buttonText="Button 3"></customButton>
运行效果

2. 通过插槽<slot></slot>
组件在需要替换的地方放入插槽<slot></slot>
<template>
<button style="margin:10px"><slot>Defalt Button</slot></button>
</template>
<script>
export default {
props: {
buttonText: String
}
};
</script>
调用者的innerHtml会替换插槽的值,若为空,使用默认的
运行效果

注意:看到是用自定义组件的innerHtml替换插槽,若插槽只有一个,可以不写name attribute,若多个插槽需指定插槽name attribute
自定义事件
1. 在组件内部调用this.$emit触发自定义事件
<template>
<div style="margin:10px">
<button @click="increment">
<slot>Defalt Button</slot>
</button>
<span>Click me {{times}} times</span>
</div>
</template>
<script>
export default {
props: {
buttonText: String
},
data() {
return { times: 0 };
},
methods: {
increment() {
this.times++;
("increment");
}
}
};
</script>
2. 调用者监听自定义事件
<template>
<div id="app">
<customButton @increment="handleIncrement"></customButton>
<customButton @increment="handleIncrement">
<span style="color:blue">Button 2</span>
</customButton>
<customButton @increment="handleIncrement">Button 3</customButton>
<p>Total click {{totalClicks}} times</p>
</div>
</template>
<script>
import CustomButton from "./components/ComponentDemo.vue";
export default {
name: "App",
components: { CustomButton },
data() {
return { totalClicks: 0 };
},
methods: {
handleIncrement() {
this.totalClicks++;
}
}
};
</script>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了JS相册图片抖动放大展示效果的示例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要介绍了VUE+Canvas实现财神爷接元宝小游戏,需要的朋友可以参考下本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
这篇文章我们来了解jQuery如何实现商品筛选功能,如果经常购物的朋友对于商品的筛选功能应该都不陌生吧,能帮助我们快速的找到所需的商品,那么这个功能究竟是如何实现的呢?下文给大家分享了两种实现思路和方法,感兴趣的朋友就继续往下看吧。
JavaScript怎样做缓动动画?使用JavaScript原生定时器就可以实现动画的缓动效果,实现的原理其实很简单,也就是通过定时器修改边距达到移动动画效果,那么具体怎样做呢?下面我们来详细的了解看看。
目录前言FabricImage 图片类事件派发小结前言虽然我们讲了这么多个章节,但其实目前为止就只有一个 Rect 类能用,略显单调。于是乎,为了让整个画布稍微生动一些,这个章节我们来尝试增加一个图片类,如果你以后需要扩展一个物体类,也是用同样的方法。另外有时候我们还希望在物体属性改变时或者画布创建后做一些额外的事情,
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008