Vue3框架中CompositionAPI的基本使用(第十课)

news2025/2/23 14:29:43

1.Setup函数

  1. 理解:Vue3.0中一个新的配置项,值为一个函数。

  2. setup是所有Composition API(组合API)“ 表演的舞台 ”

  3. 组件中所用到的:数据、方法等等,均要配置在setup中。

  4. setup函数的两种返回值:

    1. 若返回一个对象,则对象中的属性、方法, 在模板中均可以直接使用。(重点关注!)

    2. 若返回一个渲染函数:则可以自定义渲染内容。(了解)

  5. 注意点:

    1. 尽量不要与Vue2.x配置混用

      • Vue2.x配置(data、methos、computed...)中可以访问到setup中的属性、方法。

      • 但在setup中不能访问到Vue2.x配置(data、methos、computed...)。

      • 如果有重名, setup优先。

    2. setup不能是一个async函数,因为返回值不再是return的对象, 而是promise, 模板看不到return对象中的属性。(后期也可以返回一个Promise实例,但需要Suspense和异步组件的配合)

2.Ref函数

  • 作用: 定义一个响应式的数据

  • 语法: const xxx = ref(initValue)

    • 创建一个包含响应式数据的引用对象(reference对象,简称ref对象)

    • JS中操作数据: xxx.value

    • 模板中读取数据: 不需要.value,直接:<div>{{xxx}}</div>

  • 备注:

    • 接收的数据可以是:基本类型、也可以是对象类型。

    • 基本类型的数据:响应式依然是靠Object.defineProperty()getset完成的。

    • 对象类型的数据:内部 “ 求助 ” 了Vue3.0中的一个新函数—— reactive函数。

3.Reactive函数

  • 作用: 定义一个对象类型的响应式数据(基本类型不要用它,要用ref函数)

  • 语法:const 代理对象= reactive(源对象)接收一个对象(或数组),返回一个代理对象(Proxy的实例对象,简称proxy对象)

  • reactive定义的响应式数据是“深层次的”。

  • 内部基于 ES6 的 Proxy 实现,通过代理对象操作源对象内部数据进行操作。

4.Vue3.0中的响应式原理

Vue3.0中的响应式原理(第九课)_星辰镜的博客-CSDN博客

vue2.x的响应式

  • 实现原理:

    • 对象类型:通过Object.defineProperty()对属性的读取、修改进行拦截(数据劫持)。

    • 数组类型:通过重写更新数组的一系列方法来实现拦截。(对数组的变更方法进行了包裹)。

      Object.defineProperty(data, 'count', {
          get () {}, 
          set () {}
      })
  • 存在问题:

    • 新增属性、删除属性, 界面不会更新。

    • 直接通过下标修改数组, 界面不会自动更新。

5.reactive对比ref

  • 从定义数据角度对比:

    • ref用来定义:基本类型数据

    • reactive用来定义:对象(或数组)类型数据

    • 备注:ref也可以用来定义对象(或数组)类型数据, 它内部会自动通过reactive转为代理对象

  • 从原理角度对比:

    • ref通过Object.defineProperty()getset来实现响应式(数据劫持)。

    • reactive通过使用Proxy来实现响应式(数据劫持), 并通过Reflect操作源对象内部的数据。

  • 从使用角度对比:

    • ref定义的数据:操作数据需要.value,读取数据时模板中直接读取不需要.value

    • reactive定义的数据:操作数据与读取数据:均不需要.value

6.setup的两个注意点

  • setup执行的时机

    • 在beforeCreate之前执行一次,this是undefined。

  • setup的参数

    • props:值为对象,包含:组件外部传递过来,且组件内部声明接收了的属性。

    • context:上下文对象

      • attrs: 值为对象,包含:组件外部传递过来,但没有在props配置中声明的属性, 相当于 this.$attrs

      • slots: 收到的插槽内容, 相当于 this.$slots

      • emit: 分发自定义事件的函数, 相当于 this.$emit

 

//情况一:监视ref定义的响应式数据
watch(sum,(newValue,oldValue)=>{
	console.log('sum变化了',newValue,oldValue)
},{immediate:true})

//情况二:监视多个ref定义的响应式数据
watch([sum,msg],(newValue,oldValue)=>{
	console.log('sum或msg变化了',newValue,oldValue)
}) 

/* 情况三:监视reactive定义的响应式数据
			若watch监视的是reactive定义的响应式数据,则无法正确获得oldValue!!
			若watch监视的是reactive定义的响应式数据,则强制开启了深度监视 
*/
watch(person,(newValue,oldValue)=>{
	console.log('person变化了',newValue,oldValue)
},{immediate:true,deep:false}) //此处的deep配置不再奏效

//情况四:监视reactive定义的响应式数据中的某个属性
watch(()=>person.job,(newValue,oldValue)=>{
	console.log('person的job变化了',newValue,oldValue)
},{immediate:true,deep:true}) 

//情况五:监视reactive定义的响应式数据中的某些属性
watch([()=>person.job,()=>person.name],(newValue,oldValue)=>{
	console.log('person的job变化了',newValue,oldValue)
},{immediate:true,deep:true})

//特殊情况
watch(()=>person.job,(newValue,oldValue)=>{
    console.log('person的job变化了',newValue,oldValue)
},{deep:true}) //此处由于监视的是reactive素定义的对象中的某个属性,所以deep配置有效

01_Vue3的起步setup函数的使用

<template>
  <div class="app">
    <h1>01_setup函数的使用的基本参数 props context</h1>
    <!-- 自动取出 .vule的值 -->
    <h2>计数器的内容实现:</h2>
    <h3>{{ counter }}</h3>
    <button @click="add">单机+1</button>
    <button @click="del">点击-1</button>
    <!-- 在父组件中定义一个属性 -->
    <showprosshow message="App说我是父组件专递给子组件的信息showprosshow"></showprosshow>
    <!-- 非props的内容-->
    <showprosshow class="我是非porpos的内容信息"></showprosshow>
    <showprosshow @getMessage="showMessage"></showprosshow>
    <ReactiveShow></ReactiveShow>
  </div>
</template>


<script>
import showprosshow from './components/showprosshow.vue'
// ReactiveShow使用
import ReactiveShow from './components/ReactiveShow.vue'
import { ref } from "vue";
export default {
  components: {
    showprosshow,
    ReactiveShow
  },
  // setup函数信息
  setup(props, context) {
    let counter = ref(0);
    const add = () => {
      counter.value++;
      //   counter++;
      console.log(counter);
    };

    const del = () => {
      counter.value--;
      //   counter--;
      console.log(counter);
    };

    const showMessage=(e)=>{
        console.log("showMessage")
        console.log(e)
    }
    return {
      counter,
      add,
      del,
      showMessage
    };
  },
};
</script>



<style scoped>
button {
  margin-left: 10px;
  width: 200px;
  height: 60px;
  line-height: 60px;
  color: red;
  font-size: 20px;
  background-color: azure;
  border: 1px solid royalblue;
}

h1 {
  color: lightcoral;
}

h2 {
  color: cornflowerblue;
}
h3 {
  color: red;
}

h4 {
  color: yellowgreen;
}
</style>
<template>
  <div class="ReactiveShow">
    <h1>ReactiveShow</h1>
    <h2>ReactiveShow计数器的内容实现:</h2>
    <h3>{{ counter }}</h3>
    <button @click="add">单机+1</button>
    <button @click="del">点击-1</button>
    <hr>
    <h1>{{infoObject}}</h1>
    <h2>{{aaccount}}</h2>
    <button @click="updateinfoObject">updateinfoObject</button>
    <hr>
    <h4>{{list}}</h4>
  </div>
</template>


<script>
import { ref,reactive,onMounted } from "vue";
export default {
  name: "ReactiveShow",
  // setup函数信息
  
  setup() {
    let counter = ref(0);
    const add = () => {
      counter.value++;
      //   counter++;
      console.log(counter);
    };
    const del = () => {
      counter.value--;
      //   counter--;
      console.log(counter);
    };
    const showMessage = (e) => {
      console.log("showMessage");
      console.log(e);
    };

    let infoObject =reactive({
        name:"tome",
        age:23,
        height:178

    })

    let aaccount=reactive({
        username:'username_shoput',
        userpawwast:"123456"
    })

    let list=ref([])

    onMounted(()=>{
        const reslist=reactive(["我是元素的AA","我是元素BBB","我是元素CCC","我喜欢睡觉DDD"])
        list.value=reslist
    })
    function updateinfoObject(){
        infoObject.age++
        console.log("updateinfoObject")
    }
    return {
      counter,
      add,
      del,
      showMessage,
      infoObject,
      updateinfoObject,
      aaccount,
      list,
      onMounted
    };
  },
};
</script>



<style scoped>
button {
  margin-left: 10px;
  width: 200px;
  height: 60px;
  line-height: 60px;
  color: red;
  font-size: 20px;
  background-color: rgb(101, 173, 173);
  border: 1px solid royalblue;
}
h4{
    color: red;
}
</style>
<template>
    <div class="showprosshow">
       <h1> showprosshow我们自己编写的页面信息</h1>
       <i><big><h2>{{message}}</h2></big></i>
       <!-- 子组件向父组件专递的信息 -->
       <button @click="setMessage">子——>父</button>

    </div>
</template>


<script>

export default{
    // eslint-disable-next-line vue/multi-word-component-names
    name:'showprosshow',
    props:['message'],
    emits:["getMessage"],
    setup(props,context){
        console.log("在子组件中打印父组件专递过来的内容·")
        console.log(props.message)
        console.log(context.attrs.class)

        // 定义函数
        function setMessage(){
            console.log("将子组件的信息专递给父组件的内容")
            context.emit("getMessage","将信息放回给父组件的内容利用的函数是getMessage子-------->父")

        }

        return{
            setMessage
        }

    }

}
</script>



<style scoped>
button {
  margin-left: 10px;
  width: 200px;
  height: 60px;
  line-height: 60px;
  color: red;
  font-size: 20px;
  background-color: azure;
  border: 1px solid royalblue;
}

h1 {
  color: rgb(0, 255, 119);
}

h2 {
  color: cornflowerblue;
}
h3 {
  color: red;
}

h4 {
  color: yellowgreen;
}
</style>

 02_Vue3Composition基本使用 API

 

<template>
  <div class="app">
    <h2>Vue3 – Composition API 的学习内容介绍</h2>
    <p></p>
    <div>
      <h3>Composition API 的启动学习</h3>
      
      <h2>Composition API 的启动学习 data methods computed watch 声明周期函数</h2>
      <!-- 案例一 @1 SetUp函数的起步 -->
      <div class="count">{{ count }}</div>
      <button @click="add">+1</button>
      <button @click="del">-1</button>
      <p></p>

    </div>
    <!-- 在SetUpProps 定义 message="showinfor" -->
    <SetUpProps message="我是父组件向子组件转送一个消息" class="ShowinforCcontext" age="123"></SetUpProps>
    <p></p> 
    <ReactiveShow></ReactiveShow>
    <p></p>
    <!-- Readonly 的使用 -->
    <readonly></readonly>
    <p></p>
    <WatchInfo></WatchInfo>
    <!--Computed计算机的属性使用  -->
    <ComputedInfo></ComputedInfo>
    <!-- LifeFunction -->
    <LifeFunction></LifeFunction>
    <!-- 注册函数 -->
    <ProvideFunction></ProvideFunction>
    <InjectFunction></InjectFunction>
  </div>
</template>


<script>
import {ref}  from 'vue'
import SetUpProps from './components/SetUpProps.vue'
import ReactiveShow from './components/ReactiveShow.vue'
import readonly from './components/readonly.vue'
import WatchInfo from './components/WatchInfo.vue'
import ComputedInfo from './components/ComputedInfo.vue'
// LifeFunction
import LifeFunction from './components/LifeFunction.vue'
// 
import ProvideFunction from './components/ProvideFunction.vue'
import  InjectFunction from './components/InjectFunction.vue'
export default {
  // setup函数  ReactiveShow
  // RefApl
  setup(props,context) {

    let count = ref(78);
    // console.log(this)
    const add = () => {
      count.value+=3
      console.log(count);
    };

    const del = () => {
      count.value-=2
      console.log(count);
    };

    

    // SetUp函数的返回值 SetUp返回值 对应的Data
    return {
      count,
      add,
      del,
      ReactiveShow,
      

    };
  },

  data() {
    return {
      message: 0,
    };
  },


  // Vue2的写法
  methods: {
    // add() {
      // this.message++;
    // },
    // del() {
      // this.message--;
    // },
  },

  components: {
    SetUpProps,
    ReactiveShow,
    readonly,
    WatchInfo,
    ComputedInfo,
    LifeFunction,
    ProvideFunction,
    InjectFunction,


  },
};
</script>



<style scoped>
h1{
  color: rgb(0, 0, 0);
}
h2{
  color: black;
}
h3{
  color: red;
}
h4{
  color: green;
}
p{
  width: 100%;
  height: 40px;
  background-color: lightskyblue;
  border-radius: 12px;
}
.count{
  width: 200px;
  font-size: 30px;
  line-height: 100px;
  text-align: center;
  height: 100px;
  color: white;
  background-color: rgb(41, 106, 109);
}
</style>
<template>
  <div class="ComputedInfo">
    <h1>ComputedInfo</h1>
    <p><input type="text" name="" id="" v-model="age" /></p>
    <p><input type="text__" name="" id="" v-model="nextAge" /></p>
    <h1>{{ FistName + "______" + LastName }}</h1>
    <!-- 对象的遍历 -->
    <h2>{{ Name.name }}</h2>
    <!-- 数组的遍历 -->
    <h2>{{ Arr }}</h2>
    <h3>{{data}}</h3>
  </div>
</template>


<script>
import { computed, ref } from "vue";
export default {
  setup() {
    const age = ref(89);
    const nextAge = computed(() => {
      return +age.value + 1;
    });

    const LastName = "星辰";

    const FistName = "大海";

    const Name = {
      name: "请输入你的姓名",
      age: "请输入你的年龄",
      height: "请输入你的身高",
      weight: "请输入你的体重",
    };

    let data={
        scor:90
    }

    const Arr = ["1001001", "1001002"];
    return {
      age,
      nextAge,
      FistName,
      LastName,
      Name,
      Arr,
      data
    };
  },
};
</script>



<style scoped>
</style>
<template>
  <div class="LifeFunction">
    <h1>LifeFunction生命周期函数</h1>
  </div>
</template>


<script>
// 导入自己需要的生命周期函数
import {
  beforCreate,
  Created,
  beforeMount,
  mounted,
  onBeforeUpdate,
  updated,
  beforeUnmount,
  unmounted,
  activated,
  deactivated,
} from "vue";

export default {
  setup() {
    // beforCreate(()=>{
        // console.log("123")
    // })
    //   Created(()=>{
        // console.log("123")
    //   })
    //   beforeMount(()=>{
        // console.log("123")
    //   })
    //   mounted(()=>{
        // console.log("123")
    //   })
    //   onBeforeUpdate(()=>{
        // console.log("123")
    //   })
    //   updated(()=>{
        // console.log("123")
    //   })
    //   beforeUnmount(()=>{
        // console.log("123")
    //   })
    //   unmounted(()=>{
        // console.log("123")
    //   })
    //   activated(()=>{
        // console.log("123")
    //   })
    //   deactivated(()=>{
        // console.log("123")
    //   })

    return {
      beforCreate,
      Created,
      beforeMount,
      mounted,
      onBeforeUpdate,
      updated,
      beforeUnmount,
      unmounted,
      activated,
      deactivated,
    };
  },
};
</script>



<style scoped>
</style>
<template>
  <div class="ReactiveShow">
    <h2>ReactiveApl函数的使用</h2>
    <p></p>
    <!-- 函数的响应式原理 -->
    <div>
      <h4>{{ count }}</h4>
      <h1>{{ info }}</h1>

      <button @click="updateinfo">激发函数</button>
      <button @click="info.age++">updateinfo++</button>
      <p></p>
      
      <h1>{{ list }}</h1>
      <p></p>
    </div>
  </div>
</template>


<script>
// 导入自己需要的函数
import { reactive, onMounted, onBeforeMount, readonly } from "vue";
export default {
  name: "ReactiveShow",
  setup() {
    let info = reactive({
      name: "I Like Soo See Week",
      age: 90,
    });

    function updateinfo() {
      console.log(info);
      info.age+=9;
    }

    let list = reactive([]);
    // let list =reactive([])

    onMounted(() => {
      const reslist = ["我叫张飒", "我叫老刘", "我叫ness"];
      list[0] = reslist[0];
    });
    console.log(list);

    // 返回的信息内容
    return {
      reactive,
      info,
      updateinfo,
      list,
    };
  },
};
</script>



<style scoped>
p{
  width: 100%;
  height: 20px;
  background-color: lightblue;
  border-radius: 12px;
}

h1{
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  font-size: 30px;
  color: forestgreen;
}
button{
  width: 200px;
  height: 60px;
  border-radius: 12px;
  color: red;
  background-color: rgb(234, 247, 255);
  font-weight: 900;
}
</style>
<!-- eslint-disable no-undef -->
<template>
  <div class="readonly">
    <h1>我自己认识到的readonly对象函数</h1>
    <p></p>
    <!-- 将info的数据返回出来 -->
    <ReactiveShow :info="info" @getInfo="updateinfo"></ReactiveShow>
    <p></p>
    <!-- 单项数据流 -->
    <!-- <button @click="info.name = '我是修改的数据信息'">修改属性</button> -->
    <button @click="info.name = '我是修改的数据信息'">修改属性</button>
    <p></p>
    <ReactiveShow :infos="infos" @getInfo="updateinfo"></ReactiveShow>
    <p></p>
    <button @click="setMessage">修改属性Readonly</button>
  </div>
</template>


<script>
import ReactiveShow from "./readonlySon.vue";
import { reactive, readonly } from "vue";
export default {
  setup(props, context) {
    let obj = reactive({
      name: "我是Readonly.vue文件",
      age: 89,
      height: 190,
      weight: 89,
    });
    // 修改数据
    const info = readonly(obj);

    // 返回的对象步能修改
    let infos = readonly({
      name: "我是Readonly.vue文件123",
      age: 39,
      height: 190,
      weight: 89,
    });
    function setMessage() {
      context.emit("getInfo","我叫李思打印")
      return{
        setMessage
      }
    }

    return {
      info,
      infos,
      setMessage,
    };
  },
  // eslint-disable-next-line vue/multi-word-component-names
  name: "readonly",
  data() {
    return {
      message: "Hello world vue cli",
    };
  },
  methods: {
    btnclick() {
      console.log("为难忘");
    },
  },
  // 注册组件的内容信息
  components: {
    // eslint-disable-next-line vue/no-unused-components
    ReactiveShow,
  },
};
</script>



<style scoped>
* {
  color: rgb(0, 0, 0);
}
p{
  height: 30px;
  background-color: rgb(175, 242, 180);
  border-radius: 12px;
}
button{
  width: 200px;
  height: 40px;
  font-size: 26px;
  color: red;
}
</style>
<template>
  <div class="readonlySon">
    <h2 @click="btnclick">{{ message }}</h2>
    <p></p>
    <h2>info:{{ info }}</h2>
    <h2>infos:{{ infos }}</h2>
    <p></p>
    <button @click="setinfo">updateinfo</button>
  </div>
</template>


<script>
export default {
  props: ["info","infos"],
  setup(props, context) {
    function setinfo(){
        context.emit("getInfo",'李四')
    }
    

  },
  name: "readonlySon",
  data() {
    return {
      message: "Hello world vue cli",
    };
  },
  methods: {
    btnclick() {
      console.log("为难忘");
    },
  },
};
</script>



<style scoped>
p{
  height: 40px;
  width: 100%;
  background-color: rgb(193, 211, 246);
}
</style>
<template>
  <div class="SetUpProps">
    <h1>@2 SetUp参数的使用 props context</h1>
    <h1>SetUpProps</h1>
    <h1>{{ message }}</h1>
    <h1>SetUp中的Ref</h1>
    <h2 ref="titleref">我是案例信息SetUp中的Ref函数信息</h2>
  </div>
</template>


<script>
import {ref} from 'vue'
export default {
  name: "SetUpProps",
  props: ["message"],
  setup(props, context) {
    // alert("接收父组件的内容信息打印出来");
    console.log(props.message);
    // 所有的非prop的attriblue
    console.log(context.attrs.class);
    console.log(context.attrs.age)
    const titleref=ref(null)
    return {
        titleref
    };
  },
};
</script>



<style scoped>
</style>
<template>
  <div class="WatchInfo">
    <h1>Watch属性的选择</h1>
    <h1>{{ counter }}</h1>
    <button @click="counter++">+1</button>
    <button @click="counter -= 3">+1</button>
    <p></p>
    <h2>{{ info }}</h2>
    <button @click="update">修改</button>
    <button @click="info.friends.name = '我是修改的内容信息'">修改name属性</button>
  </div>
</template>


<script>
import { ref, watch, reactive, watchEffect } from "vue";

export default {
  name: "WatchInfo",
  setup() {
    let counter = ref(0);
    const stop = watchEffect(() => {
      if (counter.value > 10) {
        stop();
      }
    });

    let info = reactive({
      name: "infor",
      age: 89,
      friends: {
        name: "我是李四哈",
        age: 89,
        sex: "男",
      },
    });
    watch(
      info,
      (n, o) => {
        console.log(n, o);
        // console.log(counter)
      },
      {
        deep: true,
        immediate: true,
      }
    );

    watch(counter, (n, o) => {
      console.log(n, o);
      // console.log(counter)
    });

    watch([counter, info], (n, o) => {
      console.log(n, o);
    });

    watchEffect(() => {
      console.log(info, counter.value);
    });

    // watch(()=>{},()=>,{})

    return {
      counter,
      info,
    };
  },
  data() {
    return {
      // eslint-disable-next-line vue/no-dupe-keys
      counter: 0,
    };
  },
  watch: {
    // 用户监听数据
    // counter(n,o){
    // console.log(n,o)
    // }
    // counter:{
    // handlen(a,b){
    // console.log(a,b)
    // },
    // deep:true,
    //
    // }
  },
  methods: {
    btnclick() {
      console.log("为难忘");
    },
  },
};
</script>



<style scoped>
*{
  background-color: rgb(219, 251, 221);
}

p{
  width: 100%;
  height: 50px;
  background-color: rgb(255, 255, 255);
}

</style>
<template>
    <div class="InjectFunction">
        <h1>InjectFunction</h1>
        <h2>{{counter}}</h2>
        <h2>{{infoObject}}</h2>

    </div>
</template>


<script>

import {inject,Provide,props,context} from 'vue'
export default{
    name:'app',
    setup(props,context){
    const car = inject('car')
    return {car}
    }
}
</script>



<style scoped>
*{
    background-color: rgb(198, 241, 255);
}

</style>
<template>
  <div class="app">
    <h1>ProvideFunction</h1>
  </div>
</template>


<script>
import {inject,provide,reactive} from 'vue'
export default {
  name: "app",
  setup(){

    let car = reactive({name:'奔驰',price:'40万'})
    provide('car',car)

  }
};
</script>



<style scoped>
*{
    background-color: rgb(206, 247, 224);
}
</style>

 

 

02-1_Vue3.0对Composion的补充

<template>
  <div class="app">
    <h3 style="color: red">Provide和Inject基本使用</h3>
    <h1 @click="beforeCreate()">主版块的内容信息</h1>
    <home></home>
    <hr />
    <content></content>
  </div>
</template>


<script>
import content from "./components/content.vue";
import home from "./components/home.vue";

export default {
  name: "app",
  data() {
    return {
      message: "Hello world vue cli",
    };
  },

  components: {
    // eslint-disable-next-line vue/no-unused-components
    content,
    // eslint-disable-next-line vue/no-unused-components
    home,
  },
  methods: {
    // 函数模块
    beforeCreate() {
      console.log("初始化事件和生命周期");
      
      
    },
    
    created(){
      console.log("初始化注入&响应性")
    },


    beforMount(){
      confirm.log("是否有template选项")
    },

    mounted(){
      console.log("创建apl$el并增加到el")
    },
    

    // eslint-disable-next-line @typescript-eslint/no-empty-function
    beforeUpdate(){
      console.log("已经挂载当数据发生变化是执行beforeUpdate虚拟Dom")
    },

    updated(){
      console.log("虚拟Dom重新渲染和更新")

    },

    beforeUnmount(){
      console.log("当调用了app.unmount后")
    },

    unmounted(){
      console.log("已经删除")
    }



    

  },
};
</script>



<style scoped>
</style>
<template>
  <div class="content">
    <home></home>、
    {{info}}
  </div>
</template>


<script>
import home from "../components/home.vue";
import {inject} from 'vue'
export default {
  setup(){

    return{

    }
  },
  // eslint-disable-next-line vue/multi-word-component-names
  name: "content",
  data() {
    return {
      message: "Hello world vue cli",
    };
  },
  // 定义对象
  // 在这个组件中定义对象
  provide: {
    name: "李四",
    age: 20,
    height: 123,
    weight: 78,
    email: "2678903458@qq.com",
    qq: "2386754567",
    weixing: "12389999933",
  },
  // 函数的写法

  methods: {
    btnclick() {
      console.log("为难忘");
    
    },
  },
  components: {
    // eslint-disable-next-line vue/no-unused-components
    home,
  },
};
</script>



<style scoped>
</style>
<template>
  <div class="home">
    <content></content>
    <table>
      <h6>
        在home.vue组件中和content组件中利用 provide定义对象 利用 inject取出对象
      </h6>
      <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>身高</th>
        <th>体重</th>
        <th>邮箱</th>
        <th>qq</th>
        <th>微信</th>
      </tr>
      <tr>
        <td>
          <span>{{ name }}</span>
        </td>
        <td>
          <span>{{ age }}</span>
        </td>
        <td>
          <span>{{ height }}</span>
        </td>
        <td>
          <span>{{ weight }}</span>
        </td>
        <td>
          <span>{{ email }}</span>
        </td>
        <td>
          <span>{{ qq }}</span>
        </td>
        <td>
          <span>{{ weixing }}</span>
        </td>
      </tr>
    </table>
  </div>
</template>
<script>
import content from "../components/content.vue";
// 导入
import { provide } from "vue";
// home 与content组件为兄弟元素
export default {
  setup() {
    return {};
  },

  // eslint-disable-next-line vue/multi-word-component-names
  name: "home",
  data() {
    return {
      message: "Hello world vue cli",
    };
  },
  components: {
    // eslint-disable-next-line vue/no-unused-components
    content,
  },
  //   取值
  inject: ["name", "age", "height", "weight", "email", "qq", "weixing"],

  methods: {
    btnclick() {
      console.log("为难忘");
    },
  },
};
</script>



<style scoped>
.home {
  background-color: rgb(214, 248, 177);
  display: flex;
}
.home span {
  font-size: 20px;
  background-color: rgb(236, 253, 239);
  color: red;
  flex: 1;
}
table {
  width: 100%;
  background-color: rgb(235, 255, 153);
  color: rgb(255, 0, 0);
  font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
}

td {
  width: 18%;
  border-radius: 10px;
}
td {
  text-align: center;
  line-height: 60px;
  height: 60px;
  border: 3px solid green;
}
</style>

 

03_Vue3函数封装的思想

<template>
  <div class="app">
    appup
    <h1>Abb:{{ count }}</h1>
    <button @click="add">+1</button>
    <button @click="del">-1</button>
    <aboutconunter></aboutconunter>
    <Homeconunter></Homeconunter>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
</template>


<script>
import aboutconunter from "./components/about-conunter.vue";
import Homeconunter from "./components/Home-conunter.vue";
import UserTitle from './hook/usertitle'
import userScollPerson from './hook/userScoll'
import { ref } from "vue";
export default {
  setup() {
    let count = ref(10);
    function add() {
        count.value++
    }
    function del() {
        count.value--
    }
    UserTitle("我是App页面信息")

    userScollPerson()
    return {
      count,
      UserTitle,
      add,
      del,
      userScollPerson
    };
  },
  name: "app",
  data() {
    return {
      message: "Hello world vue cli",
    };
  },
  methods: {
    btnclick() {
      console.log("为难忘");
    },
  },
  components: {
    // eslint-disable-next-line vue/no-unused-components
    aboutconunter,
    Homeconunter,
  },
};
</script>



<style scoped>
*{
    background-color: aliceblue;
}
button {
  width: 100px;
  height: 60px;
  color: red;
  font-weight: 900;
  background-color: green;
  margin-left: 40%;
}
</style>
<template>
  <div class="about-conunter">
    about-conunter
    <h1>{{ count }}</h1>
    <button @click="add">+1</button>
    <button @click="del">-1</button>
  </div>
</template>


<script>

import userCount from "../hook/usercount";
export default {
  setup() {
    return {
      ...userCount(),
      
    };
    
  },
};
</script>



<style scoped>
*{
    background-color: coral;
}
button {
  width: 100px;
  height: 60px;
  color: red;
  font-weight: 900;
  margin-left: 20%;
}
</style>
<template>
  <div class="Home-conunter">
    Home-conunter
    <h1>{{ count }}</h1>
    <button @click="add">+1</button>
    <button @click="del">-1</button>
  </div>
</template>

<script>

import { ref } from "vue";
import useCounter from '../hook/usercount';
export default {
  setup() {
  
    let {count,add,del} =useCounter()
    return {
      count,
      add,
      del,
    };
  },
  name: "app",
  data() {
    return {
      message: "Hello world vue cli",
    };
  },
  methods: {
    btnclick() {
      console.log("为难忘");
    },
  },
  components: {
    // eslint-disable-next-line vue/no-unused-components

  
  },
};
</script>



<style scoped>
button {
  width: 100px;
  height: 60px;
  color: red;
  font-weight: 900;
  background-color: black;
  margin-left: 30%;
}
</style>
import { ref } from "vue";
export default function useCounter(){
    let count = ref(10);
    function add() {
        count.value++
    }
    function del() {
        count.value--
    }

    return{
        count,add,del
    }

}

 

04_ReactivApl基本使用

 

<template>
  <div class="app">
    认识Composition 中的Reactive函数
    <h1>{{ counter }}</h1>
    <button @click="add">《+1》</button>
    <button @click="del">《-1》</button>
    <button>修改人的信息内容</button>
    <button @click="addcolor">Vue3增加属性</button>
    <h1>一个人的信息</h1>

    <button @click="setHellow">Vue-3说话</button>
    <button @click="say">Vue-2说话</button>
    <h1 @click="changeinfo">我是setup函数的内容信息</h1>
    <h2>姓名:{{ name }}</h2>
    <h2>年龄:{{ age }}</h2>
    <h2>性别:{{ sex }}</h2>
    <h3>工作的种类信息</h3>
    <h4>{{ job.id }}</h4>
    <h4>{{ job.type }}</h4>
    <h4>{{ job.salary }}</h4>
    <h1>{{ job.a.b.c.d }}</h1>
    <h1>{{ hoby }}</h1>
  </div>
</template>
  
  
  <script>
// 导入包名ref
// reference implement
import { ref, reactive } from "vue";
export default {
  // 测试
  setup() {
    // 数据

    // get set

    let name = ref("张三");
    let age = ref(89);
    let sex = ref("女");
    let job = reactive({
      id: "1009001",
      type: "后端开发工程师",
      salary: "50k",
      a: {
        b: {
          c: {
            d: 66666,
          },
        },
      },
    });

    let hoby = reactive(["1111", "22222", "333333"]);
    console.log(name);
    console.log(age);
    console.log(sex);

    function changeinfo() {
      alert("开始修改");
      name.value = "李四";
      age.value = 24;
      sex.value = "女";
      //console.log(name);
      //console.log(age);
      console.log(job);
      //job.value.type="后端开发"
      job.a.b.c.d = 9999;
      //console.log(job.value)
      hoby[0] = 90;
    }

    function addcolor(){
      job.color="blue"
    }

    // 方法名
    function setHellow() {
      // alert(`我叫 ${name},我${age}岁呀!你好呀`)
    }

    // 定义变量counter
    let counter = ref(0);
    let add = () => {
      counter.value++;
      console.log(counter);
      // console.log("add");
    };
    let del = () => {
      counter.value--;
      // console.log(counter)
      // console.log("del");
    };

    // 返回的是对象
    return {
      counter,
      hoby,
      job,
      add,
      del,
      name,
      age,
      sex,
      addcolor,
      setHellow,
      changeinfo,
    };
    // 返回函数为
    // return ()=>{return h('h1','上学')}
  },

  data() {
    // return {
    // sex: "男",
    // };
  },
  methods: {
    say() {
      alert("欢迎来到setup函数的学习内容");
    },
  },
};
</script>
  
  
  
  <style scoped>
</style>
<template>
  <div class="app">
    <h1>我们自己编写的页面信息</h1>
    <h2 @click="btnclick">{{ message }}</h2>
    <!-- vue 2响应式原理 -->
    <ul>
      <li v-show="person.name">{{ person.name }}</li>
      <li>{{ person.age }}</li>
      <li>{{ person.sex }}</li>
      <li>{{ person.height }}</li>
      <li>{{ person.weight }}</li>
      <li v-show="person.like">{{ 爱好 }}</li>
    </ul>

    <button @click="addsex">增加属性</button>
    <button @click="delname">删除属性</button>

    <!-- Vue三 的响应式原理 -->
  </div>
</template>


<script>
import Vue from "vue";
export default {
  name: "app",
  data() {
    return {
      message: "Hello world vue cli",

      person: {
        name: "张三",
        age: 34,
        sex: "男",
        height: "190",
        weight: 56,
      },
    };
  },
  methods: {
    addsex() {
      console.log(this.person.like);
      this.person.like = "网站血虚";
      console.log(this.person.like);
      // Vue.$set(this.person,'like','王者荣耀')
    },
    delname(){
      console.log(this.person.name);
      delete   this.person.name 
      console.log(this.person.name);
      // Vue.$set(this.person,'like','王者荣耀')
    },
  },
};
</script>



<style scoped>
</style>

 

05_SetUp函数的语法糖的写法

<template>
    <div class="app">
       <h1> 05_SetUp函数的语法糖的写法</h1>
       <h2>{{message}}</h2>
       <button @click="updateMessage">修改message的基本信息</button>
        <Showinfo></Showinfo>
    </div>
</template>


<script setup>
// 导入文件包名
import {ref} from 'vue'
import Showinfo from './components/Showinfo.vue'
    let message=ref("Hello SetUp")

    function updateMessage(){
        message.value='SetUp Hello'
    }


</script>



<style scoped>

</style>
<template>
    <div class="Showinfo">
        <h1>Showinfo</h1>

    </div>
</template>


<script>

export default{
    // eslint-disable-next-line vue/multi-word-component-names
    name:'Showinfo',
    data(){
        return{
            message:"Hello world vue cli"
        }
    },
    methods:{
        btnclick(){
            console.log("为难忘")
        }
    }

}
</script>



<style scoped>

</style>

import { createApp } from 'vue'
// import App from './App.vue'

// import App from './01_Vue3的起步setup函数的使用/App.vue'

// import App from './02-1_Vue3.0对Composion的补充/App.vue'

// import App from './02_Vue3Composition基本使用 API/App.vue'

// import App from './03_Vue3函数封装的思想/App.vue'


// import App from './04_ReactivApl基本使用/App.vue'


import App from './05_SetUp函数的语法糖的写法/App.vue'

// import App from './06_案例实战/App.vue'



createApp(App).mount('#app')
 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/40875.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

kubernetes工作负载之控制器

目录 ​一、概述 二、Deployment 控制器 2.1Deployment 部署应用 2.2Deployment滚动升级 2.2.1应用部署完成 2.2.2更新镜像三种方式 2.3 Deployment 发布失败回滚 2.4Deployment 水平扩容 三、DaemonSet控制器 四、Job控制器 4.1Job一次性执行 4.2定时任务&#xf…

查询:按A分组,满足B时对应的C

1.场景 这种问题我自己归纳为“找对应行”问题&#xff0c;例如有下面一场表&#xff08;学生做题&#xff0c;对每个知识点的得分情况&#xff09; 字段&#xff1a;主键id、user_id、score、is_study、knowledgeName、updateTime场景1&#xff1a;按用户分组&#xff0c;求…

Nginx (7):nginx高可用配置

所谓的高可用&#xff0c;就是虽然nginx可以反向代理&#xff0c;如果某个内部服务器down了&#xff0c;可以使用其他的内部服务器&#xff0c;然而万一nginx挂了呢&#xff1f;&#xff1f;&#xff1f;&#xff1f;布置多个nginx再反向代理nginx&#xff1f;&#xff1f;反向…

数据结构学习笔记(V):树与二叉树

目录 1 树 1.1 树的定义和基本术语 1.定义 2.基本术语 1.2 树的性质 2 二叉树 2.1 二叉树的定义和基本术语 1.定义 2.特殊二叉树 2.2 二叉树性质 2.3 二叉树存储结构 1.顺序存储 2.链式存储 3 二叉树进阶 3.1 二叉树顺序遍历 1.先序遍历 2.中序遍历 3.后序遍…

第十二周总结

这周我来总结一下数论分块和佩尔方程&#xff1a; 已知正整数n&#xff0c;求&#xff0c;对n/i下取整&#xff0c;相当于把一组数分块了&#xff0c;首先我们来找一下规律&#xff1a;n20时 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 …

现代密码学导论-20-流密码

目录 3.6 实际操作和加密方式 3.6.1 流密码 CONSTRUCTION 3.30 使用伪随机函数构造流密码 3.6.2 流密码的同步模式 CONSTRUCTION 3.31 流密码的异步模式 3.6 实际操作和加密方式 现代密码学导论-14-基于伪随机发生器的EAV安全_南鸢北折的博客-CSDN博客 CONSTRUCTION 3.17…

Spring Cloud整合Nacos集群

目录 第一章 微服务架构图 第二章 Spring Cloud整合Nacos集群 第三章 Spring Cloud GateWay 第四章 Spring Cloud Alibaba 整合Sentinel 第五章 Spring Cloud Alibaba 整合SkyWalking链路跟踪 第六章 Spring Cloud Alibaba 整合Seata分布式事务 第七章 Spring Cloud 集成Auth用…

计量数据分析数据库-Stata分析包使用指南、计量分析资料等八大数据大全

一、计量前沿stata 分析包使用指南 当考虑自变量X对因变量Y的影响时&#xff0c;如果X通过影响变量M来影响Y,则称M为中介变量&#xff08;mediator或mediating variable) (Judd & Kenny, 1981; Baron &Kenny,1986)。X通过中介变量M对Y产生的影响就是中介效应&#xff…

【深入理解C++】可调用对象、std::function、std::bind()

文章目录1.可调用对象1.1 函数指针1.2 具有operator()成员函数的类对象/仿函数/函数对象1.3 可被转换为函数指针的类对象1.4 类成员函数指针2.std::function2.1 绑定普通函数2.2 绑定类的静态成员函数2.3 绑定具有operator()成员函数的类对象2.4 绑定可被转换为函数指针的类对象…

【无标题】SAR雷达系统反设计及典型目标建模与仿真实现研究——目标生成与检测(Matlab代码实现)

&#x1f352;&#x1f352;&#x1f352;欢迎关注&#x1f308;&#x1f308;&#x1f308; &#x1f4dd;个人主页&#xff1a;我爱Matlab &#x1f44d;点赞➕评论➕收藏 养成习惯&#xff08;一键三连&#xff09;&#x1f33b;&#x1f33b;&#x1f33b; &#x1f34c;希…

408考研科目《数据结构》第八章第一节:排序的基本概念和插入排序(直接插入排序,折半插入排序,希尔排序)

文章目录教程1.排序的基本概念1.1 排序算法的评价指标1.2 排序算法的分类2. 插入排序2.1 直接插入排序2.1.1 算法效率分析2.2 折半插入排序总结2.3 希尔排序 &#xff08;Shell Sort&#xff09;总结教程 排序&#xff1a; https://www.bilibili.com/video/BV1b7411N798/?p77…

队列——算法专项刷题(七)

七、队列 常用于辅助遍历树&#xff0c;设计队列先进先出特性的数据结构 7.1 滑动窗口的平均值 给定一个整数数据流和一个窗口大小&#xff0c;根据该滑动窗口的大小&#xff0c;计算滑动窗口里所有数字的平均值。 实现 MovingAverage 类&#xff1a; MovingAverage(int si…

图书管理系(附源码PPT)

图书管理系统1 绪 论1.1 研究背景1.2 研究意义1.3 相关研究现状1.3.1 高校图书管理面临的问题1.3.2 信息化为图书管理带来新变化2 相关技术2.1 JSP 概述2.2 MySQL 数据库技术2.3 Spring2.4 SpringMVC2.5 Dbcp2.6 Maven3 系统分析3.1 需求分析3.1.1 系统的功能需求分析3.1.2 系统…

利用衍射表面消色差的混合目镜建模

1. 摘要 同时具有折射和衍射表面的混合透镜已成为一种极具潜力的解决方案应用于多种领域。在此案例中&#xff0c;我们将演示混合目镜的一个例子&#xff0c;其中利用衍射透镜表面对色差进行了校正。由ZemaxOpticStudio进行初始化设计&#xff0c;并导入VirtualLab Fusion进行进…

TensorRt(2)快速入门介绍

文章目录1、使用ONNX部署的示例1.1、导出模型1.2、设置batch size批处理大小1.3、指定数值精度1.4、转换模型1.5、部署模型2、使用ONNX转换为engine再部署的示例2.1、导出ONNX模型2.1.1、从TensorFlow中导出ONNX2.1.1、从PyTorch中导出ONNX2.2、ONNX 转化为 TensorRT Engine2.3…

27个超实用Chrome控制台调试技巧 Source 全局搜索(关注更新)

谷歌开发者工具提供了一系列的功能来帮助开发者高效 Debug 网页应用&#xff0c;让他们可以更快地查找和修复 bug。在谷歌的开发者工具中&#xff0c;有非常多有用的小工具&#xff0c;但是很多开发者并不知道。通过这篇文章&#xff0c;我把我常用的那些高效 Debug 的 Chrome …

ARM 汇编指令集1_2

一、两个概念&#xff1a;指令与伪指令 &#xff08;汇编&#xff09;指令&#xff0c;是 CPU 机器指令的助记符&#xff0c;经过编译后会得到一串10组成的机器码&#xff0c;可以由 CPU 读取执行。&#xff08;汇编&#xff09;伪指令&#xff0c;本质上不是指令&#xff08;…

版本控制利器——changelog

问题描述 当前&#xff0c;我们项目需要进行版本的确定&#xff0c;人工审核代码已接近尾声&#xff0c;但为了防止后续继续出现该问题&#xff0c;我希望能够做到在每次push到master时&#xff0c;更新changelog 将每一个版本的commit记录下来&#xff0c;类似于下列 解决…

K8s 集成 Jenkins 部署Go Web项目

风离不摆烂学习日志 Day9 K8s 集成 Jenkins 部署Go Web项目 Dockerfile FROM golang:alpine as builder # 需要go环境 MAINTAINER fl1906WORKDIR /work # 源 RUN go env -w GOPROXYhttps://goproxy.cn && go env -w CGO_ENABLED0 && go env -w GO111MODULEon C…

Splunk UBA 从 Ldap 成功导入 HR 数据

1: 今天在配置Splunk UBA 的HRdata 和asset data 的时候,都要用到ldap, 所以还非要装add-on:\ add-on 的名字: Splunk Supporting Add-on for Active Directory (SA-LDAPSearch) (SA-LDAPSearch) 这个add-on 的配置: Configure the Splunk Supporting Add-on for Activ…