Coder Social home page Coder Social logo

Comments (3)

b2nil avatar b2nil commented on June 16, 2024

List item onClick/onSwitchChange 应该是 action 不是 props

抱歉,您是指 onClick/onSwitchChange 等事件不应该放在 props 里面吗?Vue 的 action 具体是指什么?是指 emits 吗?

from taro-ui-vue3.

pipinet avatar pipinet commented on June 16, 2024

是的。应该是emits出去。而不是通过props传入。

from taro-ui-vue3.

b2nil avatar b2nil commented on June 16, 2024

Vue 3.0 文档确实是建议将自定义事件写在 emits 里面,rfc 里面也列出了为什么这么做的 5 条 Motivation

不过,由于 Vue 3.0 的渲染函数 api 采用了扁平式 VNode 结构,事件监听器 (event listeners) 都包含在了 $attrs 里面,以 on 开头的属性 (props) 会被处理为 v-on 绑定。

{
  class: ['foo', 'bar'],
  style: { color: 'red' },
  id: 'foo',
  innerHTML: '',
  onClick: foo,
  key: 'foo'
}

例如:

<Foo @click="foo" @custom-event="bar" />

无论自定义事件是写在 props 里面,还是 emits 里面,都会被会被编译为:

h(Foo , {
  onClick: foo,
  onCustomEvent: bar
})

在最终呈现效果和用法上,都是一致的。这一点跟 Vue 2 不同。

在 Vue 2 里,如果把自定义事件写在 props 里面,如: props: { onCustomEvent: Function }, 则不能使用 v-on 语法 @custom-event="bar" 来触发事件,只能使用 v-bind 语法,即 :onCustomEvent="bar" 来触发。

taro-ui-vue3 现在发布的 alpha 版本是直接采用渲染函数来写的,最大限度地利用了 taro-ui 原有的类型定义文件(便于使用 IDE 对 Typescript 的自动提示)。

现在有一个 feat/sfc 分支,正在使用 SFC 进行重构。其中事件声明部分就都采用 emits 来写了。有兴趣的话,欢迎来 PR。

from taro-ui-vue3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.