处理组件的边界情况
parent
parent.vue
<template>
<div class="parent">
parent
<child></child>
</div>
</template>
data () {
return {
title: '获取父组件实例'
}
},
methods: {
handle () {
console.log(this.title)
}
}
1.通过$parent获取父元素的data数据 2.使用父组件的 方法 3.改变父组件的title值
child.vue
<div class="child">
child<br>
$parent.title:{{ $parent.title }}<br>
<button @click="$parent.handle">获取 $parent.title</button>
<button @click="$parent.title = 'Hello $parent.title'">改变 $parent.title</button>
<grandson></grandson>
</div>
1.子子级 vue通过
p
a
r
e
n
t
.
parent.
parent.parent获取父父级data,handle方法,改变title值
grandson.vue
<template>
<div class="grandson">
grandson<br>
$parent.$parent.title:{{ $parent.$parent.title }}<br>
<button @click="$parent.$parent.handle">获取 $parent.$parent.title</button>
<button @click="$parent.$parent.title = 'Hello $parent.$parent.title'">改变 $parent.$parent.title</button>
</div>
</template>
child
通过$children获取子元素的data对象与methods方法
parent.vue
<template>
<div>
<children1></children1>
<children2></children2>
<button @click="getChildren">获取子组件</button>
</div>
</template>
<script>
import children1 from './02-children1'
import children2 from './03-children2'
export default {
components: {
children1,
children2
},
methods: {
getChildren () {
console.log(this.$children)
console.log(this.$children[0].title)
console.log(this.$children[1].title)
}
this.$children[0].handle()
this.$children[1].handle()
}
children1.vue
<template>
<div>children1</div>
</template>
<script>
export default {
data () {
return {
title: 'children1 获取子组件 - title'
}
},
methods: {
handle () {
console.log(this.title)
}
}
}
</script>
children2.vue
<template>
<div>children1</div>
</template>
<script>
export default {
data () {
return {
title: 'children1 获取子组件 - title'
}
},
methods: {
handle () {
console.log(this.title)
}
}
}
</script>
ref
获取组件的dom对象 this.
r
e
f
s
.
m
y
t
x
t
然
后
调
用
子
组
件
的
方
法
f
o
c
u
s
(
)
改
变
子
元
素
的
d
a
t
a
数
据
t
h
i
s
.
refs.mytxt 然后调用子组件的方法focus() 改变子元素的data数据this.
refs.mytxt然后调用子组件的方法focus()改变子元素的data数据this.refs.mytxt.value
parent.vue
<template>
<div>
<myinput ref="mytxt"></myinput>
<button @click="focus">获取焦点</button>
</div>
</template>
<script>
import myinput from './02-myinput'
export default {
components: {
myinput
},
methods: {
focus () {
this.$refs.mytxt.focus()
this.$refs.mytxt.value = 'hello'
}
}
}
</script>
myinput.vue
<template>
<div>
<input v-model="value" type="text" ref="txt">
</div>
</template>
<script>
export default {
data () {
return {
value: 'default'
}
},
methods: {
focus () {
this.$refs.txt.focus()
}
}
}
</script>
provide&inject
//依赖注入,导致组件的耦合变高 相当于propsc-props的多次传递 在父组件注册provide title: this.title, handle: this.handle
parent.vue
<template>
<div class="parent">
parent
<child></child>
</div>
</template>
<script>
import child from './02-child'
export default {
components: {
child
},
provide () {
return {
title: this.title,
handle: this.handle
}
},
data () {
return {
title: '父组件 provide'
}
},
methods: {
handle () {
console.log(this.title)
}
}
}
</script>
子组件接收inject: [‘title’, ‘handle’] 子组件可以调用父组件的方法,改变title的值
child.vue
<template>
<div class="child">
child<br>
title:{{ title }}<br>
<button @click="handle">获取 title</button>
<button @click="title='xxx'">改变 title</button>
<grandson></grandson>
</div>
</template>
<script>
import grandson from './03-grandson'
export default {
components: {
grandson
},
inject: ['title', 'handle']
}
</script>
子子组件接收inject: [‘title’, ‘handle’] 子子组件可以调用父组件的方法,改变title的值
grandson.vue
<template>
<div class="grandson">
grandson<br>
title:{{ title }}<br>
<button @click="handle">获取 title</button>
<button @click="title='yyy'">改变 title</button>
</div>
</template>
<script>
export default {
inject: ['title', 'handle']
}
</script>
attrs &listener
parent.vue
<template>
<div>
<myinput
required
placeholder="Enter your username"
class="theme-dark"
@focus="onFocus"
@input="onInput"
data-test="test">
</myinput>
<button @click="handle">按钮</button>
</div>
</template>
<script>
import myinput from './02-myinput'
export default {
components: {
myinput
},
methods: {
handle () {
console.log(this.value)
},
onFocus (e) {
console.log(e)
},
onInput (e) {
console.log(e.target.value)
}
}
}
</script>
v-bind="$attrs"继承父组件的属性 对子组件设置属性可以直接在父组件写 设置inheritAttrs: false 不设置的话会把父组件的属性都继承到子组件的template的根标签最外层div $listeners 子组件的一些事件(dom自带的一些事件)emit给父组件
myinput.vue
<template>
<!--
1. 从父组件传给自定义子组件的属性,如果没有 prop 接收
会自动设置到子组件内部的最外层标签上
如果是 class 和 style 的话,会合并最外层标签的 class 和 style
-->
<!-- <input type="text" class="form-control" :placeholder="placeholder"> -->
<!--
2. 如果子组件中不想继承父组件传入的非 prop 属性,可以使用 inheritAttrs 禁用继承
然后通过 v-bind="$attrs" 把外部传入的非 prop 属性设置给希望的标签上
但是这不会改变 class 和 style
-->
<!-- <div>
<input type="text" v-bind="$attrs" class="form-control">
</div> -->
<!--
3. 注册事件
-->
<!-- <div>
<input
type="text"
v-bind="$attrs"
class="form-control"
@focus="$emit('focus', $event)"
@input="$emit('input', $event)"
>
</div> -->
<!--
4. $listeners
-->
<div>
<input
type="text"
v-bind="$attrs"
class="form-control"
v-on="$listeners"
>
</div>
</template>
<script>
export default {
inheritAttrs: false
}
</script>
快速原型开发
独立运行组件
npm install -g @vue/cli-service-global npm install -g @vue/cli-service-global vue-serve 如果不指定参数 会默认 main.js , index.js , App.vue vue serve
|