Vue3.0 learning notes

  1. vue3 creates a vue instance using createApp()

  2. Dynamic parameter expression spaces and quotation marks are invalid in HTML attribute name; The browser will force all attribute names to lowercase

    <a v-bind:['foo' + bar]="value"> ... </a> //report errors
    
    <a v-bind:[someAttr]="value"> ... </a> //Convert to v-bind:[someattr]
    
  3. ref is created as a rendering result and does not exist at the time of initial rendering

  4. Changing the method of the following array will trigger the view update

    push(),pop(),shift(),unshift(),splice(),sort(),reverse()

  5. Replacing the original array with an array containing the same elements is a very efficient operation

  6. Binding events access the original DOM events

    @click="handle($event)"
    
  7. Event modifier

    Concatenation of multiple modifiers is affected by order

    . stop: prevent click event from continuing propagation
    . prevent: submit events without reloading pages
    . capture: capture mode
    . self: the function can only be triggered by binding the element of the event, and the child element cannot be triggered
    . once: the event is triggered only once
    . passive: do not block the default behavior of events, but directly execute the default behavior (such as scrolling behavior) to improve page performance

  8. v-model principle:

    input event listener and value binding

    <input :value="test" @input="test = $event.target.value">
    
  9. v-model modifier

    . lazy: updated when the change event is triggered
    . number: automatically converts the input value to type number. If this modifier is not added, a string will be returned
    . trim: automatically remove the first and last blank characters entered

  10. Component global registration

    const app = Vue.createApp({})
    app.component('my-component-name', {
      // ...  Options
    })
    
  11. The property on the element does not use v-bind binding to represent strings, but v-bind to represent JavaScript expressions

    /*Represents a string*/
    <template title="My journey with Vue"></template>
    
    /*Representation variable*/
    <template :title="titVal"></template>
    
    /*Represents a number*/
    <template :title="10"></template>
    
    /*Represents a Boolean value*/
    <template :title="true"></template>
    
    /*Pass in multiple properties*/
    post: {
      id: 1,
      title: 'My Journey with Vue'
    }
    
    <template v-bind="post"></template>
    /*Equivalent to*/
    <template v-bind:id="post.id" v-bind:title="post.title"></template>
    
    
  12. Prop case naming

    The prop name of hump nomenclature needs to be changed to its equivalent dash delimited name in HTML

    If a string template is used, this restriction does not exist

    /*data*/
     props: ['postTitle']
     /*Template*/
    <template post-title="post"></template>
    
  13. setup is used to solve the separation of concerns

  14. setup is between the lifecycle functions beforeCreate and Created

  15. The name of the life cycle hook registered in setup is the same as that of the optional API, but the prefix is on: that is, mounted will look like onMounted

  16. To create a responsive variable in setup, you need to use the ref function

    //The 'num' data of the view is not updated
      setup (props) {
        let num = 5
        setTimeout(() => { num ++ } ,3000)
        return { num }
      }
      
      //View's' num 'data update
      setup (props) {
        let num = ref(5) // Printed out is an object
        setTimeout(() => { num.value ++ } ,3000)
        return { num }
      },
    
  17. To listen to variables in setup, you need to import the watch function from Vue

    watch(num, (newV, oldV) => {
      console.log(`${oldV} Change to ${newV}`)
     })
    
  18. props in the setup function is responsive

  19. this in the setup function is undefined

  20. Provide / Inject can be used for communication between multi-level nested components. Data is unresponsive. You can add responsiveness with ref or reactive

    const num= reactive({
      value: 0
    })
    
  21. Report: render child nodes to DOM nodes outside the parent component

    <teleport to="body">
    	Vue Render the content as body Child of label
    </teleport>
    

Keywords: Vue

Added by jesseledwards on Sat, 22 Jan 2022 07:21:32 +0200