Vue 组件基础完整示例2
简介
此页面可以直接复制运行,包含以下应用:
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的更多相关文章
- Vue 组件基础完整示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vue组件基础用法
前面的话 组件(Component)是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码.根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己所需, ...
- Vue组件基础
<!DOCTYPE html><html> <head> <meta charset="utf-8"> ...
- vue组件基础之父子传值
可以看出数据从后端获取过来,最外层的父组件接收数据,子组件不能直接获取,必须由父组件传递,此时使用props,并且父组件的值更新后,子组件的值也会随之更新,但是反过来通过修改子组件props来影响父组 ...
- Vue.js 学习笔记之四:Vue 组件基础
到目前为止,这个系列的笔记所展示的都是一些极为简单的单页面 Web 应用程序,并且页面上通常只有几个简单的交互元素.但在实际生产环境中,Web 应用程序的用户界面往往是由多个复杂的页面共同组成的.这时 ...
- vue组件基础之创建与使用
一.创建组件 <script src="vue.js"></script> <!--引入vue.js文件--> <div id=" ...
- Vue组件基础知识总结
组件系统是Vue.js其中一个重要的概念,它提供了一种抽象,让我们可以使用独立可复用的小组件来构建大型应用,任意类型的应用界面都可以抽象为一个组件树. 那么什么是组件呢?组件可以扩展HTML元素,封装 ...
- vue—组件基础了解
什么是组件? 组件是vue中的一个可复用实例,所以new Vue()是vue中最大的那个组件,根组件,有名字,使用的时候以单标签或双标签使用 vm = newVue() 是最大的组件,具有很多实用性的 ...
- Vue组件的基础用法(火柴)
前面的话 组件(component)是Vue最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码,根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己的需要,使用 ...
随机推荐
- 能用的单纯形法python代码
网上找了一些代码,发现有一些是不能用的,出现错误说集合为空 1.网上出现了好多次,但是不能用的,只能部分模型能用,比如例子中所示 原链接:https://www.jianshu.com/p/b233c ...
- c语言1博客作业10
一.本周作业头 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/SE2019-3/homework/10101 ...
- zhengrui集训笔记2
Day_6 计算几何 点积\Large 点积点积 叉积\Large 叉积叉积 极角\Large 极角极角 < π\piπ :叉积判断 else :atan2 旋转\Large 旋转旋转 左乘第一 ...
- 不一样的 Null
前不久处理一个异常的时候发现了一段有趣的代码,一同事在往表里(Sql Server 数据库)添加数据的时候给可以为 null 的字段赋了如下的值: Student stu = new Student( ...
- JQuery 实践---创建元素包装集
1. 利用选择器,选择将被JQuery包装的元素 标识和选择DOM元素.JQuery采用我们已经知道的CSS语法并且扩展了一些.为了利用JQuery来选择元素,请把选择器包装在$()中. 基本CSS选 ...
- CF70E Information Reform
题意:给你一棵树,要选择若干节点,若一个点i没有选择,则有\(d(dis(i,j))\)的代价,其中j被选择.选择一个点代价为k,求最小代价. 首先,考虑这样一个问题: 如果距离a的最近被选点为i,距 ...
- python爬虫demo01
python爬虫demo01 1 import requests, json, time, sys 2 from bs4 import BeautifulSoup 3 from contextlib ...
- Egyptian Collegiate Programming Contest (ECPC 2015)
题目链接:https://vjudge.net/contest/155219#overview. A题,用全排列来找出比当前这个数字字典序还大的排列有几个,然后前缀和dp即可.据说可以康拓展开来快速找 ...
- RHSA-2018:3059-低危: X.org X11 安全和BUG修复更新
修复命令: yum update libXfontyum update libxcbyum update xorg-x11-font-utilsyum update xorg-x11-utilsyum ...
- fsLayuiPlugin数据表格弹出form表单说明
fsLayuiPlugin 是一个基于layui的快速开发插件,支持数据表格增删改查操作,提供通用的组件,通过配置html实现数据请求,减少前端js重复开发的工作. GitHub下载 码云下载 测试环 ...