vue3迁移指南笔记

  • Composition API
    1.setup选项是组合API的入口点
    2.利用reactive实现数据相应式,利用ref将基础数据类型包装成对象类型,使用reactive包裹
  • teleport 传送
    作用:使组件挂载到指定元素中 ,避免多层嵌套的弹框样式不好处理
const app = Vue.createApp({});

app.component('modal-button', {
  template: `
    <button @click="modalOpen = true">
        Open full screen modal! (With teleport!)
    </button>

    <teleport to="body">
      <div v-if="modalOpen" class="modal">
        <div>
          I'm a teleported modal! 
          (My parent is "body")
          <button @click="modalOpen = false">
            Close
          </button>
        </div>
      </div>
    </teleport>
  `,
  data() {
    return { 
      modalOpen: false
    }
  }
})

app.mount('#app')
  • 碎片/片段
    1.vue3中正式支持多根节点组件,即片段
    2.要求开发人员明确的定义如何分配属性
  • emits 选项
    即事件也要像props 一样,在子组件中声明,也可有对应的校验规则,将使用组件事件代替本机事件侦听器。
app.component('custom-form', {
  emits: {
    // No validation
    click: null,

    // Validate submit event
    submit: ({ email, password }) => {
      if (email && password) {
        return true
      } else {
        console.warn('Invalid submit event payload!')
        return false
      }
    }
  },
  methods: {
    submitForm(email, password) {
      this.$emit('submit', { email, password })
    }
  }
})
  • v-model 替代了.sync修饰符
    1.除了.trim、.number、.lazy,可以自定义修饰符,
<div id="app">
  <my-component v-model.capitalize="myText"></my-component>
  {{ myText }}
</div>
const app = Vue.createApp({
  data() {
    return {
      myText: ''
    }
  }
})

app.component('my-component', {
  props: {
    modelValue: String,
    modelModifiers: {
      default: () => ({})
    }
  },
  emits: ['update:modelValue'],
  methods: {
    emitValue(e) {
      let value = e.target.value
      if (this.modelModifiers.capitalize) {
        value = value.charAt(0).toUpperCase() + value.slice(1)
      }
      this.$emit('update:modelValue', value)
    }
  },
  template: `<input
    type="text"
    :value="modelValue"
    @input="emitValue">`

对于v-model带有参数的绑定,生成的prop名称将为arg + "Modifiers":

  • createRenderer 自定义渲染器
    根据不同平台的特性,自定义更新/插入/删除/创建等方法,轻松实现跨平台
import { createRenderer } from '@vue/runtime-core'

const { render, createApp } = createRenderer({
  patchProp,
  insert,
  remove,
  createElement,
  // ...
})

// `render` is the low-level API
// `createApp` returns an app instance with configurable context shared
// by the entire app tree.
export { render, createApp }

export * from '@vue/runtime-core'
  • 同一节点上, v-if优先及大于v-for优先及
  • v-bind="obj" 和普通属性优先及为写在后面的覆盖写在前面
  • v-on:event.native 修饰符被移除
<script>
  export default {
    emits: ['close']
  }
</script>
  • for循环中ref不在自动创建数组 $refs
    在Vue 3中,这种用法将不再在中自动创建数组$refs。要从单个绑定中检索多个引用,请绑定ref到一个提供更大灵活性的函数(这是一个新功能):
<div v-for="item in list" :ref="setItemRef"></div>

import { onBeforeUpdate, onUpdated } from 'vue'

export default {
  setup() {
    let itemRefs = []
    const setItemRef = el => {
      if (el) {
        itemRefs.push(el)
      }
    }
    onBeforeUpdate(() => {
      itemRefs = []
    })
    onUpdated(() => {
      console.log(itemRefs)
    })
    return {
      setItemRef
    }
  }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一些概念 Vue Composition API(VCA) 在实现上也其实只是把 Vue 本身就有的响应式系统更显...
    前端精阅读 7,824评论 0赞 23
  • 五一闲在家没事做,又重温了一下vue的入门指南,边看边记录,便于记忆。 1.安装 1.1 直接<script>引入...
    写前端的大叔阅读 918评论 0赞 1
  • 以下内容是我在学习和研究Vue时,对Vue的特性、重点和注意事项的提取、精练和总结,可以做为Vue特性的字典; 1...
    科研者阅读 14,287评论 3赞 24
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,261评论 0赞 29
  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    余生动听阅读 11,248评论 0赞 11

友情链接更多精彩内容