简介
此页面可以直接复制运行,包含以下应用:

Vue slot插槽使用
Vue v-model使用
Vue props使用
父子组件数据传递
element-ui使用
HTML方式注册子组件,可以将子组件数据写在一个js文件中,用标签引入,然后将子组件对象置入components中
Live Demo 在线示例
Live Demo

提示
不建议用HTML模式开发项目,建议使用vue-cli3创建项目,使用单文件组件模式开发
Vue cli3

根组件实例和组件(实例)的区别:

https://segmentfault.com/q/1010000012918351

代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Title</title>
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script>
<script src="https://cdn.bootcss.com/element-ui/2.10.1/index.js"></script>
<link href="https://cdn.bootcss.com/element-ui/2.10.1/theme-chalk/index.css" rel="stylesheet">
<style>
#app{
display: flex;
justify-content: space-between;
}
.parent, .child{
width: 45%;
}
.el-card{
height: 100%;
}
</style>
</head>
<body>
<div id="app">
<div class="parent">
<el-card>
<div slot="header">
<span>父组件</span>
</div>
<el-input v-model="ParentMsg"></el-input>
<el-button type="primary" @click="changeChild" style="margin-top: 30px">改变子组件的msg</el-button>
</el-card> </div>
<div class="child">
<el-card>
<div slot="header">
<span>子组件</span>
</div>
<child1 v-show="display" :parent-msg="childMsg"></child1>
<child2 @change-data-from-child="changeParent"></child2>
</el-card>
</div>
</div>
<script>
new Vue({
el: '#app',
data(){
return {
display:true,
ParentMsg:"Hello This is Parent",
childMsg: 'Hello, 我来自父组件的数据'
}
},
methods: {
changeParent(data){
this.ParentMsg = data
},
changeChild(){
this.childMsg = '我被父组件改变了'
}
},
components: {
'child1': {
props: { //定义子组件的prop,用于父组件传递数据到子组件
parentMsg: {
type: String,
default: ''
}
},
template: '<div>\n' +
' <h2 v-show="msgDisplay">{{msg}}</h2>\n' +
' <p>{{parentMsg}}</p>\n' +
' <el-button @click="toggleMsgDisplay" type="success">切换子组件1中的信息显示隐藏</el-button>\n' +
' </div>', data() {//Vue中component的data必须通过function() return
return {
msg: 'This is a Child Component1!',
msgDisplay: true
}
},
methods: {
toggleMsgDisplay() {
this.msgDisplay = !this.msgDisplay
}
}
},
'child2':{
template:"<el-button type='primary' @click='changeParentData' style='margin-top: 30px'>我是子组件2按钮,点击改变父组件内容</el-button>",
data () {
return {
msg:"点击了子组件2按钮"
}
},
methods:{
changeParentData () {
this.$emit('change-data-from-child', '我来自是子组件2') //事件传递用kebab-case风格
}
}
}
},
})
</script> </body>
</html>

Vue 组件基础完整示例2的更多相关文章

  1. Vue 组件基础完整示例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Vue组件基础用法

    前面的话 组件(Component)是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码.根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己所需, ...

  3. Vue组件基础

    <!DOCTYPE html><html>    <head>        <meta charset="utf-8">      ...

  4. vue组件基础之父子传值

    可以看出数据从后端获取过来,最外层的父组件接收数据,子组件不能直接获取,必须由父组件传递,此时使用props,并且父组件的值更新后,子组件的值也会随之更新,但是反过来通过修改子组件props来影响父组 ...

  5. Vue.js 学习笔记之四:Vue 组件基础

    到目前为止,这个系列的笔记所展示的都是一些极为简单的单页面 Web 应用程序,并且页面上通常只有几个简单的交互元素.但在实际生产环境中,Web 应用程序的用户界面往往是由多个复杂的页面共同组成的.这时 ...

  6. vue组件基础之创建与使用

    一.创建组件 <script src="vue.js"></script> <!--引入vue.js文件--> <div id=" ...

  7. Vue组件基础知识总结

    组件系统是Vue.js其中一个重要的概念,它提供了一种抽象,让我们可以使用独立可复用的小组件来构建大型应用,任意类型的应用界面都可以抽象为一个组件树. 那么什么是组件呢?组件可以扩展HTML元素,封装 ...

  8. vue—组件基础了解

    什么是组件? 组件是vue中的一个可复用实例,所以new Vue()是vue中最大的那个组件,根组件,有名字,使用的时候以单标签或双标签使用 vm = newVue() 是最大的组件,具有很多实用性的 ...

  9. Vue组件的基础用法(火柴)

    前面的话 组件(component)是Vue最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码,根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己的需要,使用 ...

随机推荐

  1. window程序意外关闭自动重启脚本实现

    @echo off : tasklist|find /i "xxxx"||start yyyy ping/n 127.1>nul 新建 .bat 文件,将其写入文件 xxxx ...

  2. appium+python 【Mac】UI自动化测试封装框架介绍 <七>---脚本编写规范

    脚本的使用,注释非常关键,无论自己的后期查看还是别人使用,都可以通过注释很明确的知道代码所表达的意思,明确的知道如何调用方法等等.每个团队均有不同的商定形式来写脚本,因此没有明确的要求和规范来约束.如 ...

  3. Vue项目搭建流程

    记录一下vue项目的搭建流程. 1.安装node.npm 下载地址为:https://nodejs.org/en/ 设置环境变量,命令行分别输入: node -v   npm -v  查看安装是否成功 ...

  4. 14、Spring Boot 2.x 集成 Druid 数据源

    14.Spring Boot 2.x 集成 Druid 数据源 完整源码: Spring-Boot-Demos

  5. /bin/sh: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8) centos7

    今天登陆centos 7 遇到一个 警告 /bin/sh: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8) bash : ...

  6. MySQL Bug#67718 浅谈B+树索引的分裂优化

    原文链接:http://hedengcheng.com/?p=525 问题背景 今天,看到Twitter的DBA团队发布了其最新的MySQL分支:Changes in Twitter MySQL 5. ...

  7. AbstractWrapper ,EntityWrapper, QueryWrapper, UpdateWrappe

    https://blog.csdn.net/qq_42112846/article/details/88086035 https://blog.csdn.net/m0_37034294/article ...

  8. 富文本编辑器复制word

    这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用 后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下) ...

  9. luogu 2114 [NOI2014]起床困难综合症 位运算+贪心

    感觉这个思路非常巧妙啊~ code: #include <bits/stdc++.h> #define ll long long #define setIO(s) freopen(s&qu ...

  10. c 判断是否为nan

    /* isnan example */ #include <stdio.h> /* printf */ #include <math.h> /* isnan, sqrt */ ...