基于 vue3+ts 的父组件改变多个子组件的值 和 调用不同子组件的方法

基于 vue3+ts 的父组件改变多个子组件的值 和 调用不同子组件的方法
1.父组件代码

主要用 ref<InstanceType<typeof 组件>>() 来获取各个子组件实例

注意事项:

setup中的变量feature和world 一定要和上面标签上ref后面的一致,不然拿到的是空的,很重要!很重要!

<template>
  <div class="father">
    <MyWorld ref="world"/>
    <Feature ref="feature" />
  </div>
  <div @click="change">改变事件</div>
</template>

<script lang="ts">
import { defineComponent,onMounted } from 'vue'
import { ref } from 'vue'
import Feature from './Feature.vue'
import MyWorld from './myWorld.vue'

export default defineComponent({
    components:{
        MyWorld,
        Feature
    },
    setup(){
        // 要定义在外面,然后return出去
        // feature和world 一定要和上面标签上ref后面的一致,不然拿到的是空的,很重要!很重要!
        const world = ref<InstanceType<typeof MyWorld>>()   //泛类型   <typeof HelloWorld>的HelloWorld是组件
        const feature = ref<InstanceType<typeof Feature>>()

        // 使用的时候在 onMounted里面 或者 其他方法里面 ,不然子组件的dom没加载完毕,就获取不到
        onMounted(() => {
          // 改变前
          console.log('改变前1',world.value);
          console.log('改变前2',feature.value);
        })

      const change = () =>{
        // 改变子组件的值
        world.value!.msg = '你好,我的世界!'   // !为断言,防止为空报错
        feature.value!.plan = '你好,我的未来!'

        // 改变后
        console.log('改变后1',world.value);
        console.log('改变后2',feature.value);

        // 调用子组件的方法,也可以不用问号,可以传参
        world.value?.seyHello('1111')  
        feature.value?.toDraw('2222')
      }

      return {feature,world,change}
    }
})
</script>

2.子组件1 - MyWorld

<template>
  <div class="world">
    <h1>{{ msg }}</h1>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { ref,toRaw } from 'vue'

export default defineComponent({
  setup(){
    let msg = ref('我的世界');
    const seyHello = (str:string) => {
        console.log(msg.value + str);
    }
    return { msg,seyHello }
  }
})
</script>

3.子组件2 - Feature

<template>
  <div class="feature">
    <h1>{{ plan }}</h1>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { ref,toRaw } from 'vue'

export default defineComponent({
  setup(){
    let plan = ref('我的未来');
    const toDraw = (str:string) => {
        console.log( plan.value + str );
    }
    return { plan,toDraw }
  }
})
</script>

原载:
(https://www.cnblogs.com/wxhcode/p/15905693.html)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。首先,我们需要使用Vue3和TypeScript创建一个组件。在组件中,我们可以使用HTML5 canvas元素来绘制多颜色弥散渐变循环动态背景。 以下是一个简单的实现示例: ```html <template> <canvas ref="canvas"></canvas> </template> <script lang="ts"> import { defineComponent } from 'vue'; export default defineComponent({ name: 'GradientBackground', mounted() { this.initCanvas(); this.draw(); }, methods: { initCanvas() { this.canvas = this.$refs.canvas as HTMLCanvasElement; this.ctx = this.canvas.getContext('2d') as CanvasRenderingContext2D; this.canvas.width = window.innerWidth; this.canvas.height = window.innerHeight; }, draw() { const { ctx, canvas } = this; const grd = ctx.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 ); grd.addColorStop(0, '#F44336'); grd.addColorStop(0.2, '#FFEB3B'); grd.addColorStop(0.4, '#4CAF50'); grd.addColorStop(0.6, '#2196F3'); grd.addColorStop(0.8, '#9C27B0'); grd.addColorStop(1, '#FF9800'); ctx.fillStyle = grd; ctx.fillRect(0, 0, canvas.width, canvas.height); requestAnimationFrame(this.draw); }, }, }); </script> ``` 在上面的代码中,我们使用 `createRadialGradient()` 方法创建一个径向渐变。然后,我们使用 `addColorStop()` 方法来添加多个颜色停止点。最后,我们使用 `fillStyle` 属性设置填充样式,并使用 `fillRect()` 方法在画布上绘制一个填充矩形。 我们将 `draw()` 方法放在 `requestAnimationFrame()` 中,以便在每个动画帧中绘制更多的背景。这样可以创建一个动态的背景,可以无限循环地进行多颜色弥散渐变。 最后,我们在 `mounted()` 生命周期钩中初始化画布,并调用 `draw()` 方法来开始绘制背景。 希望这可以回答你的问题!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值