The most basic method for the parent component to pass the value to the child component is as follows: the
child component receives it through props
// 父组件
<template>
<div>
我是父组件
<testApi :environmentId="environmentId"></testApi>
</div>
</template>
<script>
import testApi from '@/views/apiManage/testApi'
export default {
data() {
return {
environmentId: ''
}
},
components: {
testApi
}
}
// 子组件
<template>
<div>
我是子组件
{
{
environmentId }}
</div>
</template>
<script>
export default {
// 接受父组件的值
props: {
environmentId: {
type: String// 定义接收到的数据的类型
default: '这个数据变量的默认值',
required: false //规定这个数据是否必传,默认false
}
},
data() {
return {
}
},
mounted () {
console.log(this.environmentId)
}
}
The second method is to directly set the value in the child component through this.$refs.myTestApi.title =''
// 父组件
<template>
<div>
我是父组件
<button @click="change">改变子组件里面的值</button>
<testApi ref="myTestApi"></testApi>
</div>
</template>
<script>
import testApi from '@/views/apiManage/testApi'
export default {
data() {
return {
}
},
components: {
testApi
},
methods: {
change() {
this.$refs.myTestApi.title = '新增产品'
}
}
}
// 子组件
<template>
<div>
我是子组件
{
{
title }}
</div>
</template>
<script>
export default {
data() {
return {
title: ''
}
},
mounted () {
console.log(this.title)
}
}
The method for the child component to call the parent component is as follows: The
first method: use $emit in the child component to trigger an event to the parent component, and the parent component listens for this event
// 父组件
<template>
<div>
我是父组件
<button @click="change">方法</button>
<testApi @change="change"></testApi>
</div>
</template>
<script>
import testApi from '@/views/apiManage/testApi'
export default {
data() {
return {
}
},
components: {
testApi
},
methods: {
change() {
console.log('我是猪')
}
}
}
// 子组件
<template>
<div>
我是子组件
{
{
title }}
</div>
</template>
<script>
export default {
data() {
return {
title: ''
}
},
mounted () {
this.$emit('change')
}
}
The second method is to call the parent component directly in the child component through this.$parent.event
// 父组件
<template>
<div>
我是父组件
<button @click="change">方法</button>
<testApi></testApi>
</div>
</template>
<script>
import testApi from '@/views/apiManage/testApi'
export default {
data() {
return {
}
},
components: {
testApi
},
methods: {
change() {
console.log('我是猪')
}
}
}
// 子组件
<template>
<div>
我是子组件
{
{
title }}
</div>
</template>
<script>
export default {
data() {
return {
title: ''
}
},
mounted () {
this.$parent.change()
}
}
The third parent component passes the method to the child component and calls this method directly in the child component
// 父组件
<template>
<div>
我是父组件
<button @click="change">方法</button>
<testApi :change="change"></testApi>
</div>
</template>
<script>
import testApi from '@/views/apiManage/testApi'
export default {
data() {
return {
}
},
components: {
testApi
},
methods: {
change() {
console.log('我是猪')
}
}
}
// 子组件
<template>
<div>
我是子组件
{
{
title }}
</div>
</template>
<script>
export default {
props: {
change: {
type: Function,
default: null
}
},
data() {
return {
title: ''
}
},
mounted () {
this.change()
}
}
Refer to this article for the method of calling the parent component by the Vue child component . Various usages may have different efficiency. If you want to study in depth, please continue to find detailed information on the Internet