当前位置: 首页 > news >正文

免费照片的网站模板免费下载wordpress 支付接口

免费照片的网站模板免费下载,wordpress 支付接口,网站导航做外链,域名主机基地1. 基本概念 provide/inject 是 Vue3 中实现跨层级组件通信的方案#xff0c;类似于 React 的 Context。它允许父组件向其所有子孙组件注入依赖#xff0c;无论层级有多深。 1.1 基本语法 // 提供方#xff08;父组件#xff09; const value ref(hello) provide(key, …1. 基本概念 provide/inject 是 Vue3 中实现跨层级组件通信的方案类似于 React 的 Context。它允许父组件向其所有子孙组件注入依赖无论层级有多深。 1.1 基本语法 // 提供方父组件 const value ref(hello) provide(key, value)// 注入方子孙组件 const value inject(key)2. 基础用法 2.1 提供静态值 !-- Parent.vue -- script setup import { provide } from vue// 提供静态值 provide(theme, dark) provide(language, zh-CN) /script!-- Child.vue -- script setup import { inject } from vue// 注入值 const theme inject(theme) const language inject(language) /scripttemplatediv :classthemepCurrent Language: {{ language }}/p/div /template2.2 提供响应式数据 !-- Parent.vue -- script setup import { provide, ref } from vueconst count ref(0) const updateCount () {count.value }// 提供响应式数据和更新方法 provide(count, {count,updateCount }) /script!-- Child.vue -- script setup import { inject } from vueconst { count, updateCount } inject(count) /scripttemplatedivpCount: {{ count }}/pbutton clickupdateCountIncrement/button/div /template3. 进阶用法 3.1 使用 Symbol 作为 key // injection-keys.ts export const COUNT_KEY Symbol(count) export const THEME_KEY Symbol(theme)!-- Parent.vue -- script setup langts import { provide } from vue import { COUNT_KEY, THEME_KEY } from ./injection-keysconst count ref(0) provide(COUNT_KEY, count) provide(THEME_KEY, dark) /script!-- Child.vue -- script setup langts import { inject } from vue import { COUNT_KEY, THEME_KEY } from ./injection-keysconst count inject(COUNT_KEY) const theme inject(THEME_KEY) /script3.2 提供默认值 script setup import { inject } from vue// 使用静态默认值 const theme inject(theme, light)// 使用工厂函数作为默认值 const now inject(timestamp, () Date.now()) /script3.3 只读数据 !-- Parent.vue -- script setup import { provide, ref, readonly } from vueconst count ref(0) // 提供只读版本防止子组件修改 provide(count, readonly(count))// 提供更新方法 provide(updateCount, () {count.value }) /script!-- Child.vue -- script setup import { inject } from vueconst count inject(count) const updateCount inject(updateCount) /script4. 实际应用场景 4.1 主题系统 !-- ThemeProvider.vue -- script setup import { provide, ref } from vueconst theme ref(light) const toggleTheme () {theme.value theme.value light ? dark : light }provide(theme, {theme,toggleTheme }) /scripttemplatediv :classtheme.valueslot/slot/div /template!-- 使用组件 -- script setup import { inject } from vueconst { theme, toggleTheme } inject(theme) /scripttemplatebutton clicktoggleThemeSwitch to {{ theme light ? dark : light }} mode/button /template4.2 多语言系统 !-- I18nProvider.vue -- script setup import { provide, ref } from vueconst locale ref(en) const messages {en: {greeting: Hello,farewell: Goodbye},zh: {greeting: 你好,farewell: 再见} }const t (key) messages[locale.value][key] const setLocale (lang) {locale.value lang }provide(i18n, {locale,t,setLocale }) /script!-- 使用组件 -- script setup import { inject } from vueconst { t, setLocale, locale } inject(i18n) /scripttemplatedivselect v-modellocaleoption valueenEnglish/optionoption valuezh中文/option/selectp{{ t(greeting) }}/p/div /template4.3 状态管理 !-- Store.vue -- script setup import { provide, reactive } from vueconst store reactive({user: null,todos: [],addTodo(text) {this.todos.push({ id: Date.now(), text, completed: false })},toggleTodo(id) {const todo this.todos.find(t t.id id)if (todo) {todo.completed !todo.completed}} })provide(store, store) /script!-- TodoList.vue -- script setup import { inject } from vueconst store inject(store) /scripttemplatedivinputv-modelnewTodokeyup.enterstore.addTodo(newTodo)ulliv-fortodo in store.todos:keytodo.idclickstore.toggleTodo(todo.id){{ todo.text }}/li/ul/div /template5. TypeScript 支持 5.1 类型定义 // types.ts export interface ThemeContext {theme: Reflight | darktoggleTheme: () void }export const ThemeSymbol Symbol(theme)5.2 带类型的 provide/inject script setup langts import { provide, inject } from vue import { ThemeContext, ThemeSymbol } from ./types// 提供方 provideThemeContext(ThemeSymbol, {theme: ref(light),toggleTheme: () { /* ... */ } })// 注入方 const theme injectThemeContext(ThemeSymbol) /script6. 最佳实践 使用 Symbol 作为 key const MyKey Symbol(my-key) provide(MyKey, value)提供只读数据 provide(data, readonly(data))集中管理 injection key // keys.ts export const THEME_KEY Symbol(theme) export const I18N_KEY Symbol(i18n)使用组合式函数封装 // useTheme.ts export function useTheme() {const theme inject(THEME_KEY)if (!theme) {throw new Error(useTheme must be used within ThemeProvider)}return theme }7. 注意事项 响应性 确保提供响应式数据时使用 ref 或 reactive注意数据的可变性和只读性 默认值 提供合理的默认值考虑使用工厂函数作为默认值 类型安全 使用 TypeScript 定义接口使用 Symbol 作为 key 性能考虑 避免提供过大的数据结构合理划分提供的数据范围 通过合理使用 provide/inject我们可以有效地管理跨组件通信构建可维护的组件树。但要注意避免过度使用以免造成数据流向难以追踪的问题。
http://www.hkea.cn/news/14421559/

相关文章:

  • 衡水景县专业做淘宝网站公司襄阳专业网站建设公司
  • 网站制作怎样做网站建设参考文献资料
  • c 做彩票网站pre_get_posts wordpress
  • 网站设计app满城区建设局网站
  • 做交流网站做得不好的知名企业网站
  • 佛山网站建设报价随州建设网站
  • php企业网站开发价格线上ui设计培训哪个好
  • 巩义网站建设方式优化企业邮箱怎么注册
  • 莆田cms建站模板什么响应式网站
  • 优化网站设计有哪些方法应该选用哪种口罩
  • 专门做签到的网站织梦网址导航网站模板
  • 北京市轨道交通建设管理有限公司网站widows安装wordpress
  • vs2013如何做网站5118关键词查询工具
  • 网站建设过程中什么最重要设计字体设计
  • 唐山网站建设选汉狮毕业设计做系统网站好
  • 上海网站设计开发公司做网站哪家公司专业
  • 珠海网站推广网站建设合同交印花税吗
  • 网站被降权了怎么办台州网站建设咨询薇
  • 洛阳网站建站asp提交到另外网站
  • 怎么在百度上能搜到自己的网站网站建设多少钱哪个济南兴田德润有活动吗
  • 网站开发与应用是什么wordpress redis memcached
  • 建设银行网站怎么打印明细2345浏览器主页网址
  • 做企业网站用什么程序闵行做网站公司铝棒易站公司
  • 重庆垫江网站建设为什么做网站能赚钱
  • 群晖如何做网站服务器柳州企业 商家应该如何做网站
  • 网站免费模版厦门住房建设局网站首页
  • 青岛百度公司总部沈阳seo顾问
  • 高度重视机关门户网站建设酒店seo是什么意思
  • 贵州省建设厅网站公众一体化平台html5做网站导航
  • 做推广可以在哪些网站发布软文程序员做网站外快