rubic icon indicating copy to clipboard operation
rubic copied to clipboard

返回的state首次undefined,导致图片第一次404

Open breezefeng opened this issue 2 years ago • 6 comments

image

image

image

breezefeng avatar May 23 '23 07:05 breezefeng

你state怎么返回的?

itmanyong avatar May 26 '23 07:05 itmanyong

...toRefs(state)

breezefeng avatar May 26 '23 07:05 breezefeng

Temp workaround:

definePage({
  data: {
    imgUrl: '...',
  },
  setup() {
    // ...
  },
})

a1mersnow avatar May 29 '23 07:05 a1mersnow

OK

breezefeng avatar May 29 '23 07:05 breezefeng

@JasKang After taking a look at the source code, there are probably no good solutions for this problem under current architecture. To fix the problem, we need to declare the data options of native Page, but the setup is executed in attached lifetime. And this is a runtime library, we can't do static extracting too. So I think this should be well documented. When you need some initial value for a property, you have to manually declare it in data options, otherwise it will be undefined. Although, wechat miniapp will just ignore undefined value. There are some cases like this issue.

And for your use case, @breezefeng , I recommand using plugins:

createApp({
  setup() {
    // ... 
  },
  plugins: [
    {
      name: 'url-prefix',
      config: (options) => {
        options.data = {
          imgUrl: 'xxxxx',
          ...options.data,
        }
      },
    },
  ],
})

a1mersnow avatar May 29 '23 09:05 a1mersnow

#14 @a1mersnow 牛的 这个问题目前没有找到好的解决方式,即使添加编译过程 (类似vue 中的 defineProps 宏 ) 也难以处理 hooks(组合式函数)中的类型推断 而如果强制声明初始值就丧失了组合式函数的灵活性。

还需要再进一步讨论

jaskang avatar May 30 '23 03:05 jaskang