Vue3-js 學習筆記


Vue3-js 學習筆記

目錄

前言

優秀的教程

reactive 數據綁定

<template>
  <div class="app">
    <h1>{{ state.name }}</h1>
  </div>
</template>

<script>
  import { reactive } from "vue";
  export default {
    name: "App",
    setup() {
      const state = reactive({
        name: "朱大常",
      });

      return { state };
    },
  };
</script>

事件綁定

<template>
  <div class="app">
    <h1>{{ state.name }}</h1>
    <button type="button" @click="login">按鈕</button>
  </div>
</template>

<script>
  import { reactive } from "vue";
  export default {
    name: "App",
    setup() {
      // 數據定義
      const state = reactive({
        name: "朱大常",
      });
      // 事件定義
      const login = () => {
        state.name = "朱大常001";
      };
      return { state, login };
    },
  };
</script>

生命函數周期

onMounted

<template>
  <div class="app"></div>
</template>

<script>
  import { onMounted } from "vue";
  export default {
    name: "App",
    setup() {
      // 組件加載
      onMounted(() => {
        console.log("組件已掛載");
      });
    },
  };
</script>

計算屬性-computed

computed (只要監聽的那個數據改變,就會重新計算)

<template>
  <div class="app">
    <h1>{{ state.name }}</h1>
    <h2>歲數:{{ state.age }}</h2>
    <h3>計算后的屬性:{{ state.setComputed }}</h3>

    <input type="text" v-model="state.username" />
    <button type="button" @click="login">按鈕</button>
  </div>
</template>

<script>
  import { reactive, computed } from "vue";
  export default {
    name: "App",
    setup() {
      // 數據定義
      const state = reactive({
        name: "朱大常",
        username: "",
        age: 18,
        setComputed: computed(() => state.age + 2),
      });
      // 事件定義
      const login = () => {
        state.name = state.username;
      };
      setTimeout(() => {
        state.age = 19;
      }, 2000);
      return { state, login };
    },
  };
</script>

props

<!-- 子組件 -->
<template>
  <h1>子組件:{{ title }}</h1>
</template>

<script>
  import { onMounted } from "vue";
  export default {
    name: "test",
    props: {
      title: {
        type: String,
        default: "",
      },
    },
    setup(props) {
      onMounted(() => {
        console.log("----組件加載完成-props值為", props.title);
      });
    },
  };
</script>

<!-- 父組件 -->
<template>
  <div class="app">
    <test :title="state.title"></test>
  </div>
</template>

<script>
  import { reactive } from "vue";
  import test from "./components/test";
  export default {
    name: "App",
    components: {
      test,
    },
    setup(props) {
      // 數據定義
      const state = reactive({
        title: "朱大常",
      });
      return { state };
    },
  };
</script>

emit-自定義事件

emit 傳值-注意這里的事件生命使用小寫加-

<!-- 子組件 -->
<template>
  <button type="button" @click="handleClick">emit按鈕</button>
</template>

<script>
  import { onMounted } from "vue";
  export default {
    name: "test",
    setup({ emit }) {
      const handleClick = () => {
        emit("my-event", "hello");
      };
      return { handleClick };
    },
  };
</script>

<!-- 父組件 -->
<template>
  <div class="app">
    <test @my-event="parentClick"></test>
  </div>
</template>

<script>
  import { reactive } from "vue";
  import test from "./components/test";
  export default {
    name: "App",
    components: {
      test,
    },
    setup(props) {
      // 數據定義
      const state = reactive({
        name: "朱大常",
      });

      const parentClick = (e) => {
        console.log(e);
      };

      return { parentClick };
    },
  };
</script>

ref-獲取元素及子組件

ref 注意-相比 vue2.x 有很大的改變--ref 和 reactive 差別-ref 線

<!-- 子組件 -->
<template>
  <div></div>
</template>

<script>
  import { ref } from "vue";
  export default {
    name: "test",
    setup() {
      // 1. 定義響應式的數據
      const count = ref(1);

      // 2. 定義方法
      const refsgh = () => {
        console.log("-----k");
      };
      return { count, refsgh };
    },
  };
</script>

<!-- 父組件 -->
<template>
  <div class="app">
    <!-- !!!!!!!!!注意ref 必須是駝峰!!!!! -->
    <test ref="comRef"></test>
    <button @click="clickClid001">獲取子組成數據</button>
    <button @click="clickClid002">調用子組件方法</button>
  </div>
</template>

<script>
  import { ref } from "vue";
  import test from "./components/test";
  export default {
    name: "App",
    components: {
      test,
    },
    setup() {
      // 1. 創建一個組件的 ref 引用
      const comRef = ref(null);

      // 2. 獲取子組成的值
      const clickClid001 = () => {
        console.log(comRef.value.count);
      };

      // 3. 調用子組件的方法
      const clickClid002 = () => {
        comRef.value.refsgh();
      };
      return { comRef, clickClid001, clickClid002 };
    },
  };
</script>

watch

<template>
  <div class="app">
    <button @click="watch001">watch方法</button>
    <!-- 這里.value可以省略 -->
    <h4>ref值:{{ nameref }}</h4>
  </div>
</template>

<script>
  import { watch } from "vue";
  export default {
    name: "App",
    setup() {
      const nameref = ref("朱大常");
      watch(nameref, (newValue, oldValue) => {
        // 輸出 前端有的玩 子君
        console.log(newValue, oldValue);
      });

      const watch001 = () => {
        nameref.value = "張德帥";
      };
      return {
        watch001,
        nameref,
      };
    },
  };
</script>

vue3-組件通信

父組件獲取子組件數據和調用子組件方法

  • props-破軟絲 (傳值)
  • ref-瑞府 (調用子組件的方法)
<!-- 子組件 -->
<template>
  <h1>{{ title }}</h1>
</template>

<script>
  import { ref } from "vue";
  export default {
    name: "menus",
    props: {
      title: {
        type: String,
        default: "menus",
      },
    },
    setup() {
      // 1. 定義響應式的數據
      const count = ref(1);

      // 2. 定義方法
      const clidFun = () => {
        console.log("-----k");
      };
      return { count, clidFun };
    },
  };
</script>

<!-- 父組件 -->
<template>
  <div class="app">
    <menus ref="menusRef" :title="state.title"></menus>
    <button @click="clickClid001">ref獲取子組件數據</button>
    <button @click="clickClid002">ref調用子組件方法</button>
  </div>
</template>

<script>
  import { ref, reactive } from "vue";
  import menus from "./components/menus";
  export default {
    name: "App",
    components: {
      menus,
    },
    setup() {
      const state = reactive({
        title: "張德帥",
      });
      // 1. 創建一個組件的 ref 引用
      const menusRef = ref(null);

      // 2. 獲取子組成的值
      const clickClid001 = () => {
        console.log(menusRef.value.count);
      };

      // 3. 調用子組件的方法
      const clickClid002 = () => {
        menusRef.value.clidFun();
      };

      return { state, menusRef, clickClid001, clickClid002 };
    },
  };
</script>

子組件通信父組件

  • props
  • emit
<!-- 子組件 -->
<template>
  <h1>{{ title }}</h1>
  <button type="button" @click="getPropsState">props獲取父組件數據</button
  ><br />
  <button type="button" @click="getEmitState">emit調用父組件方法</button>
  <br />
  <button type="button" @click="parentClik">$parent調用父組件方法</button>
</template>

<script>
  export default {
    name: "test001",
    props: {
      title: {
        type: String,
        default: "",
      },
      appFun: {
        type: Function,
        default: () => {},
      },
    },
    setup(props, { emit }) {
      // 1.props獲取父組件數據
      const getPropsState = () => {
        props.appFun();
      };

      // 2.emit調用父組件方法
      const getEmitState = () => {
        emit("my-even");
      };

      return { getPropsState, getEmitState };
    },
  };
</script>

<style lang="scss" scoped></style>

<!-- 父組件 -->
<template>
  <div>
    <h1>子組件</h1>
    <test01 :title="state.title" :appFun="appFun" @my-even="clidFun"></test01>
  </div>
</template>

<script>
  import { reactive } from "vue";
  import test01 from "./components/test001";
  export default {
    name: "App",
    components: {
      test01,
    },
    setup() {
      const state = reactive({
        title: "子組件",
      });

      // props 方法
      const appFun = () => {
        console.log("--------P");
      };

      // emit 自定義事件
      const clidFun = () => {
        console.log("父組件方法被調用");
      };

      return { state, appFun, clidFun };
    },
  };
</script>

<style lang="scss" scoped></style>

reactive-ref-區別


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM