4***@qq.com
4***@qq.com
  • 发布:2022-06-28 12:01
  • 更新:2022-06-28 14:06
  • 阅读:326

v-if 没用是为啥

分类:nvue

data() {  
        return {  
            // 底部导航栏登录后显示有用户的导航,false是没登录  
            showUserCenterAvatar : false  
        }  
    },  
    onLoad:function(e) {  
            //判断是否有本地showUserCenterAvatar缓存,没有就显示登录       
            uni.request({  
                ...  
                },  
                success: (res) => {  
                    this.showUserCenterAvatar = true;  
                    console.log('aaaaaaaaaaaa',this.showUserCenterAvatar); //这个结果也是true  
                }  
            });  
    },  

<template v-if="showUserCenterAvatar">                
    aaa  
</template>   
<template v-else>                 
    bbb  
</template>  

问题一:还是在显示else的内容,这是为啥,我写的不规范吗?

问题二:另外我在小程序的控制台里 console.log(showUserCenterAvatar) 结果如下,是因为不能这么写吗?


Uncaught ReferenceError: showUserCenterAvatar is not defined  
2022-06-28 12:01 负责人:无 分享
已邀请:
十二112

十二112

测试正常,是在什么环境下

  • 4***@qq.com (作者)

    我把js部分mixin到了父模版,然后template做成了组件引用到父模版,然后就不行了,是不是引用结构不对,但是在组件里mixin也不对

    2022-06-28 13:34

  • 十二112

    回复 4***@qq.com: 首先第一个,js混入到父模板,组件引用到父模板,你传参给组件了没。

    第二个,组件里混入,组件没有onLoad生命周期

    2022-06-28 13:38

  • 4***@qq.com (作者)

    回复 十二112: 我一会去好好看看文档吧

    2022-06-28 13:51

  • 十二112

    回复 4***@qq.com: 你这样子,混入到父模板,把那个值传给子组件, <子组件 :showUserCenterAvatar=”showUserCenterAvatar“></子组件> 子组件props去接收

    2022-06-28 13:53

十二112

十二112

js文件

export default {  
    data() {    
        return {    
            // 底部导航栏登录后显示有用户的导航,false是没登录    
            showUserCenterAvatar : false    
        }    
    },    
    onLoad:function(e) {    
            //判断是否有本地showUserCenterAvatar缓存,没有就显示登录         
            uni.request({    
                ...    
                },    
                success: (res) => {    
                    this.showUserCenterAvatar = true;    
                    console.log('aaaaaaaaaaaa',this.showUserCenterAvatar); //这个结果也是true    
                }    
            });    
    },    
}

子组件

<template>                   
   <view>  
        <template v-if="showUserCenterAvatar">                  
            aaa    
        </template>     
        <template v-else>                   
            bbb    
        </template>   
    </view>  
</template>  

<script>  
export default {  
    props: {  
        showUserCenterAvatar: {  
            type: Boolean,  
        }  
    }  
}  
</script>

父模板 页面

<template>                   
  <view>  
   <!-- 引入子组件 -->  
    <child :showUserCenterAvatar="showUserCenterAvatar"></child>  
  </view>  
</template>  

<script>  
import child from './child.vue'  
import mixin from './mixin.js'  
export default {  
    components: {  
        child,  
    },  
    mixins: [mixin]  
}  
</script>

要回复问题请先登录注册