>  기사  >  웹 프론트엔드  >  [편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

青灯夜游
青灯夜游앞으로
2022-08-30 20:20:512965검색

이 기사는 Vue3 학습 노트를 요약하고 공유하며 Vue3의 11가지 지식 포인트에 대한 심층적인 이해를 제공합니다. 모두에게 도움이 되기를 바랍니다!

[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

Vue3+Node+Webpack+API Mall 프로젝트 엔지니어링 실무개발 강좌!

1. CompositionAPI를 선택하는 이유

Vue2의 제한 사항

  • 컴포넌트 로직 확장으로 인한 가독성 저하
  • 컴포넌트 간 코드 재사용 불가
  • Vue2는 TS에 대한 지원이 제한됨

기존 OptionsAPI에서는 로직을 다음 6개 부분으로 나누어야 합니다. [관련 권장 사항: vue.js 비디오 튜토리얼]

OptionsAPI

  • 컴포넌트components
  • props
  • data
  • computed
  • methods
  • lifecycle methods
  • props

data

계산

메서드

수명 주기 메서드

CompositionAPI를 사용하여 문제를 해결하는 방법

가장 좋은 해결책은 logic 코드를 매우 읽기 쉽게 만들 수 있습니다.

이것이 우리의 CompositionAPI 구문이 달성할 수 있는 것입니다. CompositionAPI는 완전히 선택적인 구문이며 원래 OptionAPI와 충돌하지 않습니다. optionsAPI[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

    코드 재사용 방법 PK
  • Vue2의 교차 컴포넌트 재사용 코드에는 약 4가지 선택이 있습니다
  • 1.

    코드 믹싱은 사실 디자인 모드에서의 믹싱 모드인데, 단점도 매우 뚜렷합니다.
  • 간단히 말하면 아버지가 둘인 셈이다

단점

속성 이름 충돌을 피할 수 없음[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

상속 관계가 명확하지 않음

2. - Mixin Factory

Return one

✅코드 재사용이 편리함

✅상속 관계 정리

3.ScopeSlots - 스코프 슬롯

[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명❌가독성이 좋지 않음

❌구성이 복잡합니다. template

❌낮은 성능 - 각 슬롯은 인스턴스와 동일합니다

4. CompositionApi - 복합 API

✅코드 양 적음✅새로운 구문이 도입되지 않고 단순한 함수임

✅매우 유연함

✅도구 친화적인 구문 프롬프트 - 간단한 기능이므로 구문 프롬프트 및 자동 보정 구현이 쉽습니다

2. 설정 및 참조

CompositionAPI를 사용하는 이유

✅더 나은 Typescript 지원 ✅복잡한 기능 구성요소에서 코드는 특성에 따라 구성될 수 있습니다. 예:

정렬 및 검색 논리 응집력
  • ✅구성요소 간 코드 재사용
    • 설정이란 무엇입니까
    • 다음 이전에 실행됨 메서드:
    • Components
    • Props
  • Data
  • Methods
    • 계산된 속성
    • Lifecycle 메서드
이해하기 어려운
    두 개의 선택적 매개변수가 있습니다
  • props - 속성( 반응형 객체이며 들을 수 있습니다(감시))

import {watch} from "vue"
export defalut {
	props: {
		name: String
	},
	setup(props) {
		watch(() => {
			console.log(props.name)
		})
	}
}
컨텍스트 컨텍스트 객체 - 이전 이 메소드로 액세스할 수 있는 속성을 대체하는 데 사용됩니다.

setup (props,context) {
	const {attrs,slots,parent,root,emit} = context
}

ref란 무엇입니까?

기본 데이터 유형 데이터를 설치하려면 Box 작업을 통해 데이터 변경 사항을 추적할 수 있는 반응형 개체로 만듭니다. [편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

🎜🎜요약🎜🎜🎜🎜🎜🎜유지관리성이 대폭 향상되었습니다🎜
  • 可以控制哪些变量暴露
  • 可以跟中哪些属性被定义 (属性继承与引用透明)

三、Methods

基础用法

[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

自动拆装箱总结

[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

  • JS :需要通过.value访问包装对象
  • 模板: 自动拆箱

四、 Computed - 计算属性

这个地方实在没什么好讲的,和Vue2没变化

<template>
  <div>
    <div>Capacity: {{ capacity }}</div>
    <p>Spases Left: {{ sapcesLeft }} out of {{ capacity }}</p>
    <button @click="increaseCapacity()">Increase Capacity</button>
  </div>
</template>

<script>

import { ref, computed, watch } from "vue";
export default {
  setup(props, context) {
    const capacity = ref(3);
    const attending = ref(["Tim", "Bob", "Joe"]);
    function increaseCapacity() {
      capacity.value++;
    }
    const sapcesLeft = computed(() => {
      return capacity.value - attending.value.length;
    });
    return { capacity, increaseCapacity, attending, sapcesLeft };
  },
};
</script>

五、Reactive - 响应式语法

之前reactive 的 Ref 去声明所有的响应式属性

import { ref,computed } from &#39;vue&#39;
export default {
  setup(){
    const capacity = ref(4);
    const attending = ref(["Tim","Bob","Joe"]);
    const spacesLeft = computed(()=>{
      return capacity.value - attending.value.length
    })
    function increaseCapacity(){ capacity.value ++;}
    return { capacity,increaseCapacity,attending,spacesLeft}
  }
}

但是有另一个等效的方法用它去代替 reactive 的Ref

import { reactive,computed } from &#39;vue&#39;
export default {
  setup(){
    const event = reactive({
      capacity:4,
      attending:["Tim","Bob","Joe"],
      spacesLeft:computed(()=>{
        return event.capacity - event.attending.length;
      })
    })
  }
}

过去我们用vue2.0的data来声明响应式对象,但是现在在这里每一个属性都是响应式的包括computed 计算属性

这2种方式相比于第一种没有使用.

接下来 我们再声明method 这2种语法都ok,取决于你选择哪一种

setup(){
  const event = reactive(){
    capacity:4,
    attending:["Tim","Bob","Joe"],
    spacesLeft:computed(()=>{
      return event.capacity - event.attending.length;
    })
    function increaseCapacity(){event.capacity++}
    //return整个对象
    return {event,increaseCapacity}
  }
}
<p>Spaces Left:{{event.spacesLeft}} out of {{event.capacity}}</p>
<h2>Attending</h2>
<ul>>
	<li v-for="(name,index) in event.attending" :key="index">
     {{name}}
  </li>
</ul>
<button @click="increaseCapacity()"> Increase Capacity</button>

在这里我们使用对象都是.属性的方式,但是如果 这个结构变化了,event分开了编程了一个个片段,这个时候就不能用.属性的方式了

//在这里可以使用toRefs
import {reactive,computed,toRefs} from &#39;vue&#39;
export default{
  setup(){
    const event = reactive({
      capacity:4,
      attending:["Tim","Bob","Joe"],
      spacesLeft:computed(()=>{
        return event.capacity -event.attending.length;
        
      })
    })
    function increaseCapacity(){ event.capacity ++ }
    return {...toRefs(event),increaseCapacity}
  }
}

如果没有 increaseCapacity() 这个方法 直接可以简化为

return toRefs(event)

完整代码

<div>
   <p>Space Left : {{event.spacesLeft}} out of {{event.capacity}} </p>
   <h2>Attending</h2>
   <ul>
      <li v-for="(name,index)" in event.attending :key="index">{{name}}
      </li>


     
   </ul>
   <button @click="increaseCapacity">Increase Capacity</button>
   </div>
</template>

<script>
//第一种
import {ref,computed } from &#39;vue&#39;
export default {
  setup(){
    const capacity = ref(4)
    const attending = ref(["Tim","Bob","Joe"])
    const spaceLeft = computed(()=>{
      return capacity.value - attending.value.length;
    });
    function increaseCapacity(){ capacity.value++; }
    return {capacity,increaseCapacity,attending,spaceLeft}   


  }
} 

//返回一个响应式函数 第二种
import { reactive,computed } from &#39;vue&#39;
export default {
  setup(){
    const event = reactive({
      capacity:4,
      attending:["Tim","Bob","Joe"],
      spaceLeft:computed(()=>{
        return event.capacity - event.attending.length;
      })
    })
    //我们不再使用.value
    function increaseCapacity() { event.capacity++; }
    //把这个event放入到template中
    return { event,increaseCapacity}
  }
}


</script>

六、 Modularizing

使用CompositionAPI的两个理由

1、可以按照功能组织代码

[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

2、组件间功能代码复用

[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

七、 LifecycleHooks - 生命周期钩子

Vue2 Vue3
beforeCreate ❌setup(替代)
created ❌setup(替代)
beforeMount onBeforeMount
mounted onMounted
beforeUpdate onBeforeUpdate
updated onUpdated
beforeDestroy onBeforeUnmount
destroyed onUnmounted
errorCaptured onErrorCaptured
- ?onRenderTracked
- ?onRenderTriggered

setup中调用生命周期钩子

import { onBeforeMount,onMounted } from "vue";
export default {
  setup() {
    onBeforeMount(() => {
        console.log(&#39;Before Mount!&#39;)
    }) 
    onMounted(() => {
        console.log(&#39;Before Mount!&#39;)
    }) 
  },
};

八、Watch - 监听器

// 所有依赖响应式对象监听
watchEffect(() => {
   results.value = getEventCount(searchInput.value);
 });

// 特定响应式对象监听
watch(
  searchInput,
  () => {
    console.log("watch searchInput:");
  }
);

// 特定响应式对象监听 可以获取新旧值
watch(
  searchInput,
 (newVal, oldVal) => {
    console.log("watch searchInput:", newVal, oldVal);
  },
);

// 多响应式对象监听
watch(
  [firstName,lastName],
 ([newFirst,newLast], [oldFirst,oldlast]) => {
   // .....
  },
  
);

// 非懒加载方式监听 可以设置初始值
watch(
  searchInput,
  (newVal, oldVal) => {
    console.log("watch searchInput:", newVal, oldVal);
  },
  {
    immediate: true, 
  }
);

九、Sharing State - 共享状态

[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

编写一个公共函数usePromise函数需求如下:

  • results : 返回Promise执行结果
  • loading: 返回Promise运行状态
    • PENDING :true
    • REJECTED : false
    • RESOLVED: false
  • error : 返回执行错误

1[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

import { ref } from "vue";

export default function usePromise(fn) {
  const results = ref(null);
  // is PENDING
  const loading = ref(false);
  const error = ref(null);

  const createPromise = async (...args) => {
    loading.value = true;
    error.value = null;
    results.value = null;
    try {
      results.value = await fn(...args);
    } catch (err) {
      error.value = err;
    } finally {
      loading.value = false;
    }
  };
  return { results, loading, error, createPromise };
}

应用

import { ref, watch } from "vue";
import usePromise from "./usePromise";
export default {
  setup() {
    const searchInput = ref("");
    function getEventCount() {
      return new Promise((resolve) => {
        setTimeout(() => resolve(3), 1000);
      });
    }

    const getEvents = usePromise((searchInput) => getEventCount());

    watch(searchInput, () => {
      if (searchInput.value !== "") {
        getEvents.createPromise(searchInput);
      } else {
        getEvents.results.value = null;
      }
    });

    return { searchInput, ...getEvents };
  },
};

十、Suspense - 悬念

复杂的Loading实现

我们考虑一下当你加载一个远程数据时,如何显示loading状态

通常我们可以在模板中使用v-if

1[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

但是在一个组件树中,其中几个子组件需要远程加载数据,当加载完成前父组件希望处于Loading状态时我们就必须借助全局状态管理来管理这个Loading状态

1[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

Suspense基础语法

这个问题在Vue3中有一个全新的解决方法。

这就是Suspense Component,悬念组件。

1[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

<template>
  <div>
    <div>Uh oh .. {{ error }}</div>
    <suspense>
      <template>
        <div>
          <event></event>
          <asyncevent></asyncevent>
        </div>
      </template>
      <template> Loading.... </template>
    </suspense>
  </div>
</template>

<script>
import { ref, onErrorCaptured, defineAsyncComponent } from "vue";

import Event from "./Event.vue";

const AsyncEvent = defineAsyncComponent(() => import("./Event.vue"));
export default {
  components: {
    Event,
    AsyncEvent,
  },

  setup() {
    const error = ref(null);
    onErrorCaptured((e) => {
      error.value = e;
      // 阻止错误继续冒泡
      return true;
    });
    return { error };
  },
};
</script>

骨架屏实现

1[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

1[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

十一、Teleport - 传送门

功能

类似React中的Portal, 可以将特定的html模板传送到Dom的任何位置

1[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

基础语法

通过选择器QuerySelector配置

1[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

示例代码

1[편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명

<template>
  <div>
    <teleport>
      <!-- 【Teleport : This should be at the end 】 -->
      <div>
        <video>
          
        </video>
      </div>
    </teleport>
    <div>【Teleport : This should be at the top】</div>
    <button>Toggle showText</button>
  </div>
</template>
<script>
import { ref } from "vue";
export default {
  setup() {
    const showText = ref(false);
    setInterval(() => {
      showText.value = !showText.value;
    }, 1000);
    return { showText };
  },
};
</script>

更多编程相关知识,请访问:编程入门!!

위 내용은 [편집 및 요약] Vue3의 11가지 지식 포인트에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 poetries에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제