Skip to content

jQuery vue (Composition API) API

jQuery 核心 API —— 选择器和引擎、AJAX 辅助方法和 DOM 操作方法。

3 classes · 12 methods

Core

5 methods

jQuery 函数和工具,用于选择元素和链式操作。

ref<T>(value) -> Ref<T>

选择匹配 CSS 选择器的 DOM 元素,并将其包装在 jQuery 对象中。

Parameters

NameTypeDescription
valueTInitial value.

Returns

Ref<T>

Example

vue3
import { ref } from 'vue';
const count = ref(0);
count.value++;
console.log(count.value); // 1
reactive<T>(obj) -> UnwrapNestedRefs<T>

包装现有 DOM 元素或从 HTML 字符串创建新元素。

Parameters

NameTypeDescription
objTPlain object to wrap.

Returns

UnwrapNestedRefs<T>

Example

vue3
import { reactive } from 'vue';
const state = reactive({ count: 0 });
state.count++;
console.log(state.count); // 1
computed<T>(getter) -> ComputedRef<T>

遍历数组或对象的通用迭代器。从回调返回 false 可停止迭代。

Parameters

NameTypeDescription
getter() => TPure getter function.

Returns

ComputedRef<T>

Example

vue3
import { ref, computed } from 'vue';
const count = ref(2);
const doubled = computed(() => count.value * 2);
console.log(doubled.value); // 4
watch(source, callback, options?)

将两个或多个对象的内容合并到第一个对象中。

Parameters

NameTypeDescription
sourceRef | (() => any) | ArrayReactive source(s) to watch.
callback(newVal, oldVal, onCleanup) => voidSide-effect to run on change.
optionsWatchOptionsOptional { immediate, deep, flush }.

Returns

StopHandle

Example

vue3
watch(count, (n, o) => {
  console.log('changed', o, '->', n);
}, { immediate: true });
watchEffect(effect) -> StopHandle

在 DOM 完全解析并就绪后运行函数($(document).ready 的简写)。

Parameters

NameTypeDescription
effect(onCleanup) => voidEffect function.

Returns

StopHandle

Example

vue3
watchEffect(() => {
  console.log('count is', count.value);
});

AJAX

4 methods

jQuery AJAX 辅助方法,用于发起 HTTP 请求和处理响应。

defineComponent(options) -> Component

执行异步 HTTP 请求。返回类似 Promise 的 jqXHR 对象。

Parameters

NameTypeDescription
optionsComponentOptionsComponent config: data, props, methods, computed, setup.

Returns

Component

Example

vue3
import { defineComponent } from 'vue';
export default defineComponent({
  props: { msg: String },
  setup(props) {
    return { upper: () => props.msg.toUpperCase() };
  },
});
defineProps<T>() / defineProps(arrayOrObject)

HTTP GET 请求的简写。

Returns

Props object

Example

vue3
<script setup lang="ts">
const props = defineProps<{ id: number; name?: string }>();
console.log(props.id);
</script>
defineEmits<T>() / defineEmits(arrayOrObject)

HTTP POST 请求的简写。

Returns

Emit function

Example

vue3
<script setup lang="ts">
const emit = defineEmits<{ (e: 'change', v: number): void }>();
emit('change', 42);
</script>
onMounted(callback)

使用 GET HTTP 请求从服务器加载 JSON 编码的数据。

Parameters

NameTypeDescription
callback() => voidHook callback.

Returns

void

Example

vue3
import { onMounted } from 'vue';
onMounted(() => {
  console.log('mounted');
});

DOM

3 methods

jQuery DOM 操作、遍历和事件方法。

useXxx(...args) -> { state, methods }

获取第一个元素的 HTML 内容,或设置所有匹配元素的 HTML。

Parameters

NameTypeDescription
argsanyInputs to the composable.

Returns

object

Example

vue3
// useCounter.ts
import { ref } from 'vue';
export function useCounter(initial = 0) {
  const count = ref(initial);
  const inc = () => count.value++;
  return { count, inc };
}
// usage
const { count, inc } = useCounter(10);
useSlots() -> Slots

获取或设置匹配元素的合并文本内容。

Returns

Slots

Example

vue3
import { useSlots } from 'vue';
const slots = useSlots();
console.log(slots.default?.());
useAttrs() -> Record<string, unknown>

为选中元素的一个或多个事件附加事件处理程序。

Returns

Record<string, unknown>

Example

vue3
import { useAttrs } from 'vue';
const attrs = useAttrs();
console.log(attrs.class);