Home > Web Front-end > Vue.js > What are the points to note in Vue3 setup and what are the watch monitoring attributes?

What are the points to note in Vue3 setup and what are the watch monitoring attributes?

王林
Release: 2023-05-14 15:31:06
forward
1082 people have browsed it

1, setup instructions

1.1The execution time of setup

1.The execution time of setup is earlier than beforeCreate execution

What are the points to note in Vue3 setup and what are the watch monitoring attributes?

  export default {
    name: "Demo",
    beforeCreate(){
      console.log('beforeCreate已执行');
    },
    setup() {
      console.log('setup已执行');
      let person = reactive({
        name: "小明",
        age: 20,
      });
      return {
        person,
      };
    },
  };
Copy after login

1.2.steup parameters

setup parameters
1.props: The value is an object, including: properties passed from outside the component and received internally by the component

2.context :Context object
①attrs: The value is an object, including: attributes passed from outside the component but not declared in the props configuration, equivalent to this.$attrs

  export default {
    name: "Demo",
    props:['msg','age'],
    setup(props) {
      console.log(props);
      let person = reactive({
        name: "小明",
        age: 20,
      });   
      return {
        person,
      };
    },
  };
Copy after login

What are the points to note in Vue3 setup and what are the watch monitoring attributes?

②slots: The received slot content is equivalent to this.$slots.

Define the slot in the App

<template v-slot:qwe>
<span>123</span>
</template>
<template v-slot:ewq>
<span>321</span>
</template>
Copy after login

Get the slot in the subcomponent

 console.log(context.slots); // 得到插槽
Copy after login

What are the points to note in Vue3 setup and what are the watch monitoring attributes?

③emit: A function that distributes custom events, equivalent to this.$emit.

Write a custom event in the App and pass it to the component

<Demo @hi="Hello" msg="山鱼" age=10>
</Demo>
Copy after login
  setup() {
    function Hello(){
      console.log(&#39;你好!&#39;);
    }
    return {
      Hello
    }
  }
Copy after login

Then go to the sub-component and use context.comit to get the custom event

What are the points to note in Vue3 setup and what are the watch monitoring attributes?

 function point(){
        context.emit(&#39;hi&#39;,666)
      }     
5TgxPT2v-1681788304084)]

```js
 function point(){
        context.emit(&#39;hi&#39;,666)
      }
Copy after login

It is consistent with the computed configuration function in Vue2

import { reactive,computed} from "vue";
  export default {
    name: "Demo",
    setup() {
      let person = reactive({
        firstName: "小",
        lastName: "明",
      }); 
      // 计算属性的简写形式,不考虑修改,是只读的
      /*person.fullName= computed(()=>{
        return person.firstName+&#39;-&#39;+person.lastName
      }) */
      // 计算属性的完整形式(可以读改)
      person.fullName= computed({
        get(){
          return person.firstName +&#39;-&#39;+person.lastName
        },
        set(value){
          const arr = value.split(&#39;-&#39;)
          person.firstName = arr[0]
          person.lastName = arr[1]
        }
      })
      return {
        person,
      };
    },
  };
Copy after login

Second, watch monitoring attribute

There are two kinds of watches, namely Single attribute data monitoring, and multiple attribute data monitoring

The three parameters in watch are the monitored object, the monitored function, and the configuration of the monitored attribute

Monitoring the data defined by ref

①Monitoring attribute monitors a responsive value of ref

    watch(sum, (newvalue, oldvalue) => {
      console.log(&#39;当前值为&#39;+newvalue, &#39;以前值为&#39;+oldvalue);
    });
Copy after login

②Monitors multiple responsive data defined by ref

        watch([sum,msg], (newvalue, oldvalue) => {
      console.log(&#39;当前值为&#39;+newvalue, &#39;以前值为&#39;+oldvalue);
    });
Copy after login

Monitors data defined by reactive

①Monitor changes in data defined by reactive

Data defined using reactive cannot correctly obtain newValue using watch
and deep monitoring will be forced to open

             watch(person,(newValue, oldValue) => {
                console.log(&#39;person变化了&#39;,newValue,oldValue)
             })
Copy after login

②Monitor certain attributes of the responsive data defined by reactive

            watch(()=>person.name,(newValue,oldValue)=>{
               console.log(&#39;person.name发生了变化&#39;,newValue,oldValue)
           })
Copy after login

③Monitor certain attributes of the responsive data defined by reactive

watch([()=>{person.age},()=>{person.name}],(newValue,oldValue)=>{
                console.log(&#39;person.name发生了变化&#39;,newValue,oldValue)
            })
Copy after login

④Special Situation

Note: This situation monitors a certain attribute in the object defined by recative, so deep can turn on

 watch(()=>person.job,(newValue,oldValue)=>{
                console.log(&#39;person.name发生了变化&#39;,newValue,oldValue)
            }, {deep: true})
Copy after login

The above is the detailed content of What are the points to note in Vue3 setup and what are the watch monitoring attributes?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template