首页 > 其他分享 >vue3 父子组件共享响应式对象

vue3 父子组件共享响应式对象

时间:2023-04-15 10:34:53浏览次数:32  
标签:const log vue3 list props 组件 共享 ref

父组件

<template lang="">
  <div>
    <div class="greetings">按钮值:{{ num }}</div>
    <div>
      <button @click="num++">按钮</button>
    </div>

    <div> i am parent</div>
    <button @click="addList">list新增</button>
    <ul>
      <li v-for="item in list">{{item}}</li>
    </ul>
    <Child :list="list"></Child>
  </div>
</template>
<script>
export default {};
</script>
<script setup>
import { ref } from "vue";
import Child from './Child.vue'
const num = ref(0);
const list = ref([
  '标题1', '标题2'
])
let i = 2;
const addList = () => {
  list.value.push('标题' + ++i)
}
</script>
<style lang=""></style>

子组件

<template lang="">
  <div>
    <p>i am child</p>
    <ul>
      <li v-for="item in props.list">{{item}}</li>
    </ul>
    <button @click='deleteOne'>删除list</button>
  </div>
</template>
<script>
export default {};
</script>

<script setup>
const a = 1;
const props = defineProps(['list'])
console.log('获取到的props', props)
console.log('获取到的props.list', props.list)
// console.log(list)
// const list = ref(['2123'])
const deleteOne = () => {
  let list = props.list 
  list.splice(0, 1)
}
</script>
<style lang=""></style>

最终效果

初始

点击父组件新增

点击子组件删除

标签:const,log,vue3,list,props,组件,共享,ref
From: https://www.cnblogs.com/seekwind/p/17320607.html

相关文章

  • kubernetes-nfs共享存储
    搭建nfs服务端#修改权限chmod-R777/nfs/data#编辑export文件vim/etc/exports/nfs/data*(rw,no_root_squash,sync)(“*“代表所有人都能连接,建议换成具体ip或ip段,如192.168.20.0/24)#配置生效exportfs-r#查看生效exportfs#启动rpcbind、nfs服务systemctlresta......
  • typescript vue3 VueDraggable 报错 Uncaught TypeError: Cannot read properties of
    UncaughtTypeError:Cannotreadpropertiesofnull(reading'element')nnotreadpropertiesofnull(reading'index')错误写法就是说子组件需要用div包着,你用其他东西,他无法添加key,然后就会报错。<template#item="{element}"><Todo:detail=......
  • Angular 复习与进阶系列 – Component 组件 の Lifecycle Hooks
    前言我们在这篇和 这篇中已经学习了几个基本的LifecycleHooks.分别是constructorOnInitAfterContentInitAfterViewInitOnDestroyOnChanges这篇我们会把其余的LifecycleHooks都学完. InitGroupandChangesGroupAngular的Lifecycle可以分为两组.第一组......
  • vue3微信公众号商城项目实战系列(4)第一个页面
    在开始写第一个页面之前,先简单看下index.html、App.vue、main.js、HelloWorld.vue、TheWelcome.vue、WelcomeItem.vue这几个页面及文件是怎么运作的,然后再新建2个页面,完成从一个页面跳转到另一个页面这个最简单的操作。 index.html和main.js代码如下:index.html文件的......
  • element的Popover 弹出框组件关闭
    封装了个message底部消息提示框。采用了Popover组件制作。点击页面某些地方没办法关闭此弹出框。所以写了个右上角的关闭图标,调用组件内部事件:doClose()关闭;doShow()显示     ......
  • 一统天下 flutter - widget 按钮类: Ink/InkWell/InkResponse - 让任意组件支持点击事
    一统天下flutterhttps://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-widget按钮类:Ink/InkWell/InkResponse-让任意组件支持点击事件和点击效果示例如下:lib\widget\button\ink.dart/**Ink/InkWell/InkResponse-让任意组件支持点击事件和......
  • provide/reject多组件嵌套传值
    多组件嵌套传值现在用组件A、组件B、组件C三个组件一、组件嵌套1.组件A(相当于父组件),在组件A调用组件B<template><div><componentb></componentb></div></template><script>importComponentbfrom'../components/componentb.vue'//引用组件......
  • 2023-04-14 vue之组件全局注册
    新建一个vue文件,随便写点什么,然后在main.js中引入,如下:xxx.vue:<template><viewclass="container"><viewclass="content">登录窗口</view></view></template><script>exportde......
  • Vue3 Vite H5 手写一个横向展开的多级树列表
    最近写h5要做那种稍微复杂一点的树,没找到现成的UI组件库可用,vant的树只有两级不满足,只能自己写ps.选框的选择/反选/半选对父子选项的影响还有bug,留到脑子好的时候再优化效果代码框架是Vue3+Vite+Vant4。复选框用的vant的checkbox,应该也可以换别的或者原生。模板<templ......
  • SpringCloud Netflix 组件 的核心/原理
     1.Eureka:服务注册、续约、注销、心跳机制,集群的数据采用弱一致方案AP,分区的概念, https://blog.csdn.net/qq_22270363/article/details/1276690232.Ribbon:客户端(请求发起端)的负载均衡组件,核心有 --服务列表、负载均衡策略IRule、服务可用性检查IPing,负载均衡策略......