vue基础5
1.组件通信
a.父传子:
// 总结:1.父传子:传递的是基础数据类型 给父组件中的子组件绑定属性,此时属性的值在父组件中已经定义,子组件需要通过porps接收,要用数组接收 在子组件中直接渲染接收到的值即可
// 2.父传子:子修改父组件传递的值报错,如果解决,把父组件传递过来的值变为自己的属性,直接修改自己属性即可。后果:父组件修改不会影响自己的数据
// 3.父传子:父变,子变,子变,父变,需要在父组件中定义对象,传递给子组件的就是对象的方式,子组件正常接收即可
parent.vue 父组件
<template>
<div>
parent
<div>这是给儿子的礼物----{{ msg }}</div>
<input type="text" v-model="msg" />
<div>{{info}}</div>
<input type="text" v-model='info.name'>
<v-child :gift="msg" :money="money" :info='info'></v-child>
</div>
</template>
<script>
import vChild from "./child";
export default {
components: {
vChild,
},
data() {
return {
msg: "大奔",
money: 2000,
info:{
name:'张三'
},
};
},
methods: {},
mounted() {},
};
</script>
<style>
</style>
child.vue 子组件
props: ["gift", "money",'info'],
parentCase
case.vue
<template>
<div>
<v-java></v-java>
<v-ui></v-ui>
<v-web></v-web>
</div>
</template>
<script>
import vJava from './java'
import vUi from './ui'
import vWeb from './web'
export default {
components:{
vJava,
vUi,
vWeb
},
data () {
return {
}
},
methods:{
},
mounted(){
}
}
</script>
<style scoped>
</style>
java.vue
<template>
<div>
<h2>java讲师</h2>
//这是循环添加的 key必须要绑定 由于card里面渲染的是具体的数据,所以传递过去对象即可
<v-card v-for='item in arr' :key='item.id' :teachers='item'></v-card>
<!-- <v-card></v-card> -->
</div>
</template>
<script>
import vCard from './card'
export default {
components:{
vCard
},
data () {
return {
arr:[
{
id:1,
img:'http://www.ujiuye.com/uploadfile/2019/0109/20190109071725808.jpg',
name:'李老师',
job:'院长'
},
{
id:2,
img:'http://www.ujiuye.com/uploadfile/2019/0423/20190423094745162.jpg',
name:'张老师',
job:'副院长'
},
{
id:3,
img:'http://www.ujiuye.com/uploadfile/2019/0423/20190423105110779.jpg',
name:'伍老师',
job:'高级讲师'
},
]
}
},
methods:{
},
mounted(){
}
}
</script>
<style scoped>
</style>
card.vue
props:['teachers']
b.子传父:
子组件通过this.$emit触发方法
child.vue
<template>
<div>
child
<button @click="send">点击给父亲打钱</button>
</div>
</template>
<script>
export default {
components:{
},
data () {
return {
money:'2万'
}
},
methods:{
send(){
// $emit 用来触发子传父的方法的
this.$emit('giveTo',this.money)
}
},
mounted(){
}
}
</script>
<style scoped>
</style>
parent.vue
<template>
<div>
parent
这是儿子给的钱----{{myMoney}}
<v-child @giveTo='own'></v-child>
</div>
</template>
<script>
import vChild from './child'
export default {
components:{
vChild
},
data () {
return {
myMoney:''
}
},
methods:{
own(e){
console.log(e)
this.myMoney = e
}
},
mounted(){
}
}
</script>
<style scoped>
</style>
c.非父子:
1.首先创造关系 main.js->Vue.prototype.Event=new Vue()
总结:发送数据用$emit 需要触发条件 接收数据用$on
a.vue
<template>
<div>
aaaaa
<button @click="sendB">把数据发送给B</button>
</div>
</template>
<script>
export default {
components:{
},
data () {
return {
msgA:'我是a的数据'
}
},
methods:{
sendB(){
// 发送数据
this.Event.$emit('sendB',this.msgA)
}
},
mounted(){
console.log(this.Event) //Vue
}
}
</script>
<style scoped>
</style>
b.vue
<template>
<div>
bbbbb----fromA{{fromA}}
</div>
</template>
<script>
export default {
components:{
},
data () {
return {
fromA:''
}
},
methods:{
},
mounted(){
// 接收数据 $on()
this.Event.$on('sendB',(e)=>{
console.log(e)
this.fromA = e
})
}
}
</script>
<style scoped>
</style>
b->c数据
b.vue
<template>
<div>
bbbbb----fromA{{fromA}}
<button @click="sendC">发送给C</button>
</div>
</template>
<script>
export default {
components:{
},
data () {
return {
fromA:''
}
},
methods:{
sendC(){
this.Event.$emit('sendC',this.fromA)
}
},
mounted(){
// 接收数据 $on()
this.Event.$on('sendB',(e)=>{
console.log(e)
this.fromA = e
})
}
}
</script>
<style scoped>
</style>
c.vue
<template>
<div>
cccccc -----{{fromA}}
</div>
</template>
<script>
export default {
components:{
},
data () {
return {
fromA:''
}
},
methods:{
},
mounted(){
this.Event.$on('sendC',(e)=>{
this.fromA = e
})
}
}
</script>
<style scoped>
</style>
2.is
1.解决标签的固定搭配问题
2.动态组件
<template>
<div>
<!-- is 1.解决标签的固定搭配 ul>li
2.动态组件
-->
<ul>
<li is='vOne'>我是li的内容-------- <v-one></v-one></li>
</ul>
<hr>
<!-- one two 动态组件切换-->
<button @click="name='vOne'">one</button><button @click="name='vTwo'">two</button>
<div :is='name'></div>
</div>
</template>
<script>
import vOne from './one'
import vTwo from './two'
export default {
components:{
vOne,
vTwo
},
data () {
return {
name:'vOne'
}
},
methods:{
},
mounted(){
}
}
</script>
<style scoped>
</style>
3.slot
1.无名插槽
在子组件中添加<slot></slot>
在slot.vue
<v-one>
<div>我就是插入到one组件中的内容1111</div>
<div>我就是插入到one组件中的内容22222</div>
</v-one>
在one.vue
<!-- 无名插槽 -->
<slot></slot>
one
<slot></slot>
2.具名插槽
在slot.vue中
<v-two>
<div slot="aa">白日依山尽</div>
<div slot="bb">黄河入海流</div>
<div slot="cc">欲穷千里目</div>
<div slot="dd">更上一层楼</div>
<p>我是新增加的</p>
</v-two>
在two.vue中
<div>
<slot name='aa'></slot>
<slot name='bb'></slot>
two
<slot name='cc'></slot>
<slot name='dd'></slot>
<slot></slot>
</div>
4.ref(不建议使用)
1.ref 操作普通元素 就是获取到的dom元素
2.ref 操作的组件 获取的就是组件的数据和方法
3.使用ref 需要通过this.$refs来获取
在ref.vue中
<template>
<div>
<div class="box" ref='box'></div>
<v-one ref='one'></v-one>
这是从one组件拿回来的数据{{myMsg}}
</div>
</template>
<script>
import vOne from './one'
export default {
components:{
vOne
},
data () {
return {
myMsg:''
}
},
methods:{
},
mounted(){
// 总结:1.对于普通元 ref ->$refs来获取元素,获取之后就是普通的dom元素.
// console.log(this.$refs.box.offsetWidth)
console.log(this.$refs.one.fn())
console.log(this.$refs.one.msg)
this.myMsg = this.$refs.one.msg
}
}
</script>
<style>
.box{
width: 100px;
height: 100px;
background: red;
}
</style>
5.jquery
1.npm install jquery --save
2.哪个页面需要直接导入即可
import $ from 'jquery'
mounted(){
$('button').click(()=>{
$('.box').width()
})
}
3.全局导入
在main.js
import $ from 'jquery'
Vue.prototype.$ = $;
//此时这个$是vue实例中的一个属性,所以需要通过this调用
this.$('button').click(()=>{
this.$('.box').width()
})
vue基础5的更多相关文章
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十八║Vue基础: 指令(下)+计算属性+watch
回顾 今天来晚辣,给公司做了一个小项目,一个瀑布流+动态视频控制的DEMO,有需要的可以联系我,公司的项目就不对外展示了(一个后端程序员真的要干前端了哈哈哈). 书接上文,昨天正式的开始了Vue的代码 ...
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十九║Vue基础: 样式动态绑定+生命周期
回顾 哈喽大家好,前后端分离系列文章又开始了,今天周一,还是感谢大家花时间来观看我写的博客,周末呢,没有写文章,但是也没有闲着,主要是研究了下遗留问题,看过之前文章的应该知道,之前的在AOP使用Red ...
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十║Vue基础终篇:传值+组件+项目说明
缘起 新的一天又开始啦,大家也应该看到我的标题了,是滴,Vue基础基本就到这里了,咱们回头看看这一路,如果你都看了,并且都会写了,那么现在你就可以自己写一个Demo了,如果再了解一点路由,ajax请求 ...
- python 全栈开发,Day89(sorted面试题,Pycharm配置支持vue语法,Vue基础语法,小清单练习)
一.sorted面试题 面试题: [11, 33, 4, 2, 11, 4, 9, 2] 去重并保持原来的顺序 答案1: list1 = [11, 33, 4, 2, 11, 4, 9, 2] ret ...
- Vue 基础精讲
Vue 基础精讲 Vue 官方文档:https://cn.vuejs.org/v2/guide/ VUE 实例 每个组件都是一个 vue 的实例. 一个 vue 项目是由实例组成的. vue 实例上有 ...
- Vue基础以及指令
Vue 基础篇一 一.Vue框架介绍 之前大家学过HTML,CSS,JS,JQuery,Bootstrap,现在我们要学一个新的框架Vue~ Vue是一个构建数据驱动的web界面的渐进式框架. 目 ...
- 2-5 vue基础语法
一.vue基础语法 语法: {{msg}} html赋值: v-html="" 绑定属性: v-bind:id="" 使用表达式: {{ok? "ye ...
- Vue 1-- ES6 快速入门、vue的基本语法、vue应用示例,vue基础语法
一.ES6快速入门 let和const let ES6新增了let命令,用于声明变量.其用法类似var,但是声明的变量只在let命令所在的代码块内有效. { let x = 10; var y = 2 ...
- vue基础知识之vue-resource/axios
Vue基础知识之vue-resource和axios(三) vue-resource Vue.js是数据驱动的,这使得我们并不需要直接操作DOM,如果我们不需要使用jQuery的DOM选择器,就没 ...
- Vue基础及脚手架环境搭建
From:http://www.jianshu.com/p/dc5057e7ad0d 一.vue基础 “Vue2.0”跟俺一起全面入坑 01 “Vue2.0”跟俺一起全面入坑 02 “Vue2.0”跟 ...
随机推荐
- Nuxt.js 应用中的 components:dirs 事件钩子详解
title: Nuxt.js 应用中的 components:dirs 事件钩子详解 date: 2024/10/31 updated: 2024/10/31 author: cmdragon exc ...
- Nuxt.js 应用中的 components:extend 事件钩子详解
title: Nuxt.js 应用中的 components:extend 事件钩子详解 date: 2024/11/1 updated: 2024/11/1 author: cmdragon exc ...
- Abp源码分析之Abp最小系统
最小系统 创建API项目 创建API项目并安装以下依赖 修改Program.cs为以下内容 using BookApp; var builder = WebApplication.CreateBuil ...
- 人工智能模型训练中的数据之美——探索TFRecord
上一篇:<构建人工智能模型基础:TFDS和Keras的完美搭配> 序言:在人工智能模型的训练过程中,如何高效管理和处理大量数据是一个重要的课题.TensorFlow 的 TFRecord ...
- PhpStorm 中切换PHP8以上报错 VCRUNTIME140.dll 与PHP 版本不兼容
错误原因:PhpStorm 附带了旧版本或错误版本的vcruntime140.dll 解决方法: 打开PhpStorm VCRUNTIME140 存在目录 C:\Program Files\JetB ...
- Paypal绑定招商银行 —— Paypal绑定收款银行账户的注意事项
地址: https://code.newban.cn/446.html
- 解决Python的pip问题:WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None))
相关: pip安装第三方库报错Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) 国内镜像源下 ...
- 高德地图API-搜索提示并定位到位置,卫星地图和标准地图的切换
// _yourMap地图实例 _yourMap.plugin(["AMap.MapType"], function () { //添加地图类型切换插件 //地图类型切换 mapT ...
- string,字符串使用指南
string 创建 创建一个字符串或者字符串数组如下 用 cin 输入,可以读一整串字符直到空格或换行才结束 #include <iostream> using namespace std ...
- Sublime之快捷操作
列举常用的Sublime操作,涉及操作 1.每行默认需要统一添加逗号 1)全选 ctrl + a 2) 组合键 ctrl + shift + l 即可进行操作 (这里是L哦) 之后也可以使用HOME键 ...