xb18
xb18
文章78
标签0
分类0
Vue

Vue

provide

默认情况下,provide/inject 绑定并不是响应式的

在vue3中,我们可以通过传递一个 ref property 或 reactive 对象给 provide 来改变这种行为

vue2

保持响应性可以使用函数,动态获取; 或者利用对象的响应性

父组件

  provide() {
    return {
      text: () => this.text, // 函数
      textObj: {
          text: this.text
      }
    };
  },
  data() {
    return {
      text: 'provide',
    };
  },

子组件

export default {
  inject: ['text', 'textObj'],
  computed: {
    comText() {
      return this.text();
    },
    text() {
        return this.textObj.text;
    }
  },
};

Ref

1
2
3
4
5
6
7
8
9
10
11
12
// 伪代码,不是真正的实现
const myRef = {
_value: 0,
get value() {
track()
return this._value
},
set value(newValue) {
this._value = newValue
trigger()
}
}

当你在模板中使用了一个 ref,然后改变了这个 ref 的值时,Vue 会自动检测到这个变化,并且相应地更新 DOM。这是通过一个基于依赖追踪的响应式系统实现的。当一个组件首次渲染时,Vue 会追踪在渲染过程中使用的每一个 ref。然后,当一个 ref 被修改时,它会触发追踪它的组件的一次重新渲染。

在标准的 JavaScript 中,检测普通变量的访问或修改是行不通的。然而,我们可以通过 getter 和 setter 方法来拦截对象属性的 get 和 set 操作。

.value 属性给予了 Vue 一个机会来检测 ref 何时被访问或修改。在其内部,Vue 在它的 getter 中执行追踪,在它的 setter 中执行触发。

指令

注册指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// v-focus

const app = createApp({})

// 使 v-focus 在所有组件中都可用

app.directive('focus', {
/* ... */
})

// 局部
export default {
setup() {
/*...*/
},
directives: {
// 在模板中启用 v-focus
focus: {
/* ... */
}
}
}

// setup
<script setup>
// 在模板中启用 v-focus
const vFocus = {
mounted: (el) => el.focus()
}
</script>

<template>
<input v-focus />
</template>

钩子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const myDirective = {
// 在绑定元素的 attribute 前
// 或事件监听器应用前调用
created(el, binding, vnode, prevVnode) {
// 下面会介绍各个参数的细节
},
// 在元素被插入到 DOM 前调用
beforeMount(el, binding, vnode, prevVnode) {},
// 在绑定元素的父组件
// 及他自己的所有子节点都挂载完成后调用
mounted(el, binding, vnode, prevVnode) {},
// 绑定元素的父组件更新前调用
beforeUpdate(el, binding, vnode, prevVnode) {},
// 在绑定元素的父组件
// 及他自己的所有子节点都更新后调用
updated(el, binding, vnode, prevVnode) {},
// 绑定元素的父组件卸载前调用
beforeUnmount(el, binding, vnode, prevVnode) {},
// 绑定元素的父组件卸载后调用
unmounted(el, binding, vnode, prevVnode) {}
}

参数:

指令的钩子会传递以下几种参数:

  • el:指令绑定到的元素。这可以用于直接操作 DOM。
  • binding:一个对象,包含以下属性。
    • value:传递给指令的值。例如在 v-my-directive="1 + 1" 中,值是 2
    • oldValue:之前的值,仅在 beforeUpdateupdated 中可用。无论值是否更改,它都可用。
    • arg:传递给指令的参数 (如果有的话)。例如在 v-my-directive:foo 中,参数是 "foo"
    • modifiers:一个包含修饰符的对象 (如果有的话)。例如在 v-my-directive.foo.bar 中,修饰符对象是 { foo: true, bar: true }
    • instance:使用该指令的组件实例。
    • dir:指令的定义对象。
  • vnode:代表绑定元素的底层 VNode。
  • prevNode:代表之前的渲染中指令所绑定元素的 VNode。仅在 beforeUpdateupdated 钩子中可用。

binding:

1
2
3
4
5
6
7
<div v-example:foo.bar="baz">
{
arg: 'foo',
modifiers: { bar: true },
value: /* `baz` 的值 */,
oldValue: /* 上一次更新时 `baz` 的值 */
}

简化

1
2
3
4
app.directive('color', (el, binding) => {
// 这会在 `mounted` 和 `updated` 时都调用
el.style.color = binding.value
})

插件

注册插件

1
2
3
4
5
6
7
import { createApp } from 'vue'

const app = createApp({})

app.use(myPlugin, {
/* 可选的选项 */
})

i18n插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// plugins/i18n.js
export default {
install: (app, options) => {
// 注入一个全局可用的 $translate() 方法
app.config.globalProperties.$translate = (key) => {
// 获取 `options` 对象的深层属性
// 使用 `key` 作为索引
return key.split('.').reduce((options, i) => {
if (options) return options[i]
}, options)
}
}
}


// main.js
import i18nPlugin from './plugins/i18n'

app.use(i18nPlugin, {
greetings: {
hello: 'Bonjour!'
}
})

// template
$translate('greetings.hello')

Provide / Inject

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// plugins/i18n.js
export default {
install: (app, options) => {
app.provide('i18n', options)
}
}

// setup
<script setup>
import { inject } from 'vue'

const i18n = inject('i18n')

console.log(i18n.greetings.hello)
</script>
本文作者:xb18
本文链接:https://moelj.com/2024/02/26/Vue/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可