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

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. [转]python3 跨目录模块调用,你真的懂了吗?

    小伙伴们,你们有遇到过调用自己写的模块(跨目录模块调用),提示你ImportError:No module named ...的情况,如果有,而且到现在还没有搞明白的,我想说,你今天看对文章了. 这篇 ...

  2. vue3.0脚手架 创建项目

    1.下载node最新稳定版本,并且安装 2.安装好之后,在cmd或者terminal下, 使用npm -v 查看当前npm版本,验证是否安装成功 3.安装成功后,运行 npm i -g @vue/cl ...

  3. dpkg: error processing package XXX (--configure) 解决方法 (ubuntu右上角红色警告)

    在 Ubuntu 执行 sudo apt-get upgrade 时,出现了如下的报错: Setting up bluez (4.101-0ubuntu13.1) ... reload: Job is ...

  4. Codeforces Round #584 E2. Rotate Columns (hard version)

    链接: https://codeforces.com/contest/1209/problem/E2 题意: This is a harder version of the problem. The ...

  5. linux mint 19 打开 Windows 下制作的 TXT 文件时‘乱码’

    因为 Linux 采用的是 UTF-8 编码,Windows 的中文编码是 GB18030. 解决的办法:让 Linux 的文本编辑器支持 GB18030 1.我们安装一个小软件"Dconf ...

  6. JavaScript中的变量提升和严格模式

    1.什么是变量提升 所谓的变量提升指的是:函数声明和变量声明总是会被解释器悄悄地被"提升"到方法体(作用域)的最顶部. //先声明后使用 var x; console.log(x) ...

  7. 8月清北学堂培训 Day5

    今天是杨思祺老师的讲授~ 最短路练习题: POJ 1125 Stockbroker Grapevine 有 N 个股票经济人可以互相传递消息,他们之间存在一些单向的通信路径.现在有一个消息要由某个人开 ...

  8. 一个简单的puppeteer爬虫

    const puppeteer = require("puppeteer"); const path = require('path'); const pathToExtensio ...

  9. cxf报错 Cannot find any registered HttpDestinationFactory from the Bus.

    错误信息:Cannot find any registered HttpDestinationFactory from the Bus. 报错主要是因为缺少jetty依赖 一般添加如下依赖即可 < ...

  10. 第二章 c语言概述

    一.#include指令和头文件 1.#include C预处理指令,C编译器在编译前对源代码做一些准备工作 2.stdio.h标准输入输出头文件,提供了关于输入输出的信息供编译器使用 头文件包含了建 ...