vue3新发现

含有动态指令参数内容(2.6.0新增)

Event Handling

Multiple Event Handlers

指定多个事件处理函数

<!-- both one() and two() will execute on button click -->
<button @click="one($event), two($event)">
  Submit
</button>

Components In-Depth

Non-Prop Attributes

Custom Events

app.component('custom-form', {
  emits: ['in-focus', 'submit']
})

usage for Validate Emitted Events:

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() {
      this.$emit('submit', { email, password })
    }
  }
})
<user-name
  v-model:first-name="firstName"
  v-model:last-name="lastName"
></user-name>
app.component('user-name', {
  props: {
    firstName: String,
    lastName: String
  },
  emits: ['update:firstName', 'update:lastName'],
  template: `
    <input 
      type="text"
      :value="firstName"
      @input="$emit('update:firstName', $event.target.value)">

    <input
      type="text"
      :value="lastName"
      @input="$emit('update:lastName', $event.target.value)">
  `
})

v-model带入修饰符生成的用于检查的prop为modelModifiers
v-model:argument带入修饰符生成的用于检查的prop为argument+"Modifiers"
话说,要是v-model:model会是个什么情况呢?

Slots

Composition API

按照官方文档的说法,以前的option API 配置式的写法会使组件中处理一个任务的逻辑分散在各处option中,如果组件不大,还不至于影响开发,组件变大之后,各个任务的处理逻辑相互交叉,会使代码维护变得痛苦。
新的Composition API可以将处理同一任务的逻辑集中。

ref,赋予值响应特性

In Vue 3.0 we can make any variable reactive anywhere with a new ref function

import { ref } from 'vue'

const counter = ref(0)

console.log(counter) // { value: 0 }
console.log(counter.value) // 0

counter.value++
console.log(counter.value) // 1

转换后会在包裹一层对象,是为了保证始终引用调用,可以保证不丢失值的响应特性。


引用与传值

setup

  • arguments: props, context
    prop: props是响应式对象,对于props不能使用es6解构写法,否则破坏其响应特性。替代方法时使用toRefs方法,该方法只能使必须的prop保持响应性,在非必须prop又想保持其响应性的情况下可以使用toRef
    context: context是一个普通对象,没有被vue处理,可以安全地使用es6结构写法。有attrsslotsemit三个成员对应三个组件属性。
  • setup执行时,组件实例并没有被建立,所以在setup内没有this,只能通过context的四个属性值获取组件的一些情况,同理,由于组件实例没有被创建,也不能访问data、computed、methods。
  • setup的返回值可以在组件的任何地方访问到。返回值会被automatically unwrapped-自动展开?,所以不用对做过ref包装的值手动取出其value再返回。
  • 对于beforeCreate和created的钩子的处理

setup is run around the beforeCreate and created lifecycle hooks
setup在beforeCreate和created之间运行?(不知道翻译是否准确)

意思就是需要在这两个周期里执行的逻辑直接写道setup就行了。

在setup中注入相应生命周期要执行的函数

export default {
  setup() {
    // mounted
    onMounted(() => {
      console.log('Component is mounted!')
    })
  }
}

对应列表:

Options API Hook inside setup
beforeCreate Not needed*
created Not needed*
beforeMount onBeforeMount
mounted onMounted
beforeUpdate onBeforeUpdate
updated onUpdated
beforeUnmount onBeforeUnmount
unmounted onUnmounted
errorCaptured onErrorCaptured
renderTracked onRenderTracked
renderTriggered onRenderTriggered

使用Provide/Inject

  • Provide的数据应该式可响应的
    使用reactive,ref
  • Provide的数据对于inject来说应该是只读的
    使用readonly
<!-- src/components/MyMap.vue -->
<template>
  <MyMarker />
</template>

<script>
import { provide, reactive, readonly, ref } from 'vue'
import MyMarker from './MyMarker.vue

export default {
  components: {
    MyMarker
  },
  setup() {
    const location = ref('North Pole')
    const geolocation = reactive({
      longitude: 90,
      latitude: 135
    })

    const updateLocation = () => {
      location.value = 'South Pole'
    }

    provide('location', readonly(location))
    provide('geolocation', readonly(geolocation))
    provide('updateLocation', updateLocation)
  }
}
</script>

Template Syntax

Dynamic Arguments

  • v-bind
<!-- full syntax -->
<a v-bind:href="url"> ... </a>

<!-- shorthand -->
<a :href="url"> ... </a>

<!-- shorthand with dynamic argument -->
<a :[key]="url"> ... </a>
  • v-on
<!-- full syntax -->
<a v-on:click="doSomething"> ... </a>

<!-- shorthand -->
<a @click="doSomething"> ... </a>

<!-- shorthand with dynamic argument -->
<a @[event]="doSomething"> ... </a>
  • value constraint

Dynamic arguments are expected to evaluate to a string, with the exception of null. The special value null can be used to explicitly remove the binding. Any other non-string value will trigger a warning.

只接受string,null用于明确解除绑定,除此外的值触发警告

  • expression constraint

Dynamic argument expressions have some syntax constraints because certain characters, such as spaces and quotes, are invalid inside HTML attribute names.

  • recommend using with computed property
  • syntax

access to global method

https://github.com/vuejs/vue-next/blob/master/packages/shared/src/globalsWhitelist.ts#L3

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 表单数据处理是一个项目中的必不可少的内容,编写表单组件是完成复杂项目基础。本文深入整理了Vue制作表单组件的各个方...
    全栈顾问阅读 673评论 0 1
  • 前言: 基于vue 2+ 写一份知识总结,可以说是学习笔记 目录: 一、vue实例的基本结构二、vue事件处理、绑...
    下一机阅读 6,176评论 3 27
  • 一、简介 1、 Vue.js 是什么 参考网址:https://cn.vuejs.org/v2/guide/ind...
    满天繁星_28c5阅读 495评论 0 1
  • 什么是Vue.js Vue.js是目前最火的一个前端框架,React是最流行的一个前端框架,(React除了开发网...
    EEEEsun阅读 667评论 0 1
  • 什么事Vue.js Vue.js事目前最火的一个前端框架,React是流行的一个框架(React除了开发网站还可以...
    cj小牛阅读 421评论 0 0