vue实现同个窗口页面联动
时间: 2023-05-23 08:01:36 浏览: 132
Vue可以使用watch监听数据的变化,然后通过emit方法向兄弟组件通信,从而实现同个窗口页面联动。具体实现可以参考以下代码:
// A组件
<template>
<div>
<input v-model="message">
</div>
</template>
<script>
export default {
data() {
return {
message: ''
}
},
watch: {
message(newVal) {
this.$emit('update', newVal)
}
}
}
</script>
// B组件
<template>
<div>
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
props: {
message: String
}
}
</script>
// 父组件
<template>
<div>
<a-component @update="handleUpdate"></a-component>
<b-component :message="message"></b-component>
</div>
</template>
<script>
import AComponent from './A.vue'
import BComponent from './B.vue'
export default {
components: {
AComponent,
BComponent
},
data() {
return {
message: ''
}
},
methods: {
handleUpdate(val) {
this.message = val
}
}
}
</script>
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)