dcloudio / uni-app

A cross-platform framework using Vue.js
https://uniapp.dcloud.io
Apache License 2.0
39.96k stars 3.62k forks source link

微信小程序class动态绑定无效 #1033

Closed weixisheng closed 2 years ago

weixisheng commented 4 years ago

问题描述 微信小程序上class动态绑定无效,是否不支持?

复现步骤

 <view :class="{'full-container': list.length == 0}">
    <view :class="computedClassStr"></view>
  </view>
data() {
    return {
      list: []
    }
  },
  computed: {
    computedClassStr() {
      return this.list.length > 0 ? '' : 'full-container'
    }
  },
  onLoad() {
    this.getData()
  },
  methods: {
    getData() {
      setTimeout(() => {
        this.list.push(1)
      }, 200)
    }
  }

预期结果 view标签上full-container不生效

实际结果 image

系统信息:

Environment Info:

System: OS: Windows 10 CPU: (4) x64 Intel(R) Core(TM) i3-4160 CPU @ 3.60GHz Binaries: Node: 10.14.0 - D:\software\node\node.EXE Yarn: 1.15.2 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 6.4.1 - D:\software\node\npm.CMD Browsers: Edge: 44.18362.449.0 npmPackages: @dcloudio/uni-app-plus: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/uni-app-plus-nvue: 0.0.1 @dcloudio/uni-app-plus-nvue-v8: 0.0.1 @dcloudio/uni-cli-shared: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/uni-h5: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/uni-helper-json: * => 1.0.3 @dcloudio/uni-mp-alipay: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/uni-mp-baidu: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/uni-mp-qq: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/uni-mp-toutiao: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/uni-mp-weixin: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/uni-stat: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/uni-template-compiler: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/vue-cli-plugin-hbuilderx: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/vue-cli-plugin-uni: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/vue-cli-plugin-uni-optimize: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/webpack-uni-mp-loader: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @dcloudio/webpack-uni-nvue-loader: 0.0.1 @dcloudio/webpack-uni-pages-loader: ^2.0.0-23720191024001 => 2.0.0-23720191024001 @vue/babel-helper-vue-jsx-merge-props: 1.0.0 @vue/babel-plugin-transform-vue-jsx: 1.0.0 @vue/babel-preset-app: 3.12.1 @vue/babel-preset-jsx: 1.1.1 @vue/babel-sugar-functional-vue: 1.0.0 @vue/babel-sugar-inject-h: 1.0.0 @vue/babel-sugar-v-model: 1.1.1 @vue/babel-sugar-v-on: 1.1.0 @vue/cli-overlay: 4.0.5 @vue/cli-plugin-babel: 3.5.1 => 3.5.1 @vue/cli-plugin-router: 4.0.5 @vue/cli-plugin-vuex: 4.0.5 @vue/cli-service: ^4.0.0 => 4.0.5 @vue/cli-shared-utils: 3.12.1 @vue/component-compiler-utils: 3.0.1 @vue/preload-webpack-plugin: 1.1.1 @vue/web-component-wrapper: 1.2.0 eslint-plugin-vue: ^5.2.3 => 5.2.3 mpvue-page-factory: 1.0.1 mpvue-template-compiler: 1.0.13 uni-h5-vue: 2.6.10 uni-mp-vue: 2.6.10 vue: ^2.6.10 => 2.6.10 vue-eslint-parser: 5.0.0 vue-hot-reload-api: 2.3.4 vue-loader: 15.7.2 vue-router: 3.0.1 vue-style-loader: 4.1.2 vue-template-compiler: ^2.6.10 => 2.6.10 vue-template-es2015-compiler: 1.9.1 vuex: ^3.0.1 => 3.1.1 npmGlobalPackages: @vue/cli: Not Found


**补充信息**
官方文档:我们将 Class 与 Style 的表达式通过 compiler 硬编码到 uni-app 中
wakaryry commented 4 years ago

那你可以考虑使用 数组 的形式。:class="[list.length === 0 && 'full-container']"

weixisheng commented 4 years ago

那你可以考虑使用 数组 的形式。:class="[list.length === 0 && 'full-container']"

异步改变数据,不会动态响应,数组语法也一样

axetroy commented 3 years ago

同样问题,有什么解决办法吗

EDIT: 通过中间变量进行判断

1532812866 commented 2 years ago

有两种曲线办法 1: 使用Number() 把list.length 包裹起来, 这样可以正常使用 2: 使用计算属性 也可以正常使用

zhetengbiji commented 2 years ago

https://github.com/dcloudio/uni-app/issues/1827

StrivingRabbit commented 2 years ago

此贴重复,关闭处理。以 #1827 为准