uni-app在template内使用非slot传递的变量,在飞书小程序下会渲染失效

发布于 1周前 作者 zlyuanteng 来自 Uni-App

uni-app在template内使用非slot传递的变量,在飞书小程序下会渲染失效

开发环境 版本号 项目创建方式
Vue.js
<template> <view class="box"> <virtual-scroll :list="list" :cacheNum="10"> <template #default="{ item }"> <view class="item"> <view class="name"> {{ item.name }} {{text.label}} </view> <view class="age"> {{ item.age }} </view> </view> </template> </virtual-scroll> </view> </template> <script> import VirtualScroll from "@/components/virtual-scroll/virtual-scroll.vue" export default { name: 'Test', components: { VirtualScroll }, data() { return { text:{label:1233333333333333333}, list: Array.from({ length: 10000 }).map((item, index) => ({ name: '小明-' + index, age: index })) } } } </script> <style lang="scss" scoped> .box { width: 100vw; height: 100vh; .name { font-size: 32rpx; font-weight: bold; line-height: 40rpx; margin-bottom: 20rpx; color: #333; } .age { font-size: 24rpx; color: #999; } .item { margin: 30rpx; padding: 30rpx; box-sizing: border-box; background-color: #eee; border-radius: 12rpx; } } </style>

我在变量里加了一个text对象然后 template内使用这个对象做展示,在飞书的效果是 只会展示一条数据,然后其他都是空的,去掉后又正常了


3 回复

HBuilderX 3.99.2023121601-alpha 已修复。


感谢反馈,后续修复,可以先使用具名插槽临时解决此问题。

uni-app 中,如果你在 template 内使用了非 slot 传递的变量,并且在飞书小程序下出现渲染失效的问题,可能是由于飞书小程序对某些特性的支持不完全或存在兼容性问题。以下是一些可能的解决方案和排查步骤:

1. 检查变量传递方式

确保你在 template 中使用的变量是通过 propsdata 正确传递的。飞书小程序可能对某些数据绑定方式支持不完全。

<template>
  <view>{{ message }}</view>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello, Feishu Mini Program!'
    }
  }
}
</script>

2. 使用 v-ifv-show 进行条件渲染

如果某些变量在特定条件下才需要渲染,可以使用 v-ifv-show 来控制渲染。

<template>
  <view v-if="showMessage">{{ message }}</view>
</template>

<script>
export default {
  data() {
    return {
      showMessage: true,
      message: 'Hello, Feishu Mini Program!'
    }
  }
}
</script>

3. 使用 computed 属性

如果变量需要经过计算或处理后再渲染,可以使用 computed 属性。

<template>
  <view>{{ computedMessage }}</view>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello, Feishu Mini Program!'
    }
  },
  computed: {
    computedMessage() {
      return this.message.toUpperCase();
    }
  }
}
</script>

4. 检查飞书小程序的兼容性

飞书小程序可能对某些 Vue 特性支持不完全,建议查阅飞书小程序的官方文档,了解其对 Vue 特性的支持情况。

5. 使用 slot 传递内容

如果问题确实是由于非 slot 传递的变量引起的,可以尝试使用 slot 来传递内容。

<template>
  <view>
    <slot></slot>
  </view>
</template>

<script>
export default {
  // 组件逻辑
}
</script>

6. 更新 uni-app 版本

确保你使用的是最新版本的 uni-app,因为新版本可能会修复一些兼容性问题。

7. 使用 forceUpdate 强制更新

如果数据更新后视图没有及时渲染,可以尝试使用 this.$forceUpdate() 强制更新视图。

<template>
  <view>{{ message }}</view>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello, Feishu Mini Program!'
    }
  },
  methods: {
    updateMessage() {
      this.message = 'Updated Message';
      this.$forceUpdate();
    }
  }
}
</script>

8. 调试和日志

在飞书小程序中,使用 console.log 输出变量值,检查变量是否正确传递和更新。

<template>
  <view>{{ message }}</view>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello, Feishu Mini Program!'
    }
  },
  mounted() {
    console.log(this.message);
  }
}
</script>
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!