wepy中多级嵌套组件修改变量,子组件没更新

在wepy创建的项目中,pages目录添加文件text.wpy:

<style lang="less">
    .child {
        border-bottom: 1px solid #eee;
    }
</style>

<template>
    <view>
        <titlecomp :title="normalVal"></titlecomp>
    </view>
</template>

<script>
import wepy from 'wepy'
import Title from '@/components/title'

export default class Index extends wepy.page {
    data = {
        normalVal: '123',

        iterator: [
            {
                id: '0',
                title: '0000'
            },
            {
                id: '1',
                title: '0001'
            }
        ]
    }

    components = {
        titlecomp: Title
    }

    onLoad () {
        console.log('text.wpy:', this.normalVal);
        // 输出: text.wpy: 123
    }
}
</script>

在components添加文件:
title.wpy

<style lang="less">

</style>

<template>
    <title_two :title="title"></title_two>
</template>

<script>
import wepy from 'wepy'
import Title2 from './title2'

export default class Title extends wepy.component {
    props = {
        title:{
            type: String
        }
    }

    components = {
        title_two: Title2
    }

    onLoad () {
        this.title += 'xxx';
        this.$apply();
        console.log('title.wpy : ',this.title) // 输出 title.wpy :  123xxx
    }
}
</script>

title2.wpy

<style lang="less">

</style>

<template>
    <view>{{title}}</view>
</template>

<script>
import wepy from 'wepy'

export default class Title2 extends wepy.component {
    props = {
        title:{
            type: String
        }
    }

    onLoad () {
        console.log('title2.wpy:',this.title); // 输出 title2.wpy: 123
    }
}
</script>

问题:title2.wpy 没有得到预期的 123xxx

阅读 3.9k
1 个回答
:title.sync="title"

即可。。。。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题