哪个网站做黄金交易最好,转载到wordpress,网站内容建设和运营工作,网上购物平台有哪些第Ⅷ章-Ⅱ 组合式API使用 provide与inject的使用vue 生命周期的用法编程式路由的使用vuex的使用获取DOM的使用setup语法糖setup语法糖的基本结构响应数据的使用其它语法的使用引入组件的使用 父组件传值的使用defineProps 父传子defineEmits 子传父 provide与inject的使用
pro… 第Ⅷ章-Ⅱ 组合式API使用 provide与inject的使用vue 生命周期的用法编程式路由的使用vuex的使用获取DOM的使用setup语法糖setup语法糖的基本结构响应数据的使用其它语法的使用引入组件的使用 父组件传值的使用defineProps 父传子defineEmits 子传父 provide与inject的使用
provide 与 inject 是 Vue 3 中用于跨组件树传递数据的 API适合解决深层嵌套组件的通信问题。
provide父组件提供数据。inject子组件接收数据。
!-- src/App.vue --
templatedivProviderComponent //div
/templatescript setup
import ProviderComponent from ./components/ProviderComponent.vue;
/script!-- src/components/ProviderComponent.vue --
templatedivh1Provider Component/h1ChildComponent //div
/templatescript setup
import { provide } from vue;
import ChildComponent from ./ChildComponent.vue;const providedData Hello from Provider!;
provide(message, providedData);
/script!-- src/components/ChildComponent.vue --
templatedivh2Child Component/h2p{{ message }}/p/div
/templatescript setup
import { inject } from vue;const message injectstring(message, Default Message);
/scriptvue 生命周期的用法
Vue 3 引入了组合式 API使得生命周期钩子以函数形式使用增加了灵活性。
templatediv{{ message }}/div
/templatescript setup langts
import { ref, onMounted, onBeforeUnmount } from vue;const message ref(Hello, Vue 3!);onMounted(() {console.log(Component is mounted);message.value Component Mounted;
});onBeforeUnmount(() {console.log(Component is about to unmount);
});
/script
编程式路由的使用
Vue Router 支持编程式路由跳转可以使用 router.push 和 router.replace。
!-- src/components/NavigationComponent.vue --
templatedivbutton clickgoToHomeGo to Home/buttonbutton clickreplaceWithAboutReplace with About/buttonbutton clickgoBackGo Back/button/div
/templatescript setup
import { useRouter } from vue-router;const router useRouter();const goToHome () {router.push(/home);
};const replaceWithAbout () {router.replace(/about);
};const goBack () {router.go(-1);
};
/scriptvuex的使用
Vuex 是 Vue 官方的状态管理库通常使用 createStore 创建全局 store。
// src/store/index.ts
import { createStore } from vuex;interface State {count: number;
}const store createStoreState({state: {count: 0},mutations: {increment(state) {state.count 1;},decrement(state) {state.count - 1;}},actions: {incrementAsync({ commit }) {setTimeout(() {commit(increment);}, 1000);}},getters: {doubleCount(state) {return state.count * 2;}}
});export default store;在组件中使用 Vuex 状态和方法
!-- src/components/CounterComponent.vue --
templatedivh2Counter Example/h2pCount: {{ count }}/ppDouble Count (getter): {{ doubleCount }}/pbutton clickincrementIncrement/buttonbutton clickdecrementDecrement/buttonbutton clickincrementAsyncIncrement Async/button/div
/templatescript setup langts
import { computed } from vue;
import { useStore } from vuex;const store useStore();const count computed(() store.state.count);
const doubleCount computed(() store.getters.doubleCount);
const increment () store.commit(increment);
const decrement () store.commit(decrement);
const incrementAsync () store.dispatch(incrementAsync);
/script获取DOM的使用
在组合式 API 中可以使用 ref 和 onMounted 钩子来访问 DOM 元素。
templatedivinput typetext refinputElement placeholderType something... /button clickfocusInputFocus Input/button/div
/templatescript setup langts
import { ref, onMounted } from vue;const inputElement refHTMLInputElement | null(null);const focusInput () {inputElement.value?.focus();
};onMounted(() {console.log(Component Mounted and DOM is ready);
});
/scriptsetup语法糖
setup 语法糖 在 Vue 3.3 中引入它简化了 setup 函数的使用使得代码更加简洁易读。
setup语法糖的基本结构
templatediv{{ message }}/divbutton clickincrementIncrement: {{ count }}/button
/templatescript setup
import { ref } from vue;const message ref(Hello, Vue 3!);
const count ref(0);const increment () {count.value;
};
/script响应数据的使用
ref创建单一响应式值。reactive创建响应式对象。toRefs将 reactive 对象转换为 ref 对象。
templatedivp{{ message }}/ppCounter: {{ count }}/p/div
/templatescript setup
import { ref, reactive, toRefs } from vue;const message ref(Hello, Vue 3!);
const state reactive({ count: 0 });const { count } toRefs(state);
/script其它语法的使用
computed创建计算属性。watch观察响应式数据的变化并执行副作用。
templatedivpDouble Count: {{ doubleCount }}/pinput v-modelname placeholderName /p{{ name }}/p/div
/templatescript setup
import { ref, computed, watch } from vue;const count ref(2);
const doubleCount computed(() count.value * 2);const name ref(Alice);
watch(name, (newValue, oldValue) {console.log(Name changed from ${oldValue} to ${newValue});
});
/script引入组件的使用
!-- src/App.vue --
templateCounterComponent /
/templatescript setup
import CounterComponent from ./components/CounterComponent.vue;
/script父组件传值的使用
defineProps 父传子
!-- src/components/ChildComponent.vue --
templatep{{ message }}/p
/templatescript setup
import { defineProps } from vue;const props defineProps({message: String
});
/script!-- src/App.vue --
templateChildComponent messageHello, Child! /
/templatescript setup
import ChildComponent from ./components/ChildComponent.vue;
/scriptdefineEmits 子传父
!-- src/components/ChildComponent.vue --
templatebutton clicksendMessageSend Message to Parent/button
/templatescript setup
import { defineEmits } from vue;const emit defineEmits([message]);const sendMessage () {emit(message, Hello from Child Component!);
};
/script!-- src/App.vue --
templateChildComponent messagehandleMessage /p{{ parentMessage }}/p
/templatescript setup
import { ref } from vue;
import ChildComponent from ./components/ChildComponent.vue;const parentMessage ref();const handleMessage (msg: string) {parentMessage.value msg;
};
/script