compontent组件名称如何定义?

Vue.component('name', { /* ... */ })

第一个参数就是“name'”组件的名字。

如果我们想在HTML中写这样的的组件名称,<my-name></my-name>。

那么我们在对组件进行定义的名称的时候要这样写

Vue.component('yName', { /* ... */ })

即首字母大写。

组件分为全局注册和局部注册。

全局注册

Vue.component('MyName', { // ... 选项 ... })

全局注册简单的来说,在任何地方都可以使用这个组件

局部注册

var MyName={// ... 组件内容 ...}

局部注册后然后在components里面定义一下。

new Vue({el:"#demo",components:{'my-name': MyName,}
})

定义component组件

首先我们来定义简单的组件一个写法。

HTML

<div id="demo"><btn-content></btn-content><btn-content></btn-content>
</div>

javascript

<script type="text/template" id="template"><button>按钮点击</button>
</script>
<script>Vue.component("btn-content", {data: {},template: '#template'})new Vue({el: "#demo"})
</script>

关于这个<script type="text/template" id="template">写法,可以参考前期写的这篇文章。

soviya:vue-template三种写法介绍​zhuanlan.zhihu.com

我们在输出页面上可以看到有两个点击按钮。接下来,我们想点击按钮执行点击弹出内容加一操作。

点击事件是用什么指令?用v-on,关于v-on操作可以参考这篇文章

soviya:VUE操作classstyle绑定属性绑定事件事件修饰符​zhuanlan.zhihu.com

原HTML不变。

javascript

script type="text/template" id="template"><button @click="num">按钮点击{{msg}}次</button>
</script>
<script>Vue.component("btn-content", {data: function() {return {msg: 0}},methods: {num: function() {let msg = (this.msg) ++;alert("您即将点击完成" + (msg + 1) + "次");}},template: '#template'})new Vue({el: "#demo"})
</script>

这里需要注意的是,data必须是一个函数,那么data为什么是一个函数呢?假如组件中的data不是函数也可以执行的话,那么会和当前实例中的data起冲突。改变其一,另外一个也会随着改变。但是如果组件中data是一个函数的话,那么里面所定义的内容只在当前有效,不会污染到其它地方。

组件注册分为全局注册和局部注册,上面的实例给出的是全局注册。

通过 Prop 向子组件传递数据

HTML

<div id="demo"><msg title="来自父组件的标题内容"></msg>
</div>

javarscript

<script type="text/template" id="template"><h1>{{"子组件引用:"+title}}</h1>
</script>
<script>Vue.component("msg", {props: ['title'],template: '#template'})new Vue({el: "#demo"})
</script>

通过v-for来循环相似的的模块

HTML

<div id="demo"><msg v-for="post in posts" :title="post.title">{{"字组件引用:"+post.title}}</msg>
</div>

javascript

<script type="text/template" id="template"><h1>{{title}}</h1>
</script>
<script>Vue.component("msg", {props: ['title'],template: '#template'})new Vue({el: "#demo",data: {posts: [{id: 1,title: '第一段内容'},{id: 2,title: '第二段内容'},{id: 3,title: '第三段内容'}]}})
</script>

现在我们想在msg这个组件里面再加h1内容。

<script type="text/template" id="template"><h1>{{content}}</h1><h3>{{title}}</h3>
</script>

这是错误的写法,因为每个组件中都只能用一个根元素,所以要建立一个标签然后使期包裹起来。

按照上面得出,下面这个是正确的写法

<script type="text/template" id="template"><div><h1>{{content}}</h1><h3>{{title}}</h3></div>
</script>

现在有一个需求,就是点击子组件里面的按钮,然后改变父级里面所有字体的大小。

HTML

<div id="demo"><div :style="{fontSize:ftSize+'rem'}"><msg @chage="ftSize+=0.1" v-for="post in posts" :title="post.id">{{"字组件引用:"+post.title}}</msg></div>
</div>

javascript

<script type="text/template" id="template"><div><h3>{{content}}</h3><button @click="$emit('chage')">点击我改变父级全局文字大小</button></div>
</script>
<script>Vue.component("msg", {data: function() {return {content: "这是一段文字",}},props: ['post'],template: '#template'})new Vue({el: "#demo",data: {ftSize: 1,posts: [{id: 1,title: '第一段内容'},{id: 2,title: '第二段内容'},{id: 3,title: '第三段内容'}]}})
</script>

如果想让子改变父,那么首先在父定义一个想要改变的事件。然后在子使用$emit("父级定义的事件名称"。

当事件内容过多的时候,我们写在当前元素不好,不利于阅读。我们要写在methods方法里面。

HTML

<div id="demo"><div :style="{fontSize:ftSize+'rem'}"><msg @chage="onEnlargeText" v-for="post in posts" :title="post.id">{{"字组件引用:"+post.title}}</msg></div>
</div>

javascript

<script type="text/template" id="template"><div><h3>{{content}}</h3><button @click="$emit('chage',0.1)">点击我改变父级全局文字大小</button></div>
</script>
<script>Vue.component("msg", {data: function() {return {content: "这是一段文字",}},props: ['post'],template: '#template'})new Vue({el: "#demo",data: {ftSize: 1,posts: [{id: 1,title: '第一段内容'},{id: 2,title: '第二段内容'},{id: 3,title: '第三段内容'}]},methods: {onEnlargeText: function(num) {this.ftSize += num}}})
</script>

组件上使用v-model

HTML

<div id="demo"><msg></msg>
</div>

javascript

<script type="text/template" id="template"><div><input v-model="value" type="text" /><h1>{{value}}</h1></div>
</script>
<script>Vue.component("msg", {data: function() {return {value: this.value}},template: '#template',methods: {}})new Vue({el: "#demo",data: {},methods: {}})
</script>

vue component动态组件_vue-component组件相关推荐

  1. vue 获取当前路由_VUE 在组件中 获取 路由信息

    一.核心代码 1.获取参数 this.$route.query.id this.$route.query 2.页面跳转 登录 3.方法跳转 this.$router.push({ path: '/lo ...

  2. vue 父组件获取接口值传到子组件_vue父组件异步获取数据传给子组件的方法

    但是现在问题是父组件的数据是异步获取的,而子组件一开始就会渲染,如果此时没有传入数据,而子组件又要用到数据中的length属性时就会报错: 怎么办呢?最简单的办法就是让子组件条件渲染,当有数据的时候才 ...

  3. vue 手风琴组件_vue 手风琴组件

    1.创建组件 SqueezeBox.vue {{item.name}}{{item.show}} {{a.name}} {{b.name}} export default { data () { re ...

  4. vue怎么根据id获取组件_vue子组件,如何根据父组件传进来的id,去查询详情,并在子组件中显示?...

    如果我在组件的created方法里面,根据id去后台查询详情,可以正常显示,不报错,但是当父组件id值改变后,并不会再次去后台查询了, ,假如我后台返回的对象时detail,如果写在computed里 ...

  5. vue 获取动态域名_vue项目接口域名动态获取操作

    需求: 接口域名是从外部 .json 文件里获取的. 思路: 在开始加载项目前 进行接口域名获取,然后重置 接口域名的配置项. 实现: 1.config/index.js 文件 进行基础配置 impo ...

  6. 【Vue 组件化开发 三】父组件给子组件传递数据、组件通信(父传子、子传父)、父访问子(children、ref)、动态组件(is、component)

    目录 一.前言 完整内容请关注: 二.父组件给子组件传递数据 1.使用props属性,父组件向子组件传递数据 1.使用组件的props属性 2.向cmessage对象传值 2. props属性使用 1 ...

  7. import() 动态加载component组件失败

    在写 vue+element 从后台获取数据写导航栏 时,当我加载动态路由,import() 总是失败. 假设 path: "@/views/Home.vue",name: &qu ...

  8. 【Vue】组件(component)

    文章目录 一.什么是组件 二.为什么需要组件 三.创建一个组件 四.组件的复用 五.通过 Prop 向子组件传递数据 六.通过插槽分发内容 七.动态组件 八.注意事项 一.什么是组件 所谓的组件,其实 ...

  9. vue 组件 全局组件和局部组件component

    1.全局组件 <!DOCTYPE html> <html><head><meta charset="UTF-8"><title ...

  10. Vue3.0使用Element Plus组件报错[Vue warn]: Failed to resolve component: `el-XXXX` If this is a native custo

    文章目录 报错截图 一.问题描述 二.报错信息格式 三.报错原因 报错截图 一.问题描述 我的技术栈:Vue3+TypeScript+Vite+Element Plus 我的报错:Vue3 项目使用 ...

最新文章

  1. Windows下MongoDB安装及创建用户名和密码
  2. linux磁盘空间清理
  3. Windows下挂载iscsi存储及多路径功能配置
  4. switch判断条件
  5. 计算机字符编码详尽讲解
  6. css before 文字前面竖线_前端进阶: css必知的几个底层知识和技巧
  7. node --- 后端使用body-parse解析Post请求,前端使用axios发送Post请求
  8. Spring Security中的SecurityContext和SecurityContextHolder是什么?
  9. python的文件基本操作和文件指针
  10. DG导入mysql依赖包_MySql导入导出数据库(含远程导入导出)
  11. Android 系统(191)---ODM 开发用户常见需求文档(九)
  12. 分布式搜索elasticsearch 索引文档的增删改查 入门
  13. AngularJS 的常用特性(四)
  14. ThreadLocal是什么?ThreadLocal的原理分析
  15. 乌班图vim怎么编译c语言,在Ubuntu上利用vim进行程序编写及运行
  16. 致远项目管理SPM系统五大技术平台层-CMP能力概述
  17. php生成手机桌面快捷方式,php三种创建桌面快捷方式
  18. 解决问题-ERROR 1044 (42000) Access denied for user ''@'localhost' to database 'mysql
  19. 多幸运用计算机演奏的乐谱,多幸运钢琴简谱-数字双手-韩安旭
  20. 使用yguard混淆,名字出现超长字符 map=“ooooooooooooooo”

热门文章

  1. SpringBoot简单使用
  2. HTML5原生拖拽/拖放 Drag Drop 详解
  3. 可控制转速CSS3旋转风车特效
  4. 对象序列化 输入输出流概念 InputOutStream OutputStream
  5. java jvm学习笔记二(类装载器的体系结构)
  6. Windows Server 2012 R2 WSUS-13:部署二级WSUS
  7. centos安装nginx小记
  8. 介绍一个.Net资源站点
  9. Data Mapper
  10. Linux下安装Weblogic10.3.6并创建简单集群测试